Update tui.py
This commit is contained in:
+25
-7
@@ -22,14 +22,20 @@ from src.persistence.lore import update_lore
|
|||||||
|
|
||||||
|
|
||||||
class EditModal(ModalScreen):
|
class EditModal(ModalScreen):
|
||||||
def __init__(self, initial_text: str, on_save: callable):
|
def __init__(self, initial_text: str, initial_type: str, initial_target: str, on_save: callable):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.initial_text = initial_text
|
self.initial_text = initial_text
|
||||||
|
self.initial_type = initial_type
|
||||||
|
self.initial_target = initial_target
|
||||||
self.on_save = on_save
|
self.on_save = on_save
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
with Vertical(id="modal-container"):
|
with Vertical(id="modal-container"):
|
||||||
yield Label("Edit Fact Content:")
|
yield Label("Type:")
|
||||||
|
yield Input(value=self.initial_type, id="edit-type")
|
||||||
|
yield Label("Target:")
|
||||||
|
yield Input(value=self.initial_target, id="edit-target")
|
||||||
|
yield Label("Content:")
|
||||||
yield Input(value=self.initial_text, id="edit-input")
|
yield Input(value=self.initial_text, id="edit-input")
|
||||||
with Horizontal(id="modal-actions"):
|
with Horizontal(id="modal-actions"):
|
||||||
yield Button("Save", id="btn-save")
|
yield Button("Save", id="btn-save")
|
||||||
@@ -38,7 +44,9 @@ class EditModal(ModalScreen):
|
|||||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||||
if event.button.id == "btn-save":
|
if event.button.id == "btn-save":
|
||||||
edit_input = self.query_one("#edit-input", Input)
|
edit_input = self.query_one("#edit-input", Input)
|
||||||
self.on_save(edit_input.value)
|
type_input = self.query_one("#edit-type", Input)
|
||||||
|
target_input = self.query_one("#edit-target", Input)
|
||||||
|
self.on_save(edit_input.value, type_input.value, target_input.value)
|
||||||
self.dismiss()
|
self.dismiss()
|
||||||
elif event.button.id == "btn-cancel":
|
elif event.button.id == "btn-cancel":
|
||||||
self.dismiss()
|
self.dismiss()
|
||||||
@@ -109,7 +117,7 @@ class ConfirmationApp(App):
|
|||||||
align: right middle;
|
align: right middle;
|
||||||
}
|
}
|
||||||
|
|
||||||
#edit-input {
|
#edit-input, #edit-type, #edit-target {
|
||||||
margin: 1 0;
|
margin: 1 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +207,7 @@ class ConfirmationApp(App):
|
|||||||
table = self.query_one("#pending-facts-table", DataTable)
|
table = self.query_one("#pending-facts-table", DataTable)
|
||||||
if isinstance(update, LoreUpdate):
|
if isinstance(update, LoreUpdate):
|
||||||
table.add_row(
|
table.add_row(
|
||||||
"Lore", update.entity_name or "General", update.content, key=str(index)
|
update.category, update.entity_name or "General", update.content, key=str(index)
|
||||||
)
|
)
|
||||||
elif isinstance(update, CharacterStateUpdate):
|
elif isinstance(update, CharacterStateUpdate):
|
||||||
change_text = f"HP: {update.hp_change or 0}"
|
change_text = f"HP: {update.hp_change or 0}"
|
||||||
@@ -292,24 +300,34 @@ class ConfirmationApp(App):
|
|||||||
|
|
||||||
update = self.pending_updates[row_index]
|
update = self.pending_updates[row_index]
|
||||||
initial_text = ""
|
initial_text = ""
|
||||||
|
initial_type = ""
|
||||||
|
initial_target = ""
|
||||||
|
|
||||||
if isinstance(update, LoreUpdate):
|
if isinstance(update, LoreUpdate):
|
||||||
initial_text = update.content
|
initial_text = update.content
|
||||||
|
initial_type = update.category
|
||||||
|
initial_target = update.entity_name or ""
|
||||||
elif isinstance(update, CharacterStateUpdate):
|
elif isinstance(update, CharacterStateUpdate):
|
||||||
initial_text = str(update.hp_change or 0)
|
initial_text = str(update.hp_change or 0)
|
||||||
|
initial_type = "Char"
|
||||||
|
initial_target = update.character_name
|
||||||
|
|
||||||
def save_callback(new_text: str):
|
def save_callback(new_text: str, new_type: str, new_target: str):
|
||||||
if isinstance(update, LoreUpdate):
|
if isinstance(update, LoreUpdate):
|
||||||
update.content = new_text
|
update.content = new_text
|
||||||
|
update.category = new_type
|
||||||
|
update.entity_name = new_target if new_target else None
|
||||||
elif isinstance(update, CharacterStateUpdate):
|
elif isinstance(update, CharacterStateUpdate):
|
||||||
try:
|
try:
|
||||||
update.hp_change = int(new_text)
|
update.hp_change = int(new_text)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
update.character_name = new_target
|
||||||
|
|
||||||
# Update the table
|
# Update the table
|
||||||
self.refresh_table()
|
self.refresh_table()
|
||||||
|
|
||||||
self.push_screen(EditModal(initial_text, save_callback))
|
self.push_screen(EditModal(initial_text, initial_type, initial_target, save_callback))
|
||||||
|
|
||||||
def remove_update(self, index: int) -> None:
|
def remove_update(self, index: int) -> None:
|
||||||
del self.pending_updates[index]
|
del self.pending_updates[index]
|
||||||
|
|||||||
Reference in New Issue
Block a user