From a4d33847e942ab7aea1e0660af8db18579500cc4 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 7 Jun 2019 05:07:30 +0000 Subject: [PATCH] Add basic cursor tree widget --- cheuph/__init__.py | 2 ++ cheuph/cursor_tree_widget.py | 48 +++++++++++++++++++++++++ test_scripts/test_cursor_tree_widget.py | 35 ++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 cheuph/cursor_tree_widget.py create mode 100644 test_scripts/test_cursor_tree_widget.py diff --git a/cheuph/__init__.py b/cheuph/__init__.py index 130edd9..5c12663 100644 --- a/cheuph/__init__.py +++ b/cheuph/__init__.py @@ -4,6 +4,7 @@ from .attributed_lines import * from .attributed_lines_widget import * from .attributed_text_widget import * from .cursor_rendering import * +from .cursor_tree_widget import * from .element import * from .element_supply import * from .markup import * @@ -15,6 +16,7 @@ __all__ += attributed_lines.__all__ __all__ += attributed_lines_widget.__all__ __all__ += attributed_text_widget.__all__ __all__ += cursor_rendering.__all__ +__all__ += cursor_tree_widget.__all__ __all__ += element.__all__ __all__ += element_supply.__all__ __all__ += markup.__all__ diff --git a/cheuph/cursor_tree_widget.py b/cheuph/cursor_tree_widget.py new file mode 100644 index 0000000..de4ed34 --- /dev/null +++ b/cheuph/cursor_tree_widget.py @@ -0,0 +1,48 @@ +import datetime +import time +from typing import Tuple, TypeVar + +import urwid + +from .attributed_lines_widget import AttributedLinesWidget +from .cursor_rendering import CursorTreeRenderer +from .element import Element, Message + +__all__ = ["CursorTreeWidget"] + +E = TypeVar("E", bound=Element) + +class CursorTreeWidget(urwid.WidgetWrap): + """ + This widget draws a CursorTree and moves the cursor around. + """ + + def __init__(self, tree: CursorTreeRenderer[E]) -> None: + self._tree = tree + self._lines_widget = AttributedLinesWidget() + super().__init__(self._lines_widget) + + def render(self, size: Tuple[int, int], focus: bool) -> None: + width, height = size + + self._tree.render(width, height) + self._lines_widget.set_lines(self._tree.lines) + + return super().render(size, focus) + + def selectable(self): + return True + + def keypress(self, size: Tuple[int, int], key: str): + if key == "shift up": + self._tree.scroll(-1) + elif key == "shift down": + self._tree.scroll(1) + elif key == "shift right": + self._tree.scroll_horizontally(1) + elif key == "shift left": + self._tree.scroll_horizontally(-1) + else: + t = datetime.datetime(2019,5,7,13,25,6) + self._tree._supply.add(Message(str(time.time()), None, t, "key", key)) + self._invalidate() diff --git a/test_scripts/test_cursor_tree_widget.py b/test_scripts/test_cursor_tree_widget.py new file mode 100644 index 0000000..14949eb --- /dev/null +++ b/test_scripts/test_cursor_tree_widget.py @@ -0,0 +1,35 @@ +import datetime + +import urwid +import urwid.curses_display + +import cheuph +from cheuph import (AT, BasicCursorRenderer, CursorTreeRenderer, + CursorTreeWidget, InMemorySupply, Message) + + +def add(supply, level, text, amount=4): + t = datetime.datetime(2019, 5, 7, 13, 25, 6) + if level < 0: return + for i in range(amount): + new_text = f"{text}->{i}" + supply.add(Message(new_text, text or None, t, str(i), new_text)) + add(supply, level - 1, new_text, amount=amount) + +def main(): + s = InMemorySupply() + r = BasicCursorRenderer() + t = CursorTreeRenderer(s, r) + + add(s, 4, "") + + #screen = urwid.curses_display.Screen() + event_loop = urwid.AsyncioEventLoop() + loop = urwid.MainLoop( + cheuph.CursorTreeWidget(t), + #screen=screen, + event_loop=event_loop, + ) + loop.run() + +main()