Skip to content

Commit

Permalink
TUI: test tui
Browse files Browse the repository at this point in the history
Also added a way to setup the title :)

Close pythonecuador#100
  • Loading branch information
stsewd committed Nov 15, 2020
1 parent 1e64544 commit a2d7603
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lira/tui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
9 changes: 9 additions & 0 deletions lira/tui/utils.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions lira/tui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)


Expand Down
4 changes: 3 additions & 1 deletion lira/tui/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -149,6 +149,8 @@ def push(self, widget):

def pop(self):
super().pop()
if len(self.pages) <= 1:
set_title()
self.toggle_back_button()


Expand Down
18 changes: 18 additions & 0 deletions tests/tui/test_tui.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a2d7603

Please sign in to comment.