Skip to content

Commit 4dde561

Browse files
committed
implementing full_selection for SimpleCursesBoolean, SimpleCursesCounter and SimpleCursesString
1 parent cb9aae5 commit 4dde561

File tree

2 files changed

+180
-35
lines changed

2 files changed

+180
-35
lines changed

pyradio/browser.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,7 @@ class RadioBrowserConfigWindow(object):
15271527
invalid = False
15281528
_widgets = None
15291529
_params = []
1530+
_focused = 0
15301531

15311532
def __init__(
15321533
self,
@@ -1602,6 +1603,31 @@ def _fix_server(self, server):
16021603
return 'Random'
16031604
return server
16041605

1606+
def _focus_next(self):
1607+
if self._focused == len(self._widgets) - 1:
1608+
self._focused = 0
1609+
else:
1610+
self._focused += 1
1611+
self._refresh()
1612+
1613+
def _focus_previous(self):
1614+
if self._focused == 0:
1615+
self._focused = len(self._widgets) - 1
1616+
else:
1617+
self._focused -= 1
1618+
self._refresh()
1619+
1620+
def _refresh(self):
1621+
self._fix_focus()
1622+
self._win.refresh()
1623+
1624+
def _fix_focus(self, show=True):
1625+
for i, widg in enumerate(self._widgets):
1626+
widg.focused = True if self._focused == i else False
1627+
if show:
1628+
for n in self._widgets:
1629+
n.show(self._win)
1630+
16051631
def _init_set_working_params(self,
16061632
auto_save,
16071633
server,
@@ -1687,41 +1713,48 @@ def show(self, parent, init=False):
16871713
Y=2, X=3,
16881714
window=self._win,
16891715
color=curses.color_pair(5),
1690-
color_focused=curses.color_pair(9),
1716+
color_focused=curses.color_pair(6),
16911717
color_not_focused=curses.color_pair(4),
16921718
color_disabled=curses.color_pair(5),
16931719
value=self._params[0]['auto_save'],
1694-
string='Auto save config: {0}'
1720+
string='Auto save config: {0}',
1721+
full_selection=(2,58)
16951722
)
16961723
)
1724+
self._widgets[-1].token = 'auto_save'
16971725

16981726
self._widgets.append(
16991727
SimpleCursesCounter(
17001728
Y=4, X=3,
17011729
window=self._win,
17021730
color=curses.color_pair(5),
1703-
color_focused=curses.color_pair(9),
1731+
color_focused=curses.color_pair(6),
17041732
color_not_focused=curses.color_pair(4),
17051733
color_disabled=curses.color_pair(5),
17061734
minimum=0, maximum=1000,
17071735
step=1, big_step=10,
17081736
value=self._params[0]['limit'],
1709-
string='Maximum number of results: {0}'
1737+
string='Maximum number of results: {0}',
1738+
full_selection=(2,58)
17101739
)
17111740
)
1741+
self._widgets[-1].token = 'limit'
17121742

17131743
self._widgets.append(
1714-
SimpleCursesString(
1715-
Y=6, X=3,
1716-
parent=self._win,
1717-
caption='Default Server: ',
1718-
string=self._params[0]['server'],
1719-
color=curses.color_pair(5),
1720-
color_focused=curses.color_pair(9),
1721-
color_not_focused=curses.color_pair(4),
1722-
color_disabled=curses.color_pair(5),
1723-
)
1744+
SimpleCursesString(
1745+
Y=6, X=3,
1746+
parent=self._win,
1747+
caption='Default Server: ',
1748+
string=self._params[0]['server'],
1749+
color=curses.color_pair(5),
1750+
color_focused=curses.color_pair(6),
1751+
color_not_focused=curses.color_pair(4),
1752+
color_disabled=curses.color_pair(5),
1753+
full_selection=(2,58)
1754+
)
17241755
)
1756+
self._widgets[-1].token = 'server'
1757+
self._fix_focus(show=False)
17251758
logger.error('{}'.format(self._widgets))
17261759
for n in self._widgets:
17271760
n.show(self._win)
@@ -1756,6 +1789,11 @@ def keypress(self, char):
17561789
ret = self._handle_new_or_existing_search_term()
17571790
return 0 if ret == 1 else ret
17581791

1792+
elif char in (ord('\t'), 9):
1793+
self._focus_next()
1794+
elif char in (curses.KEY_BTAB, ):
1795+
self._focus_previous()
1796+
17591797
class RadioBrowserSearchWindow(object):
17601798

17611799
NUMBER_OF_WIDGETS_AFTER_SEARCH_SECTION = 3

pyradio/simple_curses_widgets.py

