confirm dialog

This commit is contained in:
jpt 2025-04-11 07:25:16 -05:00
parent 8323323dea
commit 92cc3c5b40
3 changed files with 51 additions and 18 deletions

View File

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

View File

@ -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 = """

View File

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