mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 08:11:00 +00:00
add shortcuts for console.choose, fix #2785
This commit is contained in:
parent
6d8731e144
commit
957a630bb5
@ -40,12 +40,17 @@ class SimpleOverlay(urwid.Overlay, layoutwidget.LayoutWidget):
|
|||||||
|
|
||||||
|
|
||||||
class Choice(urwid.WidgetWrap):
|
class Choice(urwid.WidgetWrap):
|
||||||
def __init__(self, txt, focus, current):
|
def __init__(self, txt, focus, current, shortcut):
|
||||||
|
if shortcut:
|
||||||
|
selection_type = "option_selected_key" if focus else "key"
|
||||||
|
txt = [(selection_type, shortcut), ") ", txt]
|
||||||
|
else:
|
||||||
|
txt = " " + txt
|
||||||
if current:
|
if current:
|
||||||
s = "option_active_selected" if focus else "option_active"
|
s = "option_active_selected" if focus else "option_active"
|
||||||
else:
|
else:
|
||||||
s = "option_selected" if focus else "text"
|
s = "option_selected" if focus else "text"
|
||||||
return super().__init__(
|
super().__init__(
|
||||||
urwid.AttrWrap(
|
urwid.AttrWrap(
|
||||||
urwid.Padding(urwid.Text(txt)),
|
urwid.Padding(urwid.Text(txt)),
|
||||||
s,
|
s,
|
||||||
@ -60,6 +65,8 @@ class Choice(urwid.WidgetWrap):
|
|||||||
|
|
||||||
|
|
||||||
class ChooserListWalker(urwid.ListWalker):
|
class ChooserListWalker(urwid.ListWalker):
|
||||||
|
shortcuts = "123456789abcdefghijklmnoprstuvwxyz"
|
||||||
|
|
||||||
def __init__(self, choices, current):
|
def __init__(self, choices, current):
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.choices = choices
|
self.choices = choices
|
||||||
@ -67,7 +74,7 @@ class ChooserListWalker(urwid.ListWalker):
|
|||||||
|
|
||||||
def _get(self, idx, focus):
|
def _get(self, idx, focus):
|
||||||
c = self.choices[idx]
|
c = self.choices[idx]
|
||||||
return Choice(c, focus, c == self.current)
|
return Choice(c, focus, c == self.current, self.shortcuts[idx:idx + 1])
|
||||||
|
|
||||||
def set_focus(self, index):
|
def set_focus(self, index):
|
||||||
self.index = index
|
self.index = index
|
||||||
@ -87,6 +94,12 @@ class ChooserListWalker(urwid.ListWalker):
|
|||||||
return None, None
|
return None, None
|
||||||
return self._get(pos, False), pos
|
return self._get(pos, False), pos
|
||||||
|
|
||||||
|
def choice_by_shortcut(self, shortcut):
|
||||||
|
for i, choice in enumerate(self.choices):
|
||||||
|
if shortcut == self.shortcuts[i:i + 1]:
|
||||||
|
return choice
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
|
class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
|
||||||
keyctx = "chooser"
|
keyctx = "chooser"
|
||||||
@ -96,7 +109,8 @@ class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
|
|||||||
self.choices = choices
|
self.choices = choices
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
choicewidth = max([len(i) for i in choices])
|
choicewidth = max([len(i) for i in choices])
|
||||||
self.width = max(choicewidth, len(title)) + 5
|
self.width = max(choicewidth, len(title)) + 7
|
||||||
|
|
||||||
self.walker = ChooserListWalker(choices, current)
|
self.walker = ChooserListWalker(choices, current)
|
||||||
super().__init__(
|
super().__init__(
|
||||||
urwid.AttrWrap(
|
urwid.AttrWrap(
|
||||||
@ -105,7 +119,7 @@ class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
|
|||||||
urwid.ListBox(self.walker),
|
urwid.ListBox(self.walker),
|
||||||
len(choices)
|
len(choices)
|
||||||
),
|
),
|
||||||
title= title
|
title=title
|
||||||
),
|
),
|
||||||
"background"
|
"background"
|
||||||
)
|
)
|
||||||
@ -116,11 +130,16 @@ class Chooser(urwid.WidgetWrap, layoutwidget.LayoutWidget):
|
|||||||
|
|
||||||
def keypress(self, size, key):
|
def keypress(self, size, key):
|
||||||
key = self.master.keymap.handle_only("chooser", key)
|
key = self.master.keymap.handle_only("chooser", key)
|
||||||
|
choice = self.walker.choice_by_shortcut(key)
|
||||||
|
if choice:
|
||||||
|
self.callback(choice)
|
||||||
|
signals.pop_view_state.send(self)
|
||||||
|
return
|
||||||
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
|
return
|
||||||
elif key == "esc":
|
elif key in ["q", "esc"]:
|
||||||
signals.pop_view_state.send(self)
|
signals.pop_view_state.send(self)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user