diff --git a/lira/tui/__init__.py b/lira/tui/__init__.py index 2275106..63c3406 100644 --- a/lira/tui/__init__.py +++ b/lira/tui/__init__.py @@ -7,7 +7,7 @@ from lira.app import LiraApp from lira.tui.themes import style, theme -from lira.tui.utils import exit_app +from lira.tui.utils import exit_app, set_title from lira.tui.widgets import BooksList from lira.tui.windows import ContentArea, SidebarMenu, StatusBar @@ -40,6 +40,14 @@ def __init__(self): padding_char="─", padding_style=theme["separator"], ) + self.app = Application( + layout=Layout(self.container), + key_bindings=self.get_key_bindings(), + mouse_support=True, + full_screen=True, + style=style, + after_render=self._ready, + ) def get_key_bindings(self): keys = KeyBindings() @@ -69,16 +77,9 @@ def _(event): def _ready(self, app): key = "__is_ready" if not hasattr(self, key): + set_title() self.status.notify("Ready!") setattr(self, key, True) def run(self): - self.app = Application( - layout=Layout(self.container), - key_bindings=self.get_key_bindings(), - mouse_support=True, - full_screen=True, - style=style, - after_render=self._ready, - ) self.app.run() diff --git a/lira/tui/utils.py b/lira/tui/utils.py index 96844d6..e9bbccc 100644 --- a/lira/tui/utils.py +++ b/lira/tui/utils.py @@ -1,6 +1,15 @@ from prompt_toolkit.application.current import get_app +from prompt_toolkit.shortcuts import set_title as set_app_title def exit_app(): """Exit the app and save any state.""" get_app().exit() + + +def set_title(title=""): + if not title: + text = "Lira" + else: + text = f"{title} - Lira" + set_app_title(text) diff --git a/lira/tui/widgets.py b/lira/tui/widgets.py index 7281839..9a091f8 100644 --- a/lira/tui/widgets.py +++ b/lira/tui/widgets.py @@ -5,6 +5,8 @@ from prompt_toolkit.layout.containers import HSplit from prompt_toolkit.widgets import Box, Button, Label +from lira.tui.utils import set_title + class List: def __init__(self, tui, parent=None, index=0): @@ -46,6 +48,7 @@ def _get_elements(self): def select(self, book, index=0): widget = BookChaptersList(self.tui, book) + set_title(book.metadata["title"]) self.tui.menu.push(widget) diff --git a/lira/tui/windows.py b/lira/tui/windows.py index 34098db..38007e6 100644 --- a/lira/tui/windows.py +++ b/lira/tui/windows.py @@ -9,7 +9,7 @@ from lira import __version__ from lira.tui.themes import theme -from lira.tui.utils import exit_app +from lira.tui.utils import exit_app, set_title class WindowContainer: @@ -149,6 +149,8 @@ def push(self, widget): def pop(self): super().pop() + if len(self.pages) <= 1: + set_title() self.toggle_back_button() diff --git a/tests/tui/test_tui.py b/tests/tui/test_tui.py new file mode 100644 index 0000000..7280213 --- /dev/null +++ b/tests/tui/test_tui.py @@ -0,0 +1,18 @@ +from prompt_toolkit.application import create_app_session +from prompt_toolkit.input import create_pipe_input +from prompt_toolkit.output import DummyOutput + +from lira.tui import TerminalUI + + +class TestTUI: + def test_layout(self): + input = create_pipe_input() + with create_app_session(input=input, output=DummyOutput()): + tui = TerminalUI() + layout = tui.app.layout + assert len(list(layout.find_all_windows())) > 1 + + children = layout.container.get_children() + assert len(children) == 2 + assert len(children[0].get_children()) == 2