aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/console/tabs.py
blob: 953f6b12fad75bbb3ffe345302857f94340d0feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import urwid


class Tabs(urwid.WidgetWrap):
    def __init__(self, tabs, tab_offset=0):
        urwid.WidgetWrap.__init__(self, "")
        self.tab_offset = tab_offset
        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 keypress(self, size, key):
        if key in ["tab", "l"]:
            self.tab_offset = (self.tab_offset + 1) % (len(self.tabs))
            self.show()
        elif key == "h":
            self.tab_offset = (self.tab_offset - 1) % (len(self.tabs))
            self.show()
        return self._w.keypress(size, key)

    def show(self):
        headers = []
        for i in range(len(self.tabs)):
            txt = self.tabs[i][0]()
            if i == self.tab_offset:
                headers.append(self._tab(txt, "heading"))
            else:
                headers.append(self._tab(txt, "heading_inactive"))
        headers = urwid.Columns(headers, dividechars=1)
        self._w = urwid.Frame(
            body = self.tabs[self.tab_offset][1](),
            header = headers
        )
        self._w.set_focus("body")