Skip to content

Commit

Permalink
add ad-hoc task support to "start"
Browse files Browse the repository at this point in the history
  • Loading branch information
vighneshiyer committed Apr 6, 2024
1 parent ec5aab8 commit 141b643
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
16 changes: 12 additions & 4 deletions today/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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",
)
Expand All @@ -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,
)


Expand Down
29 changes: 17 additions & 12 deletions today/scripts/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " <span weight='bold'>/</span> ".join(task.path)
# current_task = f"<span weight='bold'> Current Task ({cli_args.task_id}) -</span>" if False else ""
rel_path = task.file_path.relative_to(cli_args.task_dir)
task_snippet = f"<span color='white'> {path} <span weight='bold' color='red'>→</span> {task.title} <span color='lightgray'>({rel_path}:{task.line_number})</span></span>"
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"<span color='white' weight='bold'>Ad-hoc task:</span> <span color='lightgrey'>{cli_args.task_id}</span>"
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 = " <span weight='bold'>/</span> ".join(task.path)
# current_task = f"<span weight='bold'> Current Task ({cli_args.task_id}) -</span>" if False else ""
rel_path = task.file_path.relative_to(cli_args.task_dir)
task_snippet = f"<span color='white'> {path} <span weight='bold' color='red'>→</span> {task.title} <span color='lightgray'>({rel_path}:{task.line_number})</span></span>"
Path("/tmp/task").write_text(task_snippet)

# https://i3wm.org/docs/i3status.html
Expand Down
1 change: 1 addition & 0 deletions today/scripts/today.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 141b643

Please sign in to comment.