Compare commits

...

3 commits

Author SHA1 Message Date
9bb2e2f8f1
Update doitlive 2024-11-12 00:40:28 -05:00
1a0461eded
Small improvements 2024-11-12 00:40:27 -05:00
3021ab1cf0
Fix doitlive echo 2024-11-12 00:39:49 -05:00
3 changed files with 21 additions and 16 deletions

View file

@ -5,4 +5,7 @@ python3 todo.py add Test
python3 todo.py list Test
python3 todo.py list Test Test
python3 todo.py add test1 Test Test
python3 todo.py add Test test1 test2
python3 todo.py add Test test1 test3
python3 todo.py complete Test test1 test3
python3 todo.py remove Test test1 test3

View file

@ -4,27 +4,25 @@
python3 todo.py --help
python3 todo.py help
python3 todo.py -h
python3 todo.py h
echo $?
python3 todo.py asdf
echo $?
python3 todo.py h; echo $?
python3 todo.py asdf; echo $?
python3 todo.py list
python3 todo.py add Test
python3 todo.py list
python3 todo.py list Test
python3 todo.py add test Test
python3 todo.py add Test test
python3 todo.py list Test
python3 todo.py list
python3 todo.py complete test Test
python3 todo.py complete Test test
python3 todo.py list Test
python3 todo.py list
python3 todo.py complete Test
python3 todo.py list
python3 todo.py remove test Test
python3 todo.py remove Test test
python3 todo.py list Test
python3 todo.py list
python3 todo.py remove Test

20
todo.py
View file

@ -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<status>[x ])]\s+)?(?P<task>.*)$", line.rstrip()):
if matches := re.match(r"^-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.+)$", 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<status>[x ])]\s+)?(?P<task>.*)$", line.rstrip()):
elif matches := re.match(r"^\s+-\s+(\[(?P<status>[x ])]\s+)?(?P<task>.+)$", 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()