console: ignore non-nav keys in chooser

Awkward, but works

Fixes #2417
This commit is contained in:
Aldo Cortesi 2017-12-20 11:44:47 +13:00
parent 4a9deb350d
commit 2fd5bbe1e7
2 changed files with 28 additions and 2 deletions

View File

@ -17,6 +17,13 @@ Contexts = {
} }
navkeys = [
"m_start", "m_end", "m_next", "m_select",
"up", "down", "page_up", "page_down",
"left", "right"
]
class Binding: class Binding:
def __init__(self, key, command, contexts, help): def __init__(self, key, command, contexts, help):
self.key, self.command, self.contexts = key, command, sorted(contexts) self.key, self.command, self.contexts = key, command, sorted(contexts)
@ -122,3 +129,13 @@ class Keymap:
if b: if b:
return self.executor(b.command) return self.executor(b.command)
return key 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

View File

@ -5,6 +5,7 @@ import urwid
from mitmproxy.tools.console import signals from mitmproxy.tools.console import signals
from mitmproxy.tools.console import grideditor from mitmproxy.tools.console import grideditor
from mitmproxy.tools.console import layoutwidget from mitmproxy.tools.console import layoutwidget
from mitmproxy.tools.console import keymap
class SimpleOverlay(urwid.Overlay, layoutwidget.LayoutWidget): class SimpleOverlay(urwid.Overlay, layoutwidget.LayoutWidget):
@ -114,13 +115,21 @@ class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
return True return True
def keypress(self, size, key): def keypress(self, size, key):
key = self.master.keymap.handle("chooser", key) key = self.master.keymap.handle_only("chooser", key)
if key == "m_select": if key == "m_select":
self.callback(self.choices[self.walker.index]) self.callback(self.choices[self.walker.index])
signals.pop_view_state.send(self) signals.pop_view_state.send(self)
return
elif key == "esc": elif key == "esc":
signals.pop_view_state.send(self) 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): class OptionsOverlay(urwid.WidgetWrap, layoutwidget.LayoutWidget):