Add basic cursor tree widget

This commit is contained in:
Joscha 2019-06-07 05:07:30 +00:00
parent ba2f64762f
commit a4d33847e9
3 changed files with 85 additions and 0 deletions

View file

@ -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__

View file

@ -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()

View file

@ -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()