From 538f215458891f045b2de6a8b675db48754fbb4a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 6 Apr 2015 17:45:36 +1200 Subject: console: factor out selection widget --- libmproxy/console/select.py | 106 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 libmproxy/console/select.py (limited to 'libmproxy/console/select.py') diff --git a/libmproxy/console/select.py b/libmproxy/console/select.py new file mode 100644 index 00000000..411fc179 --- /dev/null +++ b/libmproxy/console/select.py @@ -0,0 +1,106 @@ +import urwid + +from . import common + +class _OptionWidget(urwid.WidgetWrap): + def __init__(self, option, text, shortcut, active, focus): + self.option = option + textattr = "text" + keyattr = "key" + if focus and active: + textattr = "option_active_selected" + keyattr = "option_selected_key" + elif focus: + textattr = "option_selected" + keyattr = "option_selected_key" + elif active: + textattr = "option_active" + text = common.highlight_key( + text, + shortcut, + textattr = textattr, + keyattr = keyattr + ) + opt = urwid.Text(text, align="left") + opt = urwid.AttrWrap(opt, textattr) + opt = urwid.Padding(opt, align = "center", width = 40) + urwid.WidgetWrap.__init__(self, opt) + + def keypress(self, size, key): + return key + + def selectable(self): + return True + + +class OptionWalker(urwid.ListWalker): + def __init__(self, options): + urwid.ListWalker.__init__(self) + self.options = options + self.focus = 0 + + def set_focus(self, pos): + self.focus = pos + + def get_focus(self): + return self.options[self.focus].render(True), self.focus + + def get_next(self, pos): + if pos >= len(self.options)-1: + return None, None + return self.options[pos+1].render(False), pos+1 + + def get_prev(self, pos): + if pos <= 0: + return None, None + return self.options[pos-1].render(False), pos-1 + + +class Heading: + def __init__(self, text): + self.text = text + + def render(self, focus): + opt = urwid.Text("\n" + self.text, align="left") + opt = urwid.AttrWrap(opt, "title") + opt = urwid.Padding(opt, align = "center", width = 40) + return opt + + +_neg = lambda: False +class Option: + def __init__(self, text, shortcut, getstate=None, activate=None): + self.text = text + self.shortcut = shortcut + self.getstate = getstate or _neg + self.activate = activate or _neg + + def render(self, focus): + return _OptionWidget(self, self.text, self.shortcut, self.getstate(), focus) + + +class Select(urwid.ListBox): + def __init__(self, options): + self.walker = OptionWalker(options) + urwid.ListBox.__init__( + self, + self.walker + ) + self.options = options + self.keymap = {} + for i in options: + if hasattr(i, "shortcut"): + if i.shortcut in self.keymap: + raise ValueError("Duplicate shortcut key: %s"%i.shortcut) + self.keymap[i.shortcut] = i + + def keypress(self, size, key): + if key == "enter" or key == " ": + self.get_focus()[0].option.activate() + return None + key = common.shortcuts(key) + if key in self.keymap: + self.keymap[key].activate() + self.set_focus(self.options.index(self.keymap[key])) + return None + return super(self.__class__, self).keypress(size, key) -- cgit v1.2.3 From 1cb1ee411b9c8ffc40f83bcca99770af7f43a521 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 7 Apr 2015 08:42:40 +1200 Subject: console: palette picker for the options screen --- libmproxy/console/select.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'libmproxy/console/select.py') diff --git a/libmproxy/console/select.py b/libmproxy/console/select.py index 411fc179..61ee50e4 100644 --- a/libmproxy/console/select.py +++ b/libmproxy/console/select.py @@ -15,12 +15,13 @@ class _OptionWidget(urwid.WidgetWrap): keyattr = "option_selected_key" elif active: textattr = "option_active" - text = common.highlight_key( - text, - shortcut, - textattr = textattr, - keyattr = keyattr - ) + if shortcut: + text = common.highlight_key( + text, + shortcut, + textattr = textattr, + keyattr = keyattr + ) opt = urwid.Text(text, align="left") opt = urwid.AttrWrap(opt, textattr) opt = urwid.Padding(opt, align = "center", width = 40) @@ -89,7 +90,7 @@ class Select(urwid.ListBox): self.options = options self.keymap = {} for i in options: - if hasattr(i, "shortcut"): + if hasattr(i, "shortcut") and i.shortcut: if i.shortcut in self.keymap: raise ValueError("Duplicate shortcut key: %s"%i.shortcut) self.keymap[i.shortcut] = i -- cgit v1.2.3 From a05a70d8168a07c92b2a3ecbbb1958d85532efe3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 30 May 2015 12:03:28 +1200 Subject: Add coding style check, reformat. --- libmproxy/console/select.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'libmproxy/console/select.py') diff --git a/libmproxy/console/select.py b/libmproxy/console/select.py index 61ee50e4..bf96a785 100644 --- a/libmproxy/console/select.py +++ b/libmproxy/console/select.py @@ -2,6 +2,7 @@ import urwid from . import common + class _OptionWidget(urwid.WidgetWrap): def __init__(self, option, text, shortcut, active, focus): self.option = option @@ -47,14 +48,14 @@ class OptionWalker(urwid.ListWalker): return self.options[self.focus].render(True), self.focus def get_next(self, pos): - if pos >= len(self.options)-1: + if pos >= len(self.options) - 1: return None, None - return self.options[pos+1].render(False), pos+1 + return self.options[pos + 1].render(False), pos + 1 def get_prev(self, pos): if pos <= 0: return None, None - return self.options[pos-1].render(False), pos-1 + return self.options[pos - 1].render(False), pos - 1 class Heading: @@ -69,6 +70,8 @@ class Heading: _neg = lambda: False + + class Option: def __init__(self, text, shortcut, getstate=None, activate=None): self.text = text @@ -77,7 +80,12 @@ class Option: self.activate = activate or _neg def render(self, focus): - return _OptionWidget(self, self.text, self.shortcut, self.getstate(), focus) + return _OptionWidget( + self, + self.text, + self.shortcut, + self.getstate(), + focus) class Select(urwid.ListBox): @@ -92,7 +100,7 @@ class Select(urwid.ListBox): for i in options: if hasattr(i, "shortcut") and i.shortcut: if i.shortcut in self.keymap: - raise ValueError("Duplicate shortcut key: %s"%i.shortcut) + raise ValueError("Duplicate shortcut key: %s" % i.shortcut) self.keymap[i.shortcut] = i def keypress(self, size, key): -- cgit v1.2.3