mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-01 15:55:28 +00:00
console: click-enable tabs
This commit is contained in:
parent
57a61ae8fd
commit
0b8cddddf5
@ -2,6 +2,24 @@ import urwid
|
||||
import signals
|
||||
|
||||
|
||||
class Tab(urwid.WidgetWrap):
|
||||
def __init__(self, offset, content, attr, onclick):
|
||||
"""
|
||||
onclick is called on click with the tab offset as argument
|
||||
"""
|
||||
p = urwid.Text(content, align="center")
|
||||
p = urwid.Padding(p, align="center", width=("relative", 100))
|
||||
p = urwid.AttrWrap(p, attr)
|
||||
urwid.WidgetWrap.__init__(self, p)
|
||||
self.offset = offset
|
||||
self.onclick = onclick
|
||||
|
||||
def mouse_event(self, size, event, button, col, row, focus):
|
||||
if event == "mouse press" and button == 1:
|
||||
self.onclick(self.offset)
|
||||
return True
|
||||
|
||||
|
||||
class Tabs(urwid.WidgetWrap):
|
||||
def __init__(self, tabs, tab_offset=0):
|
||||
urwid.WidgetWrap.__init__(self, "")
|
||||
@ -9,19 +27,16 @@ class Tabs(urwid.WidgetWrap):
|
||||
self.tabs = tabs
|
||||
self.show()
|
||||
|
||||
def _tab(self, content, attr):
|
||||
p = urwid.Text(content, align="center")
|
||||
p = urwid.Padding(p, align="center", width=("relative", 100))
|
||||
p = urwid.AttrWrap(p, attr)
|
||||
return p
|
||||
def change_tab(self, offset):
|
||||
self.tab_offset = offset
|
||||
self.show()
|
||||
|
||||
def keypress(self, size, key):
|
||||
n = len(self.tabs)
|
||||
if key in ["tab", "l"]:
|
||||
self.tab_offset = (self.tab_offset + 1) % (len(self.tabs))
|
||||
self.show()
|
||||
self.change_tab((self.tab_offset + 1) % n)
|
||||
elif key == "h":
|
||||
self.tab_offset = (self.tab_offset - 1) % (len(self.tabs))
|
||||
self.show()
|
||||
self.change_tab((self.tab_offset - 1) % n)
|
||||
return self._w.keypress(size, key)
|
||||
|
||||
def show(self):
|
||||
@ -29,9 +44,23 @@ class Tabs(urwid.WidgetWrap):
|
||||
for i in range(len(self.tabs)):
|
||||
txt = self.tabs[i][0]()
|
||||
if i == self.tab_offset:
|
||||
headers.append(self._tab(txt, "heading"))
|
||||
headers.append(
|
||||
Tab(
|
||||
i,
|
||||
txt,
|
||||
"heading",
|
||||
self.change_tab
|
||||
)
|
||||
)
|
||||
else:
|
||||
headers.append(self._tab(txt, "heading_inactive"))
|
||||
headers.append(
|
||||
Tab(
|
||||
i,
|
||||
txt,
|
||||
"heading_inactive",
|
||||
self.change_tab
|
||||
)
|
||||
)
|
||||
headers = urwid.Columns(headers, dividechars=1)
|
||||
self._w = urwid.Frame(
|
||||
body = self.tabs[self.tab_offset][1](),
|
||||
|
Loading…
Reference in New Issue
Block a user