From e7f827f1b2529ecfbb30dc89c8868aa4861ff94f Mon Sep 17 00:00:00 2001 From: mitaa Date: Thu, 19 Sep 2024 18:29:52 +0200 Subject: [PATCH] Add system commands --- src/production_planner/__init__.py | 37 ++++++++++++++++++++++++++--- src/production_planner/datatable.py | 1 + tests | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/production_planner/__init__.py b/src/production_planner/__init__.py index 0a363e4..ffb6040 100644 --- a/src/production_planner/__init__.py +++ b/src/production_planner/__init__.py @@ -29,14 +29,15 @@ from pathlib import Path import importlib.metadata import traceback +from typing import Iterable from docopt import docopt -from textual.app import App, ComposeResult +from textual.app import App, SystemCommand, ComposeResult +from textual.screen import Screen from textual.widgets import Footer from textual.reactive import reactive - __version__ = importlib.metadata.version("production_planner") @@ -44,6 +45,11 @@ # As the fuel consumption rate is directly proportional to generator power production, verify that demand matches the production capacity to ensure that Power Shards are used to their full potential. Fuel efficiency is unchanged, but consumption and power generation rates may be unexpectedly uneven[EA]. +def planner_command(title, help, callback, discover=True) -> SystemCommand: + help = " " + help + return SystemCommand(title, help, callback, discover) + + class Planner(App): CSS_PATH = "Planner.tcss" header = None @@ -60,10 +66,35 @@ def __init__(self, testrun=False, *args, **kwargs): super().__init__(*args, **kwargs) def compose(self) -> ComposeResult: - yield Header() + yield Header(icon="Menu") yield PlannerTable(header_control=True) yield Footer() + def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]: + for command in super().get_system_commands(screen): + if command.title.lower() == "light mode": + continue + yield planner_command(*command) + yield planner_command("Save As", "Save the currently active file with a new filename", self._save_as) + yield planner_command("Load", "Load a new file in the currently active table", self._load) + yield planner_command("Delete", "Delete a file from the filesystem", self._delete) + + def _save_as(self): + if not self.focused_table: + return + self.call_next(self.focused_table.action_save) + + def _load(self): + if not self.focused_table: + return + # NOTE: directly calling `action_load` somehow causes the Screen callback not to be invoked + self.call_next(self.focused_table.action_load) + + def _delete(self): + if not self.focused_table: + return + self.call_next(self.focused_table.action_delete) + def is_table_shown(self, table: PlannerTable) -> bool: raise NotImplemented diff --git a/src/production_planner/datatable.py b/src/production_planner/datatable.py index 37d5b2f..69dc8dd 100644 --- a/src/production_planner/datatable.py +++ b/src/production_planner/datatable.py @@ -77,6 +77,7 @@ class PlannerTable(DataTable): BINDINGS = [ ("+", "row_add", "Add"), + ("insert", "row_add", "Add"), ("-", "row_remove", "Remove"), ("ctrl+up", "move_up", "Move Up"), ("ctrl+down", "move_down", "Move Down"), diff --git a/tests b/tests index 311caa5..13ff77b 160000 --- a/tests +++ b/tests @@ -1 +1 @@ -Subproject commit 311caa5c2ce7d2bebe34ad3e71eecd5cbd0686f0 +Subproject commit 13ff77b98d685c7a88011a475041cecb0938c627