From fa1534e0839fcfdee5c0953969271ac3b38fb65a Mon Sep 17 00:00:00 2001 From: jpt Date: Fri, 11 Apr 2025 20:06:54 -0500 Subject: [PATCH] field loading dynamic based on toml too --- src/tt/tui/editor.py | 15 +++++++++++++++ src/tt/tui/tasks.py | 45 ++++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/tt/tui/editor.py b/src/tt/tui/editor.py index a6e5ad7..81543b2 100644 --- a/src/tt/tui/editor.py +++ b/src/tt/tui/editor.py @@ -13,6 +13,8 @@ from ..utils import ( remove_rich_tag, filter_to_string, get_text_from_editor, + get_color_enum, + get_colored_date, ) from .keymodal import KeyModal from .modals import ChoiceModal, DateModal, ConfirmModal @@ -50,6 +52,12 @@ class TableColumnConfig: # default edit mode app._show_input("edit", current_value) + def format_for_display(self, val): + val = str(val) + if "\n" in val: + val = val.split("\n")[0] + ELLIPSIS + return val + class EnumColumnConfig(TableColumnConfig): def __init__(self, field: str, display_name: str, enum, **kwargs): @@ -62,6 +70,10 @@ class EnumColumnConfig(TableColumnConfig): else: raise NotifyValidationError(f"Invalid value {val}. Use: {list(self.enum)}") + def format_for_display(self, val): + return get_color_enum(val, self.enum, "red") + + def start_change(self, app, current_value): # a weird hack? pass app here and correct modal gets pushed app.push_screen(ChoiceModal(self.enum, current_value), app.apply_change) @@ -74,6 +86,9 @@ class DateColumnConfig(TableColumnConfig): except ValueError: raise NotifyValidationError("Invalid date format. Use YYYY-MM-DD") + def format_for_display(self, val): + return get_colored_date(val) + def start_change(self, app, current_value): app.push_screen(DateModal(current_value), app.apply_change) diff --git a/src/tt/tui/tasks.py b/src/tt/tui/tasks.py index 0564213..35832ea 100644 --- a/src/tt/tui/tasks.py +++ b/src/tt/tui/tasks.py @@ -1,7 +1,6 @@ import json from textual.widgets import Input -from .. import config from ..controller.tasks import ( get_task, get_tasks, @@ -10,14 +9,7 @@ from ..controller.tasks import ( save_view, get_saved_view, ) -from ..utils import ( - get_color_enum, - get_colored_date, -) -from .editor import ( - TableEditor, - ELLIPSIS, -) +from .editor import TableEditor class TT(TableEditor): @@ -60,8 +52,21 @@ class TT(TableEditor): self._load_view(event.value) self.refresh_tasks(restore_cursor=False) self._hide_input() + # if event isn't handled here it will bubble to parent event.prevent_default() - # if event isn't handled here it will bubble to parent + + def _db_item_to_row(self, item): + """ + convert db item to an item + """ + row = [] + + for col in self.table_config: + val = getattr(item, col.field) + display_val = col.format_for_display(val) + row.append(display_val) + + return row def refresh_items(self): items = get_tasks( @@ -73,26 +78,8 @@ class TT(TableEditor): sort=self.sort_string, ) for item in items: - category = get_color_enum( - item.category.name if item.category else " - ", - config.PROJECTS, - "grey" - ) - status = get_color_enum(item.status, config.STATUSES, "red") - due = get_colored_date(item.due) - - if "\n" in item.text: - text = item.text.split("\n")[0] + ELLIPSIS - else: - text = item.text - self.table.add_row( - str(item.id), - text, - status, - item.type, - due, - category, + *self._db_item_to_row(item), key=str(item.id), )