Skip to content

Commit

Permalink
simplify sort menu more
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed Oct 3, 2023
1 parent 9156d14 commit a933058
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ def sort_menu(
(parent_win.getmaxyx()[1] - (len(max(lines, key=len)) + 1)) // 2,
)
win.box()
move_options = {
107: ("k", lambda cursor: cursor - 1),
106: ("j", lambda cursor: cursor + 1),
103: ("g", lambda _: 0),
71: ("G", lambda _: len(lines)),
}
cursor = 0
while True:
parent_win.refresh()
Expand All @@ -315,19 +321,17 @@ def sort_menu(
key = win.getch()
except KeyboardInterrupt:
return todos, cursor
move_options = {
107: ("k", lambda cursor: cursor - 1),
106: ("j", lambda cursor: cursor + 1),
103: ("g", lambda _: 0),
71: ("G", lambda _: len(lines)),
return_options: dict[int, tuple[str, Callable[..., tuple[list[Todo], int]]]] = {
113: ("q", lambda: (todos, int(selected))),
27: ("esc", lambda: (todos, int(selected))),
10: ("enter", lambda: sort_by(lines[cursor], todos, selected)),
}
if key in move_options:
_, func = move_options[key]
cursor = func(cursor)
elif key in (113, 27): # q | esc
return todos, cursor
elif key == 10: # enter
return sort_by(lines[cursor], todos, selected)
elif key in return_options: # q, esc, enter
_, func = return_options[key]
return func()
else:
continue
cursor = clamp(cursor, 0, len(lines))
Expand Down

0 comments on commit a933058

Please sign in to comment.