mitmproxy/libmproxy/console/select.py

121 lines
3.2 KiB
Python
Raw Normal View History

2015-04-06 05:45:36 +00:00
import urwid
from . import common
2015-05-30 00:03:28 +00:00
2015-04-06 05:45:36 +00:00
class _OptionWidget(urwid.WidgetWrap):
2016-01-27 09:12:18 +00:00
2015-04-06 05:45:36 +00:00
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"
if shortcut:
text = common.highlight_key(
text,
shortcut,
textattr = textattr,
keyattr = keyattr
)
2015-04-06 05:45:36 +00:00
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):
2016-01-27 09:12:18 +00:00
2015-04-06 05:45:36 +00:00
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):
2015-05-30 00:03:28 +00:00
if pos >= len(self.options) - 1:
2015-04-06 05:45:36 +00:00
return None, None
2015-05-30 00:03:28 +00:00
return self.options[pos + 1].render(False), pos + 1
2015-04-06 05:45:36 +00:00
def get_prev(self, pos):
if pos <= 0:
return None, None
2015-05-30 00:03:28 +00:00
return self.options[pos - 1].render(False), pos - 1
2015-04-06 05:45:36 +00:00
class Heading:
2016-01-27 09:12:18 +00:00
2015-04-06 05:45:36 +00:00
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
2015-05-30 00:03:28 +00:00
2015-04-06 05:45:36 +00:00
class Option:
2016-01-27 09:12:18 +00:00
2015-04-06 05:45:36 +00:00
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):
2015-05-30 00:03:28 +00:00
return _OptionWidget(
self,
self.text,
self.shortcut,
self.getstate(),
focus)
2015-04-06 05:45:36 +00:00
class Select(urwid.ListBox):
2016-01-27 09:12:18 +00:00
2015-04-06 05:45:36 +00:00
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") and i.shortcut:
2015-04-06 05:45:36 +00:00
if i.shortcut in self.keymap:
2015-05-30 00:03:28 +00:00
raise ValueError("Duplicate shortcut key: %s" % i.shortcut)
2015-04-06 05:45:36 +00:00
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)