From b5e486020e2d233f65901c8a7217bad48d378fcc Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Fri, 27 Oct 2023 01:13:08 +0100 Subject: [PATCH] Basic TUI using textual --- hamstertools/__init__.py | 7 +++++ hamstertools/app.py | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 hamstertools/app.py diff --git a/hamstertools/__init__.py b/hamstertools/__init__.py index f65890f..666b530 100755 --- a/hamstertools/__init__.py +++ b/hamstertools/__init__.py @@ -700,6 +700,13 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte output_file.close() +@cli.command() +def app(): + from .app import HamsterToolsApp + app = HamsterToolsApp(db_cursor=c) + app.run() + + @cli.command() def hamster(): click.echo('🐹') diff --git a/hamstertools/app.py b/hamstertools/app.py new file mode 100644 index 0000000..e8d6d2d --- /dev/null +++ b/hamstertools/app.py @@ -0,0 +1,59 @@ +from textual.app import App, ComposeResult +from textual.widgets import Header, Footer, DataTable + + +ROWS = [ + ("lane", "swimmer", "country", "time"), + (4, "Joseph Schooling", "Singapore", 50.39), + (2, "Michael Phelps", "United States", 51.14), + (5, "Chad le Clos", "South Africa", 51.14), + (6, "László Cseh", "Hungary", 51.14), + (3, "Li Zhuhao", "China", 51.26), + (8, "Mehdy Metella", "France", 51.58), + (7, "Tom Shields", "United States", 51.73), + (1, "Aleksandr Sadovnikov", "Russia", 51.84), + (10, "Darren Burns", "Scotland", 51.84), +] + +class HamsterToolsApp(App): + """A Textual app to manage stopwatches.""" + + BINDINGS = [ + ("q", "quit", "Quit"), + ("m", "mark", "Mark"), + ] + + def __init__(self, db_cursor): + self.db_cursor = db_cursor + super().__init__() + + def compose(self) -> ComposeResult: + """Create child widgets for the app.""" + yield Header() + yield DataTable() + yield Footer() + + def on_mount(self) -> None: + table = self.query_one(DataTable) + + sql = ''' + SELECT + categories.id, categories.name, activities.id, activities.name + FROM + activities + LEFT JOIN + categories + ON + activities.category_id = categories.id ''' + + results = self.db_cursor.execute(sql) + results = [[cell or "" for cell in row] for row in self.db_cursor.fetchall()] + + columns = table.add_columns("category id","category","activity ID","activity") + + table.add_rows(results) + table.sort(columns[1], columns[3]) + table.cursor_type = "row" + + def action_quit(self) -> None: + self.exit()