-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_todos.py
More file actions
106 lines (87 loc) · 3.11 KB
/
Copy pathcmd_todos.py
File metadata and controls
106 lines (87 loc) · 3.11 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
"""Команда todos: просмотр задач (todo) из сессий."""
import argparse
from db import SessionError, resolve_session_id
from i18n import _
from utils import build_help_epilog, format_ts
_TODOS_EXAMPLES = [
("", "help.todos.e0"),
("<session_id>", "help.todos.e1"),
("--status pending", "help.todos.e2"),
("--json", "help.todos.e3"),
]
def register(subparsers) -> None:
p = subparsers.add_parser(
"todos",
help=_("help.cmd.todos"),
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=build_help_epilog("todos", _TODOS_EXAMPLES),
)
p.add_argument("session_id", nargs="?", help="Session ID")
p.add_argument(
"--status",
choices=["pending", "completed", "cancelled"],
help="Filter by status",
)
p.add_argument("--limit", type=int, default=30, help="Max entries")
p.add_argument("--json", action="store_true", help="JSON output")
def run(args, db) -> int:
conditions = []
params = []
if args.session_id:
try:
full_id = resolve_session_id(db, args.session_id)
except SessionError as e:
print(e.message)
return 1
conditions.append("t.session_id = ?")
params.append(full_id)
if args.status:
conditions.append("t.status = ?")
params.append(args.status)
where = " AND ".join(conditions) if conditions else "1=1"
rows = db.execute(
f"""
SELECT t.session_id, t.content, t.status, t.priority,
t.position, t.time_created, t.time_updated,
s.title as session_title
FROM todo t
JOIN session s ON s.id = t.session_id
WHERE {where}
ORDER BY t.time_created DESC
LIMIT ?
""",
(*params, args.limit),
).fetchall()
if args.json:
from formatters import print_json
data = []
for r in rows:
data.append(
{
"session_id": r["session_id"][:24],
"session_title": r["session_title"],
"content": r["content"],
"status": r["status"],
"priority": r["priority"],
"created": format_ts(r["time_created"]) if r["time_created"] else None,
}
)
print_json(data)
return 0
if not rows:
print(_("todos.none"))
return 0
status_icon = {"pending": "⏳", "completed": "✅", "cancelled": "❌"}
priority_mark = {"high": " 🔴", "medium": "", "low": " 🟢"}
print(_("todos.header", n=len(rows)))
print(f" {'─' * 55}")
for r in rows:
icon = status_icon.get(r["status"], "📝")
prio = priority_mark.get(r["priority"], "")
created = format_ts(r["time_created"], "%Y-%m-%d") if r["time_created"] else "—"
title = r["session_title"] or "—"
print(f" {icon} {r['content'][:60]}{prio}")
print(f" {_('todos.session')}: {title[:40]}")
print(f" {_('todos.status')}: {r['status']}, {_('todos.created')}: {created}")
print()
return 0