Compare commits

...

2 commits

Author SHA1 Message Date
c72d0773e7
Small improvements 2024-11-12 00:26:48 -05:00
950aa8920a
Add doitlive 2024-11-12 00:26:19 -05:00
3 changed files with 51 additions and 8 deletions

8
breakitlive.sh Normal file
View file

@ -0,0 +1,8 @@
#doitlive speed: 3
#doitlive prompt: sorin
python3 todo.py add Test
python3 todo.py list Test
python3 todo.py list Test Test
python3 todo.py add test1 Test Test

31
doitlive.sh Normal file
View file

@ -0,0 +1,31 @@
#doitlive speed: 3
#doitlive prompt: sorin
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 list
python3 todo.py add Test
python3 todo.py list
python3 todo.py list 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 list Test
python3 todo.py list
python3 todo.py complete Test
python3 todo.py list
python3 todo.py remove test Test
python3 todo.py list Test
python3 todo.py list
python3 todo.py remove Test
python3 todo.py list

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()