From 8e187c9790be755ecda8780853748ecb5d64c757 Mon Sep 17 00:00:00 2001 From: Yehuda Deutsch Date: Mon, 11 Nov 2024 16:39:47 -0500 Subject: [PATCH] Small improvements --- todo.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/todo.py b/todo.py index fa26f2a..03b802a 100755 --- a/todo.py +++ b/todo.py @@ -8,11 +8,12 @@ todo_file = os.path.join(base_dir, "todo.md") def add_task(task: str, parent=None): - print(f"Adding task '{task}' to '{parent if parent else 'root'}'") todo = parse_todo() if parent and parent not in todo: print(f"Could not find parent task '{parent}' in root task list") exit(1) + + print(f"Adding task '{task}' to '{parent if parent else 'root'}'") if parent: todo[parent]["sub_tasks"][task] = {"completed": False} else: @@ -21,18 +22,18 @@ def add_task(task: str, parent=None): def list_tasks(parent=None): - print(f"Listing tasks in '{parent if parent else 'root'}'") todo = parse_todo() if parent and parent not in todo: print(f"Could not find parent task '{parent}' in root task list") exit(1) + + print(f"Listing tasks in '{parent if parent else 'root'}'") if parent: todo = {parent: todo[parent]} print(render_todo(todo)) def complete_task(task, parent=None): - print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed") todo = parse_todo() if parent: if parent not in todo: @@ -44,6 +45,8 @@ def complete_task(task, parent=None): if task not in todo: print(f"Could not find task '{task}' in root task list") exit(1) + + print(f"Marking task '{task}' in '{parent if parent else 'root'}' as completed") if parent: task = todo[parent]["sub_tasks"][task] else: @@ -53,7 +56,6 @@ def complete_task(task, parent=None): def remove_task(task, parent=None): - print(f"Removing task '{task}' from '{parent if parent else 'root'}'") todo = parse_todo() if parent: if parent not in todo: @@ -65,6 +67,8 @@ def remove_task(task, parent=None): if task not in todo: print(f"Could not find task '{task}' in root task list") exit(1) + + print(f"Removing task '{task}' from '{parent if parent else 'root'}'") if parent: del todo[parent]["sub_tasks"][task] else: @@ -85,14 +89,14 @@ def parse_todo() -> dict[str, dict]: lines = f.readlines() root_task = None for line in lines: - if matches := re.match(r"^-\s+(\[(?P[x ])]\s+)?(?P.*)$", line.rstrip()): + if matches := re.match(r"^-\s+(\[(?P[x ])]\s+)?(?P.+)$", line.rstrip()): root_task_name = matches.group("task") root_task = { "completed": matches.group("status") == "x", "sub_tasks": {}, } todo[root_task_name] = root_task - elif matches := re.match(r"^\s+-\s+(\[(?P[x ])]\s+)?(?P.*)$", line.rstrip()): + elif matches := re.match(r"^\s+-\s+(\[(?P[x ])]\s+)?(?P.+)$", line.rstrip()): leaf_task = matches.group("task") root_task["sub_tasks"][leaf_task] = { "completed": matches.group("status") == "x", @@ -125,7 +129,7 @@ def main(): print_help() case "list": match sys.argv[2:]: - case [parent]: + case [parent, *_]: list_tasks(parent) case _: list_tasks() @@ -134,7 +138,7 @@ def main(): match sys.argv[2:]: case [task]: globals()[task_func](task) - case [task, parent]: + case [parent, task, *_]: globals()[task_func](task, parent) case _: print_help()