todo/cli.py

79 lines
2.9 KiB
Python
Raw Normal View History

#!/bin/python3
import argparse
import re
from todo.TodoObject import TodoObject
from todo import Todo, Category, Task
parser = argparse.ArgumentParser("Todo.md CLI Application", description="Access your todos via the command line!")
parser.add_argument("-md", "--markdown", action="store_true", help="Show markdown.")
parser.add_argument("-cat", "--category", action="store", help="Category the command should target.")
parser.add_argument("-t", "--task", action="store", help="Target a specific task in a category.")
parser.add_argument("-i", "--info", action="store_true", help="Print information about the given object.")
parser.add_argument("-x", "--complete", action="store_true", help="Toggle a task's completed status.") # x = done, right?
todo = Todo()
args = parser.parse_args()
def print_incomplete_tasks(data: TodoObject):
if len(data.get_tasks(complete=False)) > 0:
print("\n--- Incomplete Task(s) ---")
for category in [data, *data.get_categories()]: # allows us to catch tasks in subcategories and the main data object.
if len(category.get_tasks(immediate=False, complete=False)) > 0:
print(f"\n{category}")
for task in category.get_tasks(immediate=True, complete=False):
print(task)
if task.has_children:
print(task.get_md())
data = todo # start with base todo object as input data.
# --category handler
if args.category is not None:
args.category = f".*{args.category}"
data = data.get_category(args.category)
# --task handler
if args.task is not None:
for i in data.get_tasks():
if re.match(args.task, i.text, re.IGNORECASE):
data = i
# --done handler
if args.complete:
if isinstance(data, Task):
data.toggle_complete()
print(f"Set complete status for '{data.text}' to '{data.complete}'.")
todo.write_data()
else:
print("-d is only applicable to tasks.")
## --markdown handler
if args.markdown:
print(data)
print(data.get_md())
## --info handler
if args.info:
task_count = len(data.get_tasks())
incomplete_count = len(data.get_tasks(complete=False))
if isinstance(data, Todo):
print(data)
print("Categories: ", ", ".join([x.text for x in data.get_children(obj_type=Category)]))
print(f"Tasks: \n\tTotal: {task_count}\n\tIncomplete: {incomplete_count}")
print_incomplete_tasks(data)
elif isinstance(data, Category):
print("Category:", data.text)
print(f"Tasks: \n\tTotal: {task_count}\n\tIncomplete: {incomplete_count}")
print_incomplete_tasks(data)
elif isinstance(data, Task):
print("Category: ", data.task_category().text)
print("Task:", data.text)
print("Due Date:", data.date.strftime("%b %d %Y"))
print("Complete:", data.complete)
else:
print("Don't know how we ended up here. :/")