From 141b643f372ca2764c09fc93c9ba321486988782 Mon Sep 17 00:00:00 2001 From: Vighnesh Iyer Date: Sat, 6 Apr 2024 15:40:35 -0700 Subject: [PATCH] add ad-hoc task support to "start" --- today/cli.py | 16 ++++++++++++---- today/scripts/start.py | 29 +++++++++++++++++------------ today/scripts/today.py | 1 + 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/today/cli.py b/today/cli.py index 263d61a..529ccec 100644 --- a/today/cli.py +++ b/today/cli.py @@ -3,7 +3,7 @@ from pathlib import Path import itertools from datetime import date, timedelta -from typing import List, Optional +from typing import List, Optional, Union import functools from dataclasses import dataclass @@ -20,7 +20,7 @@ class CliArgs: task_dir: Path today: date lookahead_days: timedelta - task_id: Optional[int] + task_id: Optional[Union[int, str]] # Only display tasks that are due / have reminders up to and including this day def task_date_filter(self) -> date: @@ -50,7 +50,7 @@ def build_parser() -> argparse.ArgumentParser: ) parser.add_argument( "task_id", - type=int, + type=str, nargs="?", help="Show the description of this specific task", ) @@ -76,11 +76,19 @@ def parse_args(parser: argparse.ArgumentParser, args: List[str]) -> CliArgs: today = date(int(today_split[2]), int(today_split[0]), int(today_split[1])) else: today = date.today() + task_id: Optional[Union[int, str]] + if not ns.task_id: + task_id = None + else: + try: + task_id = int(ns.task_id) + except ValueError: + task_id = ns.task_id return CliArgs( task_dir=task_dir, lookahead_days=lookahead_days, today=today, - task_id=ns.task_id, + task_id=task_id, ) diff --git a/today/scripts/start.py b/today/scripts/start.py index bb86eb6..2aba50c 100644 --- a/today/scripts/start.py +++ b/today/scripts/start.py @@ -14,18 +14,23 @@ def run(args) -> None: task_file.write_text("") else: tasks = parse_task_files(cli_args) - if cli_args.task_id >= len(tasks): - print( - f"The task id provided ({cli_args.task_id}) is not in range, rerun today" - ) - sys.exit(1) - task = tasks[cli_args.task_id] - # path = " → ".join(task.path) - # path = " / ".join(task.path) - path = " / ".join(task.path) - # current_task = f" Current Task ({cli_args.task_id}) -" if False else "" - rel_path = task.file_path.relative_to(cli_args.task_dir) - task_snippet = f" {path} {task.title} ({rel_path}:{task.line_number})" + task_snippet: str + if isinstance(cli_args.task_id, str): + # This is an ad-hoc task that doesn't correspond to any task file, just display the string + task_snippet = f"Ad-hoc task: {cli_args.task_id}" + else: + if cli_args.task_id >= len(tasks): + print( + f"The task id provided ({cli_args.task_id}) is not in range, rerun today" + ) + sys.exit(1) + task = tasks[cli_args.task_id] + # path = " → ".join(task.path) + # path = " / ".join(task.path) + path = " / ".join(task.path) + # current_task = f" Current Task ({cli_args.task_id}) -" if False else "" + rel_path = task.file_path.relative_to(cli_args.task_dir) + task_snippet = f" {path} {task.title} ({rel_path}:{task.line_number})" Path("/tmp/task").write_text(task_snippet) # https://i3wm.org/docs/i3status.html diff --git a/today/scripts/today.py b/today/scripts/today.py index aeeaf57..252f5d1 100644 --- a/today/scripts/today.py +++ b/today/scripts/today.py @@ -21,6 +21,7 @@ def run(args) -> None: # If a specific task is displayed, the program will exit if cli_args.task_id is not None: + assert isinstance(cli_args.task_id, int) if cli_args.task_id < 0 or cli_args.task_id >= len(tasks): console.print(f"The task_id {args.task_id} does not exist") sys.exit(1)