console: keyboard shortcuts for options

This commit is contained in:
Aldo Cortesi 2015-04-03 14:38:04 +13:00
parent 65971f02ad
commit 57bdb89342
3 changed files with 43 additions and 15 deletions

View File

@ -37,14 +37,14 @@ def is_keypress(k):
return True return True
def highlight_key(s, k): def highlight_key(str, key, textattr="text", keyattr="key"):
l = [] l = []
parts = s.split(k, 1) parts = str.split(key, 1)
if parts[0]: if parts[0]:
l.append(("text", parts[0])) l.append((textattr, parts[0]))
l.append(("key", k)) l.append((keyattr, key))
if parts[1]: if parts[1]:
l.append(("text", parts[1])) l.append((textattr, parts[1]))
return l return l

View File

@ -6,15 +6,25 @@ help_context = None
class OptionWidget(urwid.WidgetWrap): class OptionWidget(urwid.WidgetWrap):
def __init__(self, option, text, active, focus): def __init__(self, option, text, shortcut, active, focus):
self.option = option self.option = option
opt = urwid.Text(text, align="center") textattr = "text"
keyattr = "key"
if focus and active: if focus and active:
opt = urwid.AttrWrap(opt, "option_active_selected") textattr = "option_active_selected"
elif focus: elif focus:
opt = urwid.AttrWrap(opt, "option_selected") textattr = "option_selected"
keyattr = "option_selected_key"
elif active: elif active:
opt = urwid.AttrWrap(opt, "option_active") textattr = "option_active"
text = common.highlight_key(
text,
shortcut,
textattr=textattr,
keyattr=keyattr
)
opt = urwid.Text(text, align="center")
opt = urwid.AttrWrap(opt, textattr)
opt = urwid.Padding(opt, align="center", width=("relative", 20)) opt = urwid.Padding(opt, align="center", width=("relative", 20))
urwid.WidgetWrap.__init__(self, opt) urwid.WidgetWrap.__init__(self, opt)
@ -58,24 +68,33 @@ class OptionListBox(urwid.ListBox):
self, self,
OptionWalker(options) OptionWalker(options)
) )
self.options = options
self.keymap = {}
for i in options:
self.keymap[i.shortcut] = i
def keypress(self, size, key): def keypress(self, size, key):
key = common.shortcuts(key) key = common.shortcuts(key)
if key == "enter": if key == "enter":
self.get_focus()[0].option.ativate() self.get_focus()[0].option.activate()
return None
if key in self.keymap:
self.keymap[key].activate()
self.set_focus(self.options.index(self.keymap[key]))
return None return None
return super(self.__class__, self).keypress(size, key) return super(self.__class__, self).keypress(size, key)
_neg = lambda: False _neg = lambda: False
class Option: class Option:
def __init__(self, text, getstate=None, ativate=None): def __init__(self, text, shortcut, getstate=None, activate=None):
self.text = text self.text = text
self.shortcut = shortcut
self.getstate = getstate or _neg self.getstate = getstate or _neg
self.ativate = ativate or _neg self.activate = activate or _neg
def render(self, focus): def render(self, focus):
return OptionWidget(self, self.text, self.getstate(), focus) return OptionWidget(self, self.text, self.shortcut, self.getstate(), focus)
class Options(urwid.WidgetWrap): class Options(urwid.WidgetWrap):
@ -85,11 +104,13 @@ class Options(urwid.WidgetWrap):
[ [
Option( Option(
"Anti-Cache", "Anti-Cache",
"C",
lambda: master.anticache, lambda: master.anticache,
self.toggle_anticache self.toggle_anticache
), ),
Option( Option(
"Anti-Compression", "Anti-Compression",
"o",
lambda: master.anticomp, lambda: master.anticomp,
self.toggle_anticomp self.toggle_anticomp
), ),
@ -97,6 +118,7 @@ class Options(urwid.WidgetWrap):
#Option("Ignore Patterns"), #Option("Ignore Patterns"),
Option( Option(
"Kill Extra", "Kill Extra",
"E",
lambda: master.killextra, lambda: master.killextra,
self.toggle_killextra self.toggle_killextra
), ),
@ -104,6 +126,7 @@ class Options(urwid.WidgetWrap):
#Option("Replacement Patterns"), #Option("Replacement Patterns"),
Option( Option(
"Show Host", "Show Host",
"H",
lambda: master.showhost, lambda: master.showhost,
self.toggle_showhost self.toggle_showhost
), ),
@ -112,11 +135,13 @@ class Options(urwid.WidgetWrap):
#Option("TCP Proxying"), #Option("TCP Proxying"),
Option( Option(
"No Refresh", "No Refresh",
"R",
lambda: not master.refresh_server_playback, lambda: not master.refresh_server_playback,
self.toggle_refresh_server_playback self.toggle_refresh_server_playback
), ),
Option( Option(
"No Upstream Certs", "No Upstream Certs",
"U",
lambda: master.server.config.no_upstream_cert, lambda: master.server.config.no_upstream_cert,
self.toggle_upstream_cert self.toggle_upstream_cert
), ),

View File

@ -19,6 +19,7 @@ class Palette:
# Options # Options
'option_selected', 'option_active', 'option_active_selected', 'option_selected', 'option_active', 'option_active_selected',
'option_selected_key',
# List and Connections # List and Connections
'method', 'focus', 'method', 'focus',
@ -120,6 +121,7 @@ class LowLight(Palette):
# Options # Options
option_selected = ('light gray', 'dark blue'), option_selected = ('light gray', 'dark blue'),
option_selected_key = ('dark blue,bold', 'dark blue'),
option_active = ('light red', 'default'), option_active = ('light red', 'default'),
option_active_selected = ('light red', 'dark blue'), option_active_selected = ('light red', 'dark blue'),
@ -194,6 +196,7 @@ class SolarizedLight(LowLight):
# Options # Options
option_selected = (sol_base2, sol_base02), option_selected = (sol_base2, sol_base02),
option_selected_key = (sol_blue, sol_base02),
option_active = (sol_orange, 'default'), option_active = (sol_orange, 'default'),
option_active_selected = (sol_orange, sol_base02), option_active_selected = (sol_orange, sol_base02),
@ -271,7 +274,7 @@ class SolarizedDark(LowDark):
palettes = { palettes = {
#"lowlight": LowLight(), "lowlight": LowLight(),
#"lowdark": LowDark(), #"lowdark": LowDark(),
#"light": Light(), #"light": Light(),
#"dark": Dark(), #"dark": Dark(),