save cursor position

This commit is contained in:
James Turk 2025-01-03 23:24:44 -06:00
parent 89a8afbd6a
commit 555397452c

View File

@ -10,8 +10,11 @@ from .db import initialize_db
# TODO: add footer w/ filter status
# TODO: toggle status with 't'
# TODO: add way to filter on other columns
# TODO: nicer editor for some fields
# TODO: auto edit on add
# TODO: safe DB mode
# Nice to Have
# TODO: CLI?
# TODO: config- default category, types, colors
# TODO: dropdowns for status, type, category
column_to_field = {
1: "text",
@ -89,8 +92,7 @@ class TT(App):
def __init__(self):
super().__init__()
self.search_query = ""
self.edit_row = None
self.edit_column = None
self.saved_cursor_pos = (0, 0)
def compose(self):
self.header = Header()
@ -106,7 +108,7 @@ class TT(App):
def on_mount(self):
self.table.add_columns("ID", "Task", "Status", "Type", "Due", "Category")
self.refresh_tasks()
self.refresh_tasks(restore_cursor=False)
def action_cursor_left(self):
self.table.move_cursor(column=self.table.cursor_column - 1)
@ -130,7 +132,7 @@ class TT(App):
def action_cursor_bottom(self):
self.table.move_cursor(row=self.table.row_count - 1)
def refresh_tasks(self):
def refresh_tasks(self, *, restore_cursor=True):
self.table.clear()
tasks = get_tasks(self.search_query)
@ -150,22 +152,42 @@ class TT(App):
category,
key=str(task.id),
)
if restore_cursor:
self.table.move_cursor(
row=self.saved_cursor_pos[0], column=self.saved_cursor_pos[1]
)
else:
self.table.move_cursor(row=0, column=1)
def action_delete_task(self):
if self.table.cursor_column == 0:
task_id = int(self.table.get_cell_at((self.table.cursor_row, 0)))
cur_row = self.table.cursor_row
task_id = int(self.table.get_cell_at((cur_row, 0)))
update_task(task_id, deleted=True)
self._save_cursor()
self.refresh_tasks()
def action_add_task(self):
"""Add a new task with default values."""
add_task(
new_task = add_task(
text="New Task",
type="task",
type="",
status=TaskStatus.ZERO.value,
category="main",
)
self.refresh_tasks()
self.refresh_tasks(restore_cursor=False)
self.move_cursor_to_task(new_task.id)
self.action_start_edit()
def move_cursor_to_task(self, task_id):
# ick, but only way to search table?
for row in range(len(self.table.rows)):
data = self.table.get_row_at(row)
if data[0] == str(task_id):
self.table.move_cursor(row=row, column=1)
break
else:
raise Exception("nope")
# Control of edit bar ####################
@ -189,11 +211,14 @@ class TT(App):
def action_start_search(self):
self._show_input("search", "")
def _save_cursor(self):
self.saved_cursor_pos = (self.table.cursor_row, self.table.cursor_column)
def action_start_edit(self):
if self.table.cursor_row is None or self.table.cursor_column == 0:
return
self.saved_cursor_pos = (self.table.cursor_row, self.table.cursor_column)
self._save_cursor()
current_value = self.table.get_cell_at(
(self.table.cursor_row, self.table.cursor_column)
)
@ -203,7 +228,7 @@ class TT(App):
def on_input_submitted(self, event: Input.Submitted):
if self.mode == "search":
self.search_query = event.value
self.refresh_tasks()
self.refresh_tasks(restore_cursor=False)
elif self.mode == "edit":
self.apply_change(event.value)
self._hide_input()