From dd139b9c10706921400e0553ab13b057eb628e2e Mon Sep 17 00:00:00 2001 From: Ahmad Ghozali Date: Sun, 5 Oct 2025 11:27:52 +0700 Subject: [PATCH 1/3] feat: add sorting to tix list --- tix/cli.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tix/cli.py b/tix/cli.py index 1198a55..b447c50 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() @@ -26,7 +22,7 @@ import json -context_storage = ContextStorage() +# context_storage = ContextStorage() history = HistoryManager() @click.group(invoke_without_command=True) @@ -197,7 +193,9 @@ def add(task, priority, tag, attach, link): @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 @@ -206,6 +204,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', {}) @@ -232,8 +234,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")]) From 5f5507a92cb66075a2e0fd55a707cc5a80033d5b Mon Sep 17 00:00:00 2001 From: Ahmad Ghozali Date: Sun, 5 Oct 2025 11:28:57 +0700 Subject: [PATCH 2/3] feat: add sorting tasks utils --- tix/utils/sort_tasks.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tix/utils/sort_tasks.py 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 From b5544f7cd183fa8066c1344043b96473ca071584 Mon Sep 17 00:00:00 2001 From: Ahmad Ghozali Date: Sun, 5 Oct 2025 11:35:15 +0700 Subject: [PATCH 3/3] docs: add tix list with sorting example --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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` |