From e8939b8b9fd53f49cc06f1f75c88ca9e63823b9e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 14 Jun 2017 10:01:07 +1200 Subject: [PATCH] console: keybindings tweaks - consistent sort order - preserve help on edit --- mitmproxy/tools/console/keybindings.py | 2 +- mitmproxy/tools/console/keymap.py | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mitmproxy/tools/console/keybindings.py b/mitmproxy/tools/console/keybindings.py index cbde030a6..45f5c33c8 100644 --- a/mitmproxy/tools/console/keybindings.py +++ b/mitmproxy/tools/console/keybindings.py @@ -48,8 +48,8 @@ class KeyListWalker(urwid.ListWalker): def sig_modified(self, sender): self.bindings = list(self.master.keymap.list("all")) - self._modified() self.set_focus(min(self.index, len(self.bindings) - 1)) + self._modified() def get_edit_text(self): return self.focus_obj.get_edit_text() diff --git a/mitmproxy/tools/console/keymap.py b/mitmproxy/tools/console/keymap.py index 354b3aacb..e406905d7 100644 --- a/mitmproxy/tools/console/keymap.py +++ b/mitmproxy/tools/console/keymap.py @@ -19,7 +19,7 @@ Contexts = { class Binding: def __init__(self, key, command, contexts, help): - self.key, self.command, self.contexts = key, command, contexts + self.key, self.command, self.contexts = key, command, sorted(contexts) self.help = help def keyspec(self): @@ -29,6 +29,9 @@ class Binding: """ return self.key.replace("space", " ") + def sortkey(self): + return self.key + ",".join(self.contexts) + class Keymap: def __init__(self, master): @@ -56,16 +59,16 @@ class Keymap: Add a key to the key map. """ self._check_contexts(contexts) - self.remove(key, contexts) for b in self.bindings: - if b.key == key and b.command == command: - b.contexts = list(set(b.contexts + contexts)) + if b.key == key and b.command.strip() == command.strip(): + b.contexts = sorted(list(set(b.contexts + contexts))) if help: b.help = help self.bind(b) break else: + self.remove(key, contexts) b = Binding(key=key, command=command, contexts=contexts, help=help) self.bindings.append(b) self.bind(b) @@ -107,8 +110,8 @@ class Keymap: b = [x for x in self.bindings if context in x.contexts or context == "all"] single = [x for x in b if len(x.key.split()) == 1] multi = [x for x in b if len(x.key.split()) != 1] - single.sort(key=lambda x: x.key) - multi.sort(key=lambda x: x.key) + single.sort(key=lambda x: x.sortkey()) + multi.sort(key=lambda x: x.sortkey()) return single + multi def handle(self, context: str, key: str) -> typing.Optional[str]: