From 57bdb893425058d03b1aaf28e1c774c81a8d9403 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 3 Apr 2015 14:38:04 +1300 Subject: [PATCH] console: keyboard shortcuts for options --- libmproxy/console/common.py | 10 ++++---- libmproxy/console/options.py | 43 +++++++++++++++++++++++++++-------- libmproxy/console/palettes.py | 5 +++- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index bc8a2aad8..23d3a4a47 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -37,14 +37,14 @@ def is_keypress(k): return True -def highlight_key(s, k): +def highlight_key(str, key, textattr="text", keyattr="key"): l = [] - parts = s.split(k, 1) + parts = str.split(key, 1) if parts[0]: - l.append(("text", parts[0])) - l.append(("key", k)) + l.append((textattr, parts[0])) + l.append((keyattr, key)) if parts[1]: - l.append(("text", parts[1])) + l.append((textattr, parts[1])) return l diff --git a/libmproxy/console/options.py b/libmproxy/console/options.py index c6c4afb7f..bb6b6c095 100644 --- a/libmproxy/console/options.py +++ b/libmproxy/console/options.py @@ -6,15 +6,25 @@ help_context = None class OptionWidget(urwid.WidgetWrap): - def __init__(self, option, text, active, focus): + def __init__(self, option, text, shortcut, active, focus): self.option = option - opt = urwid.Text(text, align="center") + textattr = "text" + keyattr = "key" if focus and active: - opt = urwid.AttrWrap(opt, "option_active_selected") + textattr = "option_active_selected" elif focus: - opt = urwid.AttrWrap(opt, "option_selected") + textattr = "option_selected" + keyattr = "option_selected_key" 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)) urwid.WidgetWrap.__init__(self, opt) @@ -58,24 +68,33 @@ class OptionListBox(urwid.ListBox): self, OptionWalker(options) ) + self.options = options + self.keymap = {} + for i in options: + self.keymap[i.shortcut] = i def keypress(self, size, key): key = common.shortcuts(key) 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 super(self.__class__, self).keypress(size, key) _neg = lambda: False class Option: - def __init__(self, text, getstate=None, ativate=None): + def __init__(self, text, shortcut, getstate=None, activate=None): self.text = text + self.shortcut = shortcut self.getstate = getstate or _neg - self.ativate = ativate or _neg + self.activate = activate or _neg 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): @@ -85,11 +104,13 @@ class Options(urwid.WidgetWrap): [ Option( "Anti-Cache", + "C", lambda: master.anticache, self.toggle_anticache ), Option( "Anti-Compression", + "o", lambda: master.anticomp, self.toggle_anticomp ), @@ -97,6 +118,7 @@ class Options(urwid.WidgetWrap): #Option("Ignore Patterns"), Option( "Kill Extra", + "E", lambda: master.killextra, self.toggle_killextra ), @@ -104,6 +126,7 @@ class Options(urwid.WidgetWrap): #Option("Replacement Patterns"), Option( "Show Host", + "H", lambda: master.showhost, self.toggle_showhost ), @@ -112,11 +135,13 @@ class Options(urwid.WidgetWrap): #Option("TCP Proxying"), Option( "No Refresh", + "R", lambda: not master.refresh_server_playback, self.toggle_refresh_server_playback ), Option( "No Upstream Certs", + "U", lambda: master.server.config.no_upstream_cert, self.toggle_upstream_cert ), diff --git a/libmproxy/console/palettes.py b/libmproxy/console/palettes.py index 020863f87..98a1e0425 100644 --- a/libmproxy/console/palettes.py +++ b/libmproxy/console/palettes.py @@ -19,6 +19,7 @@ class Palette: # Options 'option_selected', 'option_active', 'option_active_selected', + 'option_selected_key', # List and Connections 'method', 'focus', @@ -120,6 +121,7 @@ class LowLight(Palette): # Options option_selected = ('light gray', 'dark blue'), + option_selected_key = ('dark blue,bold', 'dark blue'), option_active = ('light red', 'default'), option_active_selected = ('light red', 'dark blue'), @@ -194,6 +196,7 @@ class SolarizedLight(LowLight): # Options option_selected = (sol_base2, sol_base02), + option_selected_key = (sol_blue, sol_base02), option_active = (sol_orange, 'default'), option_active_selected = (sol_orange, sol_base02), @@ -271,7 +274,7 @@ class SolarizedDark(LowDark): palettes = { - #"lowlight": LowLight(), + "lowlight": LowLight(), #"lowdark": LowDark(), #"light": Light(), #"dark": Dark(),