Skip to content

Commit b54e5b4

Browse files
committed
- counter and extra mode tokens will display correctly
- update notification will work correctly with beta, re, r releases
1 parent b5b5ae4 commit b54e5b4

File tree

2 files changed

+137
-111
lines changed

2 files changed

+137
-111
lines changed

pyradio/log.py

Lines changed: 90 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
class Log(object):
1010
""" Log class that outputs text to a curses screen """
1111

12-
msg = None
13-
cursesScreen = None
12+
msg = suffix = counter = cursesScreen = None
1413

1514
last_written_string = ''
1615
last_written_suffix=''
@@ -33,109 +32,119 @@ def setScreen(self, cursesScreen):
3332
if self.msg:
3433
self.write(self.msg)
3534

35+
def _do_i_print_last_char(self, first_print):
36+
if first_print:
37+
first_print = False
38+
try:
39+
self.cursesScreen.addstr(0, self.width +1, ' ')
40+
except:
41+
pass
42+
return first_print
43+
3644
def write(self, msg=None, suffix=None, counter=None, help_msg=False, notify_function=None):
3745
if self.asked_to_stop:
46+
self.counter = None
3847
return
39-
""" msg may or may not be encoded """
4048
if self.cursesScreen:
41-
if suffix == '':
42-
if msg is None:
43-
msg = self.last_written_string
44-
self.last_written_suffix = ''
45-
if msg:
49+
#if logger.isEnabledFor(logging.DEBUG):
50+
# logger.debug('before ----------------------------')
51+
# logger.debug('msg = "{}"'.format(msg))
52+
# logger.debug('self.msg = "{}"'.format(self.msg))
53+
# logger.debug('suffix = "{}"'.format(suffix))
54+
# logger.debug('self.suffix = "{}"'.format(self.suffix))
55+
# logger.debug('counter = "{}"'.format(counter))
56+
# logger.debug('self.counter = "{}"'.format(self.counter))
57+
58+
first_print = True
59+
if msg is not None: self.msg = msg
60+
if suffix is not None: self.suffix = suffix
61+
if counter is not None: self.counter = counter
62+
63+
#if logger.isEnabledFor(logging.DEBUG):
64+
# logger.debug('after ----------------------------')
65+
# logger.debug('msg = "{}"'.format(msg))
66+
# logger.debug('self.msg = "{}"'.format(self.msg))
67+
# logger.debug('suffix = "{}"'.format(suffix))
68+
# logger.debug('self.suffix = "{}"'.format(self.suffix))
69+
# logger.debug('counter = "{}"'.format(counter))
70+
# logger.debug('self.counter = "{}"'.format(self.counter))
71+
72+
""" update main message """
73+
if self.msg:
4674
if self.lock is not None:
4775
self.lock.acquire()
4876
self.cursesScreen.erase()
4977
try:
50-
self.msg = msg.strip()[0: self.width].replace("\r", "").replace("\n", "")
51-
self.cursesScreen.addstr(0, 1, self.msg)
78+
d_msg = self.msg.strip()[0: self.width].replace("\r", "").replace("\n", "")
79+
self.cursesScreen.addstr(0, 1, d_msg)
5280
except:
5381
try:
54-
self.msg = msg.encode('utf-8', 'replace').strip()[0: self.width].replace("\r", "").replace("\n", "")
55-
self.cursesScreen.addstr(0, 1, self.msg)
82+
d_msg = self.msg.encode('utf-8', 'replace').strip()[0: self.width].replace("\r", "").replace("\n", "")
83+
self.cursesScreen.addstr(0, 1, d_msg)
5684
except:
5785
if logger.isEnabledFor(logging.ERROR):
5886
logger.error('Cannot update the Status Bar...')
5987
if logger.isEnabledFor(logging.DEBUG):
6088
try:
61-
logger.debug('Status: "{}"'.format(msg))
89+
logger.debug('Status: "{}"'.format(self.msg))
6290
except:
6391
pass
6492
self.cursesScreen.refresh()
6593
if self.lock is not None:
6694
self.lock.release()
67-
self.last_written_string = msg
68-
suffix_string = ''
95+
96+
self._active_width = self.width
97+
98+
""" display suffix """
99+
if self.suffix:
100+
d_msg = ' [' + self.suffix + ']'
101+
if self.lock is not None:
102+
self.lock.acquire()
103+
self.cursesScreen.addstr(0, self._active_width - len(d_msg), d_msg)
104+
self.cursesScreen.chgat(0, self._active_width - len(d_msg) +1, len(d_msg) -1, curses.color_pair(1))
105+
first_print = self._do_i_print_last_char(first_print)
106+
self.cursesScreen.refresh()
107+
if self.lock is not None:
108+
self.lock.release()
109+
self._active_width -= len(d_msg)
110+
if logger.isEnabledFor(logging.DEBUG):
111+
logger.debug('Suffix: {}'.format(self.suffix))
112+
113+
""" display counter """
114+
if self.counter:
115+
if self.counter == '0':
116+
self.counter = None
117+
if self.counter:
118+
if self.suffix:
119+
self._active_width += 1
120+
d_msg = ' [' + self.counter + ']'
121+
if self.lock is not None:
122+
self.lock.acquire()
123+
self.cursesScreen.addstr(0, self._active_width - len(d_msg), d_msg)
124+
first_print = self._do_i_print_last_char(first_print)
125+
self.cursesScreen.refresh()
126+
if self.lock is not None:
127+
self.lock.release()
128+
self._active_width -= len(d_msg)
129+
if logger.isEnabledFor(logging.DEBUG):
130+
logger.debug('Counter: {}'.format(self.counter))
131+
132+
""" display press ? """
69133
if help_msg or self.display_help_message:
134+
self.counter = None
70135
suffix_string = ' Press ? for help'
136+
if self.lock is not None:
137+
self.lock.acquire()
138+
self.cursesScreen.addstr(0, self._active_width - len(suffix_string), suffix_string)
139+
self.cursesScreen.refresh()
140+
if self.lock is not None:
141+
self.lock.release()
71142
self.display_help_message = True
72-
self._color_change = False
73-
if suffix is None:
74-
# use last suffix
75-
if self.last_written_suffix:
76-
#suffix_string += ' ' + self.last_written_suffix
77-
suffix_string += ' [' + self.last_written_suffix + ']'
78-
self._color_change = True
79-
elif suffix == '':
80-
# clear last suffix
81-
self.last_written_suffix = ''
82-
self._color_change = False
143+
if logger.isEnabledFor(logging.DEBUG):
144+
logger.debug('Press ? for help: yes')
83145
else:
84-
# write new suffix
85-
suffix_string += ' [' + suffix + ']'
86-
#suffix_string += ' ' + suffix
87-
self.last_written_suffix = suffix
88-
self._color_change = True
89-
self._highlight_len = len(suffix)
90-
91-
if suffix_string:
92-
self._write_right(suffix_string)
93-
#self._write_right(suffix_string, counter='')
94-
if counter is not None:
95-
if self.last_written_suffix:
96-
suffix_string = '[' + self.last_written_suffix + ']'
97-
self._color_change = True
98-
self._write_right(suffix_string, ' [' + counter + ']')
99-
else:
100-
self._color_change = False
101-
self._write_right('', ' [' + counter + ']')
102-
103-
if notify_function:
104-
notify_function()
105-
106-
def _write_right(self, msg, counter=None):
107-
if self.asked_to_stop:
108-
return
109-
""" msg may or may not be encoded """
110-
a_msg=''
111-
if self.cursesScreen:
112-
if self.lock is not None:
113-
self.lock.acquire()
114-
logger.error('DE msg = {}'.format(msg))
115-
116-
if counter is not None:
117-
logger.error('DE writing counter... "{}"'.format(counter))
118-
try:
119-
if msg:
120-
self.cursesScreen.addstr(0, self.width - len(msg) - len(counter), counter)
121-
else:
122-
self.cursesScreen.addstr(0, self.width - len(counter), counter)
123-
except:
124-
pass
125-
logger.error('DE counter = {0}, msg = {1}, suf = {2}'.format(counter, msg, self.last_written_suffix))
126-
if msg:
127-
try:
128-
self.cursesScreen.addstr(0, self.width - len(msg), msg.replace("\r", "").replace("\n", ""))
129-
except:
130-
msg = msg.encode('utf-8', 'replace')
131-
self.cursesScreen.addstr(0, self.width - len(msg), msg.replace("\r", "").replace("\n", ""))
132-
#if logger.isEnabledFor(logging.DEBUG):
133-
# logger.debug('Status right: "{}"'.format(msg))
134-
if self._color_change:
135-
self.cursesScreen.chgat(0, self.width + 2 - self._highlight_len, self._highlight_len + 2, curses.color_pair(1))
136-
self.cursesScreen.refresh()
137-
if self.lock is not None:
138-
self.lock.release()
146+
if logger.isEnabledFor(logging.DEBUG):
147+
logger.debug('Press ? for help: no')
139148

140149
def readline(self):
141150
pass

0 commit comments

Comments
 (0)