-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodocli.py
67 lines (50 loc) · 2 KB
/
todocli.py
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
import typer
from rich.console import Console
from rich.table import Table
from model import Todo
from database import get_all_todos, delete_todo, insert_todo, complete_todo, update_todo
console = Console()
app = typer.Typer()
@app.command(short_help='adds a todo')
def add(task: str, category: str):
typer.echo(f"adding {task}, {category}")
todo = Todo(task, category)
insert_todo(todo)
show()
@app.command(short_help='deletes a todo')
def delete(position: int):
typer.echo(f"deleting {position}")
# in UI begin at 1, but in database at 0
delete_todo(position-1)
show()
@app.command(short_help='update a todo')
def update(position: int,task: str=None, category: str=None):
typer.echo(f"updating {position}")
update_todo(position-1, task, category)
show()
@app.command(short_help='used is mark a todo as complete')
def complete(position: int):
typer.echo(f"complete {position}")
complete_todo(position-1)
show()
@app.command(short_help='shows all the todos')
def show():
tasks = get_all_todos()
console.print("[bold magenta]Todos[/bold magenta]!", "💻")
table = Table(show_header=True, header_style="bold blue",padding=(1,0),expand=True)
table.add_column("#", style="dim", width=6,justify="center")
table.add_column("Todo", min_width=20, justify="center")
table.add_column("Category", min_width=12, justify="center")
table.add_column("Done", min_width=12, justify="center")
def get_category_color(category):
COLORS = {'Learn Python': 'cyan', 'Learn Java': 'red', 'Learn C++': 'blue', 'Learn C': 'green'}
if category in COLORS:
return COLORS[category]
return 'white'
for idx, task in enumerate(tasks, start=1):
c = get_category_color(task.category)
is_done_str = '✅' if task.status == 1 else '❌'
table.add_row(str(idx), task.task, f'[{c}]{task.category}[/{c}]', is_done_str)
console.print(table)
if __name__ == "__main__":
app()