Skip to content

Commit

Permalink
feat: factor get_executable_args into helpers file
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed Jun 11, 2024
1 parent 1c8ce5c commit e23b770
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
34 changes: 34 additions & 0 deletions src/keyboard_input_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Utility functions for handling keyboard input in curses
systems
"""

from typing import TypeVar

_T = TypeVar("_T")

def get_executable_args(
args: str,
possible_args: dict[str, _T | int],
) -> list[_T | int]:
"""
Convert `args` from a comma-space (, ) delimited string
to separate python objects provided in `possible_args`.
If `args` == None, return empty list
Example `possible_args`:
possible_args: dict[str, list | int | str] = {
"lst": lst,
"len(lst)": len(lst),
"set_str": set_str,
}
"""
params: list[_T | int] = []
for arg in args.split(", "):
if arg.isdigit():
params.append(int(arg))
continue
if arg != "None":
params.append(possible_args[arg])
return params
31 changes: 9 additions & 22 deletions todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
from src.clipboard import CLIPBOARD_EXISTS, copy_todo, paste_todo
from src.get_args import (
FILENAME,
UI_TYPE,
HEADER,
UI_TYPE,
UiType,
)
from src.get_todo import get_todo
from src.io import file_string_to_todos, read_file, update_file
from src.keyboard_input_helpers import get_executable_args
from src.keys import Key
from src.menus import (
color_menu,
Expand All @@ -44,7 +45,7 @@

# Migrate the following once Python 3.12 is more common
# type PossibleArgs = ...
PossibleArgs: TypeAlias = (
_PossibleArgs: TypeAlias = (
Todo | int | UndoRedo | SingleLineModeImpl | Cursor | curses.window | Todos
)

Expand Down Expand Up @@ -353,29 +354,14 @@ def _handle_alert(stdscr: curses.window, todos: Todos, selected: int) -> None:
alert(stdscr, todos[selected].get_display_text())


def _get_possible_todos(
func: Callable[..., Todos | None],
args: str,
possible_args: dict[str, PossibleArgs],
) -> Todos | None:
params: list[PossibleArgs] = []
for arg in args.split(", "):
if arg.isdigit():
params.append(int(arg))
continue
if arg != "None":
params.append(possible_args[arg])
return func(*params)


def _get_main_input(
stdscr: curses.window,
todos: Todos,
keys_esckeys: tuple[
dict[int, tuple[Callable[..., Todos | None], str]],
dict[int, tuple[Callable[..., Todos | None], str]],
],
possible_args: dict[str, PossibleArgs],
possible_args: dict[str, _PossibleArgs],
) -> int | Todos:
try:
key: int = stdscr.getch()
Expand All @@ -400,10 +386,11 @@ def _get_main_input(
)
return -1
func, args = keys_esckeys[1][key]
possible_todos = _get_possible_todos(
func,
args,
possible_args,
possible_todos = func(
*get_executable_args(
args,
possible_args,
)
)
if isinstance(possible_todos, Todos):
todos = possible_todos
Expand Down

0 comments on commit e23b770

Please sign in to comment.