From e23b7708dda4570bb01e536e5b6b9c9e8e46224f Mon Sep 17 00:00:00 2001 From: Mecaneer23 <74385377+Mecaneer23@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:30:45 -0500 Subject: [PATCH] feat: factor get_executable_args into helpers file --- src/keyboard_input_helpers.py | 34 ++++++++++++++++++++++++++++++++++ todo.py | 31 +++++++++---------------------- 2 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 src/keyboard_input_helpers.py diff --git a/src/keyboard_input_helpers.py b/src/keyboard_input_helpers.py new file mode 100644 index 0000000..183e038 --- /dev/null +++ b/src/keyboard_input_helpers.py @@ -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 diff --git a/todo.py b/todo.py index 7c7fe3b..f459248 100755 --- a/todo.py +++ b/todo.py @@ -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, @@ -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 ) @@ -353,21 +354,6 @@ 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, @@ -375,7 +361,7 @@ def _get_main_input( 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() @@ -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