Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def validate_description(description):
return description.strip()


def add_task(description):
def add_task(description, use_json=False):
"""Add a new task."""
description = validate_description(description)

Expand All @@ -31,7 +31,12 @@ def add_task(description):
tasks = json.loads(tasks_file.read_text())

task_id = len(tasks) + 1
tasks.append({"id": task_id, "description": description, "done": False})
task = {"id": task_id, "description": description, "done": False}
tasks.append(task)

tasks_file.write_text(json.dumps(tasks, indent=2))
print(f"Added task {task_id}: {description}")

if use_json:
print(json.dumps({"status": "ok", "task": task}))
else:
print(f"Added task {task_id}: {description}")
17 changes: 13 additions & 4 deletions commands/done.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ def validate_task_id(tasks, task_id):
return task_id


def mark_done(task_id):
def mark_done(task_id, use_json=False):
"""Mark a task as complete."""
tasks_file = get_tasks_file()
if not tasks_file.exists():
print("No tasks found!")
if use_json:
print(json.dumps({"status": "error", "message": "No tasks found"}))
else:
print("No tasks found!")
return

tasks = json.loads(tasks_file.read_text())
Expand All @@ -31,7 +34,13 @@ def mark_done(task_id):
if task["id"] == task_id:
task["done"] = True
tasks_file.write_text(json.dumps(tasks, indent=2))
print(f"Marked task {task_id} as done: {task['description']}")
if use_json:
print(json.dumps({"status": "ok", "task": task}))
else:
print(f"Marked task {task_id} as done: {task['description']}")
return

print(f"Task {task_id} not found")
if use_json:
print(json.dumps({"status": "error", "message": f"Task {task_id} not found"}))
else:
print(f"Task {task_id} not found")
22 changes: 15 additions & 7 deletions commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ def validate_task_file():
return tasks_file


def list_tasks():
def list_tasks(use_json=False):
"""List all tasks."""
# NOTE: No --json flag support yet (feature bounty)
tasks_file = validate_task_file()
if not tasks_file:
print("No tasks yet!")
if use_json:
print(json.dumps({"status": "ok", "tasks": []}))
else:
print("No tasks yet!")
return

tasks = json.loads(tasks_file.read_text())

if not tasks:
print("No tasks yet!")
if use_json:
print(json.dumps({"status": "ok", "tasks": []}))
else:
print("No tasks yet!")
return

for task in tasks:
status = "✓" if task["done"] else " "
print(f"[{status}] {task['id']}. {task['description']}")
if use_json:
print(json.dumps({"status": "ok", "tasks": tasks}))
else:
for task in tasks:
status = "✓" if task["done"] else " "
print(f"[{status}] {task['id']}. {task['description']}")
37 changes: 31 additions & 6 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,62 @@
from commands.done import mark_done


DEFAULT_CONFIG = """# Default configuration for task CLI
storage:
format: json
max_tasks: 1000

display:
color: true
unicode: true
"""


def load_config():
"""Load configuration from file."""
config_path = Path.home() / ".config" / "task-cli" / "config.yaml"
# NOTE: This will crash if config doesn't exist - known bug for bounty testing
"""Load configuration from file, creating default if missing."""
config_dir = Path.home() / ".config" / "task-cli"
config_path = config_dir / "config.yaml"

if not config_path.exists():
config_dir.mkdir(parents=True, exist_ok=True)
with open(config_path, "w") as f:
f.write(DEFAULT_CONFIG)
print(f"[task-cli] Created default config at {config_path}")

with open(config_path) as f:
return f.read()


def main():
parser = argparse.ArgumentParser(description="Simple task manager")
parser.add_argument("--json", action="store_true", help="Output as JSON")
subparsers = parser.add_subparsers(dest="command", help="Command to run")

# Add command
add_parser = subparsers.add_parser("add", help="Add a new task")
add_parser.add_argument("description", help="Task description")
add_parser.add_argument("--json", action="store_true", help="Output as JSON")

# List command
list_parser = subparsers.add_parser("list", help="List all tasks")
list_parser.add_argument("--json", action="store_true", help="Output as JSON")

# Done command
done_parser = subparsers.add_parser("done", help="Mark task as complete")
done_parser.add_argument("task_id", type=int, help="Task ID to mark done")
done_parser.add_argument("--json", action="store_true", help="Output as JSON")

args = parser.parse_args()

# Check for --json flag at global or subcommand level
use_json = getattr(args, "json", False)

if args.command == "add":
add_task(args.description)
add_task(args.description, use_json=use_json)
elif args.command == "list":
list_tasks()
list_tasks(use_json=use_json)
elif args.command == "done":
mark_done(args.task_id)
mark_done(args.task_id, use_json=use_json)
else:
parser.print_help()

Expand Down