diff --git a/src/tt/tui/editor.py b/src/tt/tui/editor.py index 4be867a..4118981 100644 --- a/src/tt/tui/editor.py +++ b/src/tt/tui/editor.py @@ -1,3 +1,4 @@ +import datetime from textual.app import App from textual.widgets import ( DataTable, @@ -13,10 +14,7 @@ from ..utils import ( get_text_from_editor, ) from .keymodal import KeyModal -from .modals import ChoiceModal - -ELLIPSIS = "…" - +from .modals import ChoiceModal, DateModal, ConfirmModal class NotifyValidationError(Exception): """will notify and continue if raised""" @@ -77,6 +75,19 @@ class EnumColumnConfig(TableColumnConfig): +class DateColumnConfig(TableColumnConfig): + def preprocess(self, val): + try: + return datetime.strptime(val, "%Y-%m-%d") + except ValueError: + raise NotifyValidationError("Invalid date format. Use YYYY-MM-DD") + + def start_change(self, app, current_value): + app.push_screen(DateModal(current_value), app.apply_change) +ELLIPSIS = "…" + + + class TableEditor(App): CSS = """ #footer { @@ -148,7 +159,7 @@ class TableEditor(App): ("?", "show_keys", "show keybindings"), ] - def __init__(self, default_view="default"): + def __init__(self): super().__init__() self.filters = {} self.sort_string = "" # TODO: default sort @@ -226,7 +237,10 @@ class TableEditor(App): self.table.move_cursor(row=0, column=1) def action_delete_item(self): - if self.table.cursor_column == 0: + self.push_screen(ConfirmModal(f"delete ?"), self._delete_item_callback) + + def _delete_item_callback(self, confirm): + if confirm and self.table.cursor_column == 0: cur_row = self.table.cursor_row item_id = int(self.table.get_cell_at((cur_row, 0))) # deletable items need a delete @@ -243,6 +257,8 @@ class TableEditor(App): continue val = self.filters.get(fc.field, fc.default) if val is not None: + if "," in val: + val = val.split(",")[0] # TODO: fix hack for enums prepopulated[fc.field] = val new_item = self.add_item_callback(**prepopulated) diff --git a/src/tt/tui/modals.py b/src/tt/tui/modals.py index 5338fd0..e0c18cc 100644 --- a/src/tt/tui/modals.py +++ b/src/tt/tui/modals.py @@ -4,6 +4,34 @@ from textual.widgets import RadioSet, RadioButton, Label from .. import config from ..utils import get_color_enum +class ConfirmModal(ModalScreen): + CSS = """ + ConfirmModal { + align: center middle; + background: $primary 30%; + } + """ + + BINDINGS = [ + ("y", "confirm", "Down"), + ("n,escape", "cancel", "cancel"), + ] + + def __init__(self, message): + self.message = message + super().__init__() + + def compose(self): + yield Label(self.message) + yield Label("(y)es") + yield Label("(n)o") + + def action_confirm(self): + self.dismiss(True) + + def action_cancel(self): + self.app.pop_screen() + class ChoiceModal(ModalScreen): CSS = """ diff --git a/src/tt/tui/tasks.py b/src/tt/tui/tasks.py index 47aa41a..3103cc9 100644 --- a/src/tt/tui/tasks.py +++ b/src/tt/tui/tasks.py @@ -19,20 +19,9 @@ from .editor import ( TableEditor, TableColumnConfig, EnumColumnConfig, - NotifyValidationError, + DateColumnConfig, ELLIPSIS, ) -from .modals import DateModal - -class DateColumnConfig(TableColumnConfig): - def preprocess(self, val): - try: - return datetime.strptime(val, "%Y-%m-%d") - except ValueError: - raise NotifyValidationError("Invalid date format. Use YYYY-MM-DD") - - def start_change(self, app, current_value): - app.push_screen(DateModal(current_value), app.apply_change) class TT(TableEditor):