2015-03-29 04:40:43 +00:00
|
|
|
import urwid
|
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2015-03-29 04:40:43 +00:00
|
|
|
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):
|
2015-03-29 06:21:54 +00:00
|
|
|
p = urwid.Text(content, align="center")
|
|
|
|
p = urwid.Padding(p, align="center", width=("relative", 100))
|
2015-03-29 04:40:43 +00:00
|
|
|
p = urwid.AttrWrap(p, attr)
|
|
|
|
return p
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
2015-03-31 21:34:58 +00:00
|
|
|
if key in ["tab", "l"]:
|
2015-05-30 00:03:28 +00:00
|
|
|
self.tab_offset = (self.tab_offset + 1) % (len(self.tabs))
|
2015-03-29 04:40:43 +00:00
|
|
|
self.show()
|
2015-03-31 21:34:58 +00:00
|
|
|
elif key == "h":
|
2015-05-30 00:03:28 +00:00
|
|
|
self.tab_offset = (self.tab_offset - 1) % (len(self.tabs))
|
2015-03-31 21:34:58 +00:00
|
|
|
self.show()
|
2015-03-31 20:25:50 +00:00
|
|
|
return self._w.keypress(size, key)
|
2015-03-29 04:40:43 +00:00
|
|
|
|
|
|
|
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"))
|
2015-03-29 06:21:54 +00:00
|
|
|
headers = urwid.Columns(headers, dividechars=1)
|
2015-03-29 04:40:43 +00:00
|
|
|
self._w = urwid.Frame(
|
|
|
|
body = self.tabs[self.tab_offset][1](),
|
|
|
|
header = headers
|
|
|
|
)
|
2015-03-31 02:59:54 +00:00
|
|
|
self._w.set_focus("body")
|