field loading dynamic based on toml too

This commit is contained in:
jpt 2025-04-11 20:06:54 -05:00
parent f8b5399f2f
commit fa1534e083
2 changed files with 31 additions and 29 deletions

View File

@ -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)

View File

@ -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),
)