forked from TruFoundation/TruShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
136 lines (108 loc) · 4.29 KB
/
Copy pathtasks.py
File metadata and controls
136 lines (108 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from __future__ import annotations
from typing import Callable
import typer
from trushell.core.database import complete_todo, delete_todo, get_all_todos, insert_todo, update_todo
from trushell.core.models import Todo
def add_task(args: str) -> None:
"""Add a new task to the todo list.
Supports two formats:
addtask "task text" "category" — explicit quotes
addtask task text here — entire text is the task
"""
if not args.strip():
typer.secho('⚠️ Missing arguments. Syntax: addtask "task-name" "category"', fg=typer.colors.YELLOW)
return
# Only parse as "task" "category" when the user explicitly used quotes.
# Otherwise treat the entire args as the task text.
import shlex
stripped = args.strip()
if stripped.startswith('"') or stripped.startswith("'"):
try:
parts = shlex.split(stripped)
except ValueError:
parts = None
if parts and len(parts) >= 2:
task_text = parts[0]
category = parts[1]
else:
task_text = stripped.strip('"').strip("'")
category = "General"
else:
task_text = stripped
category = "General"
todo = Todo(task=task_text, category=category)
insert_todo(todo)
typer.secho("✅ Task added.", fg=typer.colors.GREEN)
def show_tasks(_: str) -> None:
"""Display the current todo list."""
tasks = get_all_todos()
if not tasks:
typer.echo("No tasks found.")
return
for index, task in enumerate(tasks, start=1):
status = "✅" if task.status == 2 else "❌"
typer.echo(f"{index}. {task.task} [{task.category}] {status}")
def complete_task(args: str) -> None:
"""Mark a todo item as complete."""
if not args.strip() or not args.strip().isdigit():
typer.secho("⚠️ Usage: task done <task-number>", fg=typer.colors.YELLOW)
return
index = int(args.strip()) - 1
try:
complete_todo(index)
typer.secho("✅ Task completed.", fg=typer.colors.GREEN)
except Exception as error:
typer.secho(f"❌ Task error: {error}", fg=typer.colors.RED)
def remove_task(args: str) -> None:
"""Remove a task by its numeric position."""
if not args.strip() or not args.strip().isdigit():
typer.secho("⚠️ Usage: task remove <task-number>", fg=typer.colors.YELLOW)
return
index = int(args.strip()) - 1
try:
delete_todo(index)
typer.secho("✅ Task removed.", fg=typer.colors.GREEN)
except Exception as error:
typer.secho(f"❌ Task error: {error}", fg=typer.colors.RED)
def update_task(args: str) -> None:
"""Update an existing task's text and/or category."""
parts = args.split(maxsplit=2)
if len(parts) < 2 or not parts[0].isdigit():
typer.secho('⚠️ Usage: task update <task-number> "<task>" ["<category>"]', fg=typer.colors.YELLOW)
return
index = int(parts[0]) - 1
task_text = parts[1].strip('"') if len(parts) >= 2 else None
category = parts[2].strip('"') if len(parts) == 3 else None
try:
update_todo(index, task_text, category)
typer.secho("✅ Task updated.", fg=typer.colors.GREEN)
except Exception as error:
typer.secho(f"❌ Task error: {error}", fg=typer.colors.RED)
def list_tasks(_: str) -> None:
"""Alias for show_tasks."""
show_tasks("")
def run_task_command(args: str) -> None:
"""Dispatch task subcommands from the manifest-driven task wrapper."""
subcommands: dict[str, Callable[[str], None]] = {
"add": add_task,
"show": show_tasks,
"done": complete_task,
"list": list_tasks,
"remove": remove_task,
"delete": remove_task,
"update": update_task,
}
if not args.strip():
typer.secho("⚠️ Usage: task <add|show|done|list|remove|update> [options]", fg=typer.colors.YELLOW)
return
parts = args.split(maxsplit=1)
subcmd = parts[0].lower()
subargs = parts[1] if len(parts) > 1 else ""
handler = subcommands.get(subcmd)
if handler:
try:
handler(subargs)
except Exception as error:
typer.secho(f"❌ Task error: {error}", fg=typer.colors.RED)
else:
typer.secho(f"❓ Unknown task subcommand: {subcmd}", fg=typer.colors.YELLOW)