diff --git a/README.md b/README.md index f4644f2..8b7151c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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` | diff --git a/tix/cli.py b/tix/cli.py index 6d02994..2353065 100644 --- a/tix/cli.py +++ b/tix/cli.py @@ -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() @@ -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 @@ -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', {}) @@ -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")]) diff --git a/tix/utils/sort_tasks.py b/tix/utils/sort_tasks.py new file mode 100644 index 0000000..2ddcc32 --- /dev/null +++ b/tix/utils/sort_tasks.py @@ -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