Lines changed: 128 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,32 @@ def keypress(char):
238238

239239

240240
class SimpleCursesString(SimpleCursesWidget):
241+
''' A class to provide a String value
242+
243+
Parameters
244+
==========
245+
Y, X, window
246+
Coordinates and parent window
247+
caption
248+
preffix string
249+
string
250+
the string to display (variable)
251+
color
252+
text color
253+
color_focused
254+
counter color when enabled and focused
255+
color_not_focused
256+
counter color when enabled but not focused
257+
color_disabled
258+
counter color when disabled
259+
full_slection
260+
if not None, it should be a tuple:
261+
(go left from self._X, numbder of chars to print)
262+
draw selection cursor as a full line
263+
'''
264+
265+
266+
_max_string = 0
241267

242268
def __init__(
243269
self,
@@ -248,19 +274,23 @@ def __init__(
248274
color_not_focused,
249275
color_disabled,
250276
right_arrow_selects=True,
251-
callback_function_on_activation=None
277+
callback_function_on_activation=None,
278+
full_selection=None
252279
):
253280
self._Y = Y
254281
self._X = X
255282
self._win = self._parent = parent
256283
self._string = string
284+
if cjklen(self._string) > self._max_string:
285+
self._max_string = cjklen(self._string)
257286
self._caption = caption
258287
self._color = color
259288
self._color_focused = color_focused
260289
self._color_not_focused = color_not_focused
261290
self._color_disabled = color_disabled
262291
self._right_arrow_selects = right_arrow_selects
263292
self._callback_function_on_activation = callback_function_on_activation
293+
self._full_selection = full_selection
264294

265295
@property
266296
def caption(self):
@@ -277,6 +307,8 @@ def string(self):
277307
@string.setter
278308
def string(self, value):
279309
self._string = value
310+
if cjklen(self._string) > self._max_string:
311+
self._max_string = cjklen(self._string)
280312

281313
@property
282314
def string_len(self):
@@ -315,11 +347,36 @@ def keypress(self, char):
315347
self._callback_function_on_activation()
316348
return ret
317349

350+
def _print_full_line(self, col):
351+
tmp = self._full_selection[0] * ' ' + self.caption + self._string
352+
self._win.addstr(
353+
self._Y,
354+
self._X - self._full_selection[0],
355+
tmp.ljust(self._full_selection[1]),
356+
col
357+
)
318358
def show(self, parent=None):
319359
if parent:
320360
self._win = self_parent = parent
321-
self._win.addstr(self._Y, self._X, self._caption, self._color)
322-
self._win.addstr(self._string, self._color_not_focused)
361+
if self._full_selection and self._enabled and self._focused:
362+
self._print_full_line(self._color_focused)
363+
else:
364+
if self._full_selection:
365+
self._win.addstr(
366+
self._Y,
367+
self._X - self._full_selection[0],
368+
(self._full_selection[1] ) * ' ',
369+
self._color
370+
)
371+
if self._enabled:
372+
self._win.addstr(self._Y, self._X, self._caption, self._color)
373+
if self._focused:
374+
self._win.addstr(self._string.ljust(self._max_string), self._color_focused)
375+
else:
376+
self._win.addstr(self._string.ljust(self._max_string), self._color_not_focused)
377+
else:
378+
self._win.addstr(self._Y, self._X, self._caption, self._color_disabled)
379+
self._win.addstr(self._string.ljust(self._max_string), self._color_disabled)
323380

324381
class SimpleCursesCounter(SimpleCursesWidget):
325382
''' A class to provide a counter
@@ -344,6 +401,10 @@ class SimpleCursesCounter(SimpleCursesWidget):
344401
counter color when enabled but not focused
345402
color_disabled
346403
counter color when disabled
404+
full_slection
405+
if not None, it should be a tuple:
406+
(go left from self._X, numbder of chars to print)
407+
draw selection cursor as a full line
347408
'''
348409
def __init__(
349410
self, Y, X, window,
@@ -352,7 +413,8 @@ def __init__(
352413
color_disabled,
353414
minimum=0, maximum=100,
354415
step=1, big_step=5, value=1,
355-
number_length=3, string='{0}'
416+
number_length=3, string='{0}',
417+
full_selection=None
356418
):
357419
self._Y = Y
358420
self._X = X
@@ -375,6 +437,7 @@ def __init__(
375437
self._color_focused = color_focused
376438
self._color_not_focused = color_not_focused
377439
self._color_disabled = color_disabled
440+
self._full_selection = full_selection
378441

379442
def refresh(self):
380443
self.show(self._win)
@@ -457,6 +520,15 @@ def move(self, newY=-1, newX=-1, parent=None):
457520
if parent:
458521
self._win = self._parent = parent
459522

523+
def _print_full_line(self, col):
524+
tmp = self._full_selection[0] * ' ' + self._prefix + str(self._value).rjust(self._len) + self._suffix
525+
self._win.addstr(
526+
self._Y,
527+
self._X - self._full_selection[0],
528+
tmp.ljust(self._full_selection[1]),
529+
col
530+
)
531+
460532
def show(self, window, opening=False):
461533
if window:
462534
self._win = self._parent = window
@@ -467,14 +539,24 @@ def show(self, window, opening=False):
467539
col = self._color_not_focused
468540
else:
469541
col = self._color_disabled
470-
self._win.move(self._Y, self._X)
471-
if self._prefix:
472-
self._win.addstr(self._prefix, self._color)
473-
self._win.addstr(self._number.format(str(self._value).rjust(self._len)), col)
474-
if self._suffix:
475-
self._win.addstr(self._suffix, self._color)
476-
''' overwrite last self._len characters '''
477-
self._win.addstr(' ' * self._len, self._color)
542+
if self._full_selection and self._enabled and self._focused:
543+
self._print_full_line(col)
544+
else:
545+
if self._full_selection:
546+
self._win.addstr(
547+
self._Y,
548+
self._X - self._full_selection[0],
549+
(self._full_selection[1] ) * ' ',
550+
self._color
551+
)
552+
self._win.move(self._Y, self._X)
553+
if self._prefix:
554+
self._win.addstr(self._prefix, self._color)
555+
self._win.addstr(self._number.format(str(self._value).rjust(self._len)), col)
556+
if self._suffix:
557+
self._win.addstr(self._suffix, self._color)
558+
''' overwrite last self._len characters '''
559+
self._win.addstr(' ' * self._len, self._color)
478560
self._showed = True
479561

480562
def keypress(self, char):
@@ -2929,14 +3011,19 @@ class SimpleCursesBoolean(SimpleCursesCounter):
29293011
counter color when enabled but not focused
29303012
color_disabled
29313013
counter color when disabled
3014+
full_slection
3015+
if not None, it should be a tuple:
3016+
(go left from self._X, numbder of chars to print)
3017+
draw selection cursor as a full line
29323018
'''
29333019

29343020
def __init__(
29353021
self, Y, X, window,
29363022
color, color_focused,
29373023
color_not_focused,
29383024
color_disabled,
2939-
string='{0}', value=False
3025+
string='{0}', value=False,
3026+
full_selection=None
29403027
):
29413028
self._Y = Y
29423029
self._X = X
@@ -2947,6 +3034,16 @@ def __init__(
29473034
self._color_focused = color_focused
29483035
self._color_not_focused = color_not_focused
29493036
self._color_disabled = color_disabled
3037+
self._full_selection = full_selection
3038+
3039+
def _print_full_line(self, col):
3040+
tmp = self._full_selection[0] * ' ' + self._prefix + str(self._value).rjust(5) + self._suffix
3041+
self._win.addstr(
3042+
self._Y,
3043+
self._X - self._full_selection[0],
3044+
tmp.ljust(self._full_selection[1]),
3045+
col
3046+
)
29503047

29513048
def show(self, window=None):
29523049
if window:
@@ -2958,14 +3055,24 @@ def show(self, window=None):
29583055
col = self._color_not_focused
29593056
else:
29603057
col = self._color_disabled
2961-
self._win.move(self._Y, self._X)
2962-
if self._prefix:
2963-
self._win.addstr( self._prefix, self._color)
2964-
self._win.addstr(str(self._value), col)
2965-
if self._suffix:
2966-
self._win.addstr(self._suffix, self._color)
2967-
''' overwrite last self._len characters '''
2968-
self._win.addstr(' ' * 2, self._color)
3058+
if self._full_selection and self._enabled and self._focused:
3059+
self._print_full_line(col)
3060+
else:
3061+
if self._full_selection:
3062+
self._win.addstr(
3063+
self._Y,
3064+
self._X - self._full_selection[0],
3065+
(self._full_selection[1] ) * ' ',
3066+
self._color
3067+
)
3068+
self._win.move(self._Y, self._X)
3069+
if self._prefix:
3070+
self._win.addstr( self._prefix, self._color)
3071+
self._win.addstr(str(self._value).ljust(5), col)
3072+
if self._suffix:
3073+
self._win.addstr(self._suffix, self._color)
3074+
''' overwrite last self._len characters '''
3075+
self._win.addstr(' ' * 2, self._color)
29693076
self._showed = True
29703077

29713078
def keypress(self, char):

0 commit comments

Comments
 (0)