diff --git a/mitmproxy/tools/console/keymap.py b/mitmproxy/tools/console/keymap.py index b268906c4..fbb569a40 100644 --- a/mitmproxy/tools/console/keymap.py +++ b/mitmproxy/tools/console/keymap.py @@ -17,6 +17,13 @@ Contexts = { } +navkeys = [ + "m_start", "m_end", "m_next", "m_select", + "up", "down", "page_up", "page_down", + "left", "right" +] + + class Binding: def __init__(self, key, command, contexts, help): self.key, self.command, self.contexts = key, command, sorted(contexts) @@ -122,3 +129,13 @@ class Keymap: if b: return self.executor(b.command) return key + + def handle_only(self, context: str, key: str) -> typing.Optional[str]: + """ + Like handle, but ignores global bindings. Returns the key if it has + not been handled, or None. + """ + b = self.get(context, key) + if b: + return self.executor(b.command) + return key diff --git a/mitmproxy/tools/console/overlay.py b/mitmproxy/tools/console/overlay.py index f97f23f92..55acbfddb 100644 --- a/mitmproxy/tools/console/overlay.py +++ b/mitmproxy/tools/console/overlay.py @@ -5,6 +5,7 @@ import urwid from mitmproxy.tools.console import signals from mitmproxy.tools.console import grideditor from mitmproxy.tools.console import layoutwidget +from mitmproxy.tools.console import keymap class SimpleOverlay(urwid.Overlay, layoutwidget.LayoutWidget): @@ -114,13 +115,21 @@ class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget): return True def keypress(self, size, key): - key = self.master.keymap.handle("chooser", key) + key = self.master.keymap.handle_only("chooser", key) if key == "m_select": self.callback(self.choices[self.walker.index]) signals.pop_view_state.send(self) + return elif key == "esc": signals.pop_view_state.send(self) - return super().keypress(size, key) + return + + binding = self.master.keymap.get("global", key) + # This is extremely awkward. We need a better way to match nav keys only. + if binding and binding.command.startswith("console.nav"): + self.master.keymap.handle("global", key) + elif key in keymap.navkeys: + return super().keypress(size, key) class OptionsOverlay(urwid.WidgetWrap, layoutwidget.LayoutWidget):