Created
September 26, 2017 22:07
-
-
Save geier/740580e2a4193088964a3b4d2c5eb4af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding: utf-8 | |
# Copyright (C) 2013 Patrick Totzke <[email protected]> | |
# This file is released under the GNU GPL, version 3 or a later revision. | |
# | |
# adapted from example1.py and example3.collapse.py from the urwidtrees examples | |
from urwidtrees.tree import SimpleTree # noqa | |
from urwidtrees.decoration import CollapseIconMixin, DecoratedTree, CollapsibleIndentedTree, CollapsibleArrowTree # noqa | |
from urwidtrees.nested import NestedTree # noqa | |
from urwidtrees.widgets import TreeBox, TreeListWalker # noqa | |
import urwid # noqa | |
import re # noqa | |
palette = [ | |
('body', 'black', 'light gray'), | |
('focus', 'light gray', 'dark blue', 'standout'), | |
('bars', 'dark blue', 'light gray', ''), | |
('arrowtip', 'light blue', 'light gray', ''), | |
('connectors', 'light red', 'light gray', ''), | |
] | |
class FocusableText(urwid.WidgetWrap): | |
"""Selectable Text used for nodes in our example""" | |
def __init__(self, txt, att, att_focus): | |
t = urwid.Text(txt) | |
w = urwid.AttrMap(t, att, att_focus) | |
urwid.WidgetWrap.__init__(self, w) | |
def selectable(self): | |
return True | |
def keypress(self, size, key): | |
return key | |
@property | |
def text(self): | |
return self._wrapped_widget._original_widget.text | |
class TextlinesList(SimpleTree): | |
def __init__(self, content, attr=None, attr_focus=None): | |
""" | |
:class:`SimpleTree` that contains a list of all-level-0 Text widgets | |
for each line in content. | |
""" | |
structure = [] | |
# depending on this config setting, we either add individual lines | |
# or the complete context as focusable objects. | |
for line in content.splitlines(): | |
structure.append((FocusableText(line, attr, attr_focus), None)) | |
SimpleTree.__init__(self, structure) | |
def selectable(self): | |
return True | |
def textlineslist(content, attr, attr_focus): | |
structure = [] | |
# depending on this config setting, we either add individual lines | |
# or the complete context as focusable objects. | |
for line in content.splitlines(): | |
structure.append(FocusableText(line, attr, attr_focus)) | |
return urwid.Pile(structure) | |
def build_tree(): | |
tree1 = TextlinesList('Hello Alice,\nI am fine\nBob wrote:\n', 'body', 'focus') | |
tree2 = TextlinesList('> Hi Bob,\n> How are you?\n> Charlie wrote:\n', 'body', 'focus') | |
tree3 = TextlinesList('>> Hi Dean,\n>> How are you?\n>> Best, Charlie\n', 'body', 'focus') | |
line1 = FocusableText('hello', 'body', 'focus') | |
line2 = FocusableText('foo', 'body', 'focus') | |
last_tree = SimpleTree( | |
[(tree1, [(tree2, [(tree3, None)])])] | |
) | |
return last_tree | |
def unhandled_input(key): | |
if isinstance(key, str) and key.lower() == 'q': | |
raise urwid.ExitMainLoop() | |
if __name__ == "__main__": | |
tree = CollapsibleIndentedTree(build_tree()) | |
treebox = TreeBox(NestedTree(tree)) | |
rootwidget = urwid.AttrMap(treebox, 'body') | |
urwid.MainLoop( | |
urwid.Frame(rootwidget, footer=urwid.AttrMap(urwid.Text('Q to quit'), 'focus')), | |
palette, | |
unhandled_input=unhandled_input, | |
).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment