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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ tix ls
# Show all tasks (including completed)
tix ls --all
tix ls -a

# Show sorted tasks
tix ls --sort-by [priority, text, id, created]
tix ls -s [priority, text, id, created]

tix ls --sort-by priority --sort-order [asc, desc]
tix ls -s priority -o [asc, desc]
```

#### Completing Tasks
Expand Down Expand Up @@ -437,7 +444,7 @@ Example structure:
| Command | Description | Example |
|---------|-------------|---------|
| `add` | Add a new task | `tix add "Task" -p high -t work -f file.txt -l https://example.com` |
| `ls` | List tasks | `tix ls --all` |
| `ls` | List tasks | `tix ls --all --sort-by priority --sort-order asc` |
| `done` | Complete a task | `tix done 1` |
| `done-all` | Complete multiple tasks | `tix done-all 1 2 3` |
| `rm` | Remove a task | `tix rm 1 -y` |
Expand Down
23 changes: 14 additions & 9 deletions tix/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
from rich.table import Table
from pathlib import Path
from tix.storage.json_storage import TaskStorage
# from tix.storage.context_storage import ContextStorage
# from tix.storage.context_storage import ContextStorage #ImportError
from tix.storage.history import HistoryManager
from tix.storage.backup import create_backup, list_backups, restore_from_backup
from tix.models import Task
from rich.prompt import Prompt
from rich.markdown import Markdown
from tix.utils.sort_tasks import sort_tasks
from datetime import datetime
# from .storage import storage
# from .config import CONFIG
# from .context import context_storage

console = Console()
storage = TaskStorage()
Expand Down Expand Up @@ -245,7 +241,9 @@ def add(task, priority, tag, attach, link, estimate):

@cli.command()
@click.option("--all", "-a", "show_all", is_flag=True, help="Show completed tasks too")
def ls(show_all):
@click.option("--sort-by", "-s", type=click.Choice(["priority", "created", "id", "text"]), help="Short task by")
@click.option("--sort-order", "-o", type=click.Choice(["asc", "desc"]), help="Short order")
def ls(show_all, sort_by, sort_order):
"""List all tasks"""
from tix.config import CONFIG

Expand All @@ -254,6 +252,10 @@ def ls(show_all):
if not tasks:
console.print("[dim]No tasks found. Use 'tix add' to create one![/dim]")
return

if not sort_by and sort_order:
console.print("[dim]Sort by is not given. Try add '--sort-by' or '-s'[/dim]")
return

# Get display settings from config
display_config = CONFIG.get('display', {})
Expand All @@ -280,8 +282,11 @@ def ls(show_all):
table.add_column("Created", style="dim")

count = dict()

for task in sorted(tasks, key=lambda t: (getattr(t, "completed", False), getattr(t, "id", 0))):

# Sorting
sorted_tasks = sort_tasks(tasks, sort_by, sort_order)

for task in sorted_tasks:
status = "✔" if getattr(task, "completed", False) else "○"
priority_color = priority_colors.get(getattr(task, "priority", "medium"),
{'high': 'red', 'medium': 'yellow', 'low': 'green'}[getattr(task, "priority", "medium")])
Expand Down
18 changes: 18 additions & 0 deletions tix/utils/sort_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tix.models import Task

def sort_tasks(tasks: list[Task], sort_by: str = None, sort_order:str = None) -> list[Task]:
try:
priority_order = {"low": 0, "medium": 1, "high": 2}
if sort_by:
sort_by_target = sort_by if sort_by != "created" else "created_at" # fix difference key on task model
is_reverse = sort_order == 'desc'

if sort_by == "priority":
sorted_tasks = sorted(tasks, key=lambda t: priority_order.get(getattr(t, sort_by_target, ""), ""), reverse=is_reverse)
else:
sorted_tasks = sorted(tasks, key=lambda t: getattr(t, sort_by_target, ""), reverse=is_reverse)
else:
sorted_tasks = sorted(tasks, key=lambda t: (getattr(t, "completed", False) ,getattr(t, "id", 0)))
except:
sorted_tasks = sorted(tasks, key=lambda t: (getattr(t, "completed", False), getattr(t, "id", 0)))
return sorted_tasks