Add arrow-key menus and report loading status
wizard: when stdin and stdout are both TTYs, menus render inline and are driven by keys: up/down/j/k/Home/End move the bold '>' highlight, Enter accepts, Esc/Ctrl-C/q back out of cancellable menus while Ctrl-C on fixed menus still aborts. Rows redraw with erase+CR across the option block only and clear before returning so prompts stay aligned. Non-TTY stdio keeps the original numbered prompt verbatim. report: each configured source shows a transient "# Generating report: pulling <name> data..." line that is erased on completion, only when stdout is a TTY; piped output remains exactly the report. tests: cover the numbered fallback, row rendering, status suppression and show+clear parity; reformat stray test files to satisfy ruff.
This commit is contained in:
@@ -8,7 +8,13 @@ from collections.abc import Sequence
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
from weekly_activity.aggregate import build_specs, collect_specs, format_combined
|
||||
from weekly_activity.aggregate import (
|
||||
Collected,
|
||||
SourceSpec,
|
||||
build_specs,
|
||||
collect_specs,
|
||||
format_combined,
|
||||
)
|
||||
from weekly_activity.config import ConfigError, default_config_path, load_config
|
||||
from weekly_activity.model import last_week_window
|
||||
from weekly_activity.report import format_report
|
||||
@@ -128,6 +134,42 @@ def resolve_period(args: argparse.Namespace) -> tuple[datetime, datetime]:
|
||||
return last_week_window()
|
||||
|
||||
|
||||
_STATUS_PREFIX = "# Generating report"
|
||||
_STATUS_ERASE = "\x1b[2K\r"
|
||||
|
||||
|
||||
def _report_status_active() -> bool:
|
||||
"""Transient progress lines only make sense on an interactive terminal."""
|
||||
return sys.stdout.isatty()
|
||||
|
||||
|
||||
def _show_report_status(name: str) -> None:
|
||||
"""Erase-then-overwrite in place; no newline keeps updates on one row."""
|
||||
sys.stdout.write(f"{_STATUS_ERASE}{_STATUS_PREFIX}: pulling {name} data...")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def _hide_report_status() -> None:
|
||||
"""Erase the status row so nothing of it survives into the final output."""
|
||||
sys.stdout.write(_STATUS_ERASE)
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def _collect_report_sources(
|
||||
specs: list[SourceSpec], since: datetime, until: datetime
|
||||
) -> list[Collected]:
|
||||
"""Run collect_specs per source, showing a TTY-only progress line each."""
|
||||
show = _report_status_active()
|
||||
collected: list[Collected] = []
|
||||
for spec in specs:
|
||||
if show:
|
||||
_show_report_status(spec.name)
|
||||
collected.extend(collect_specs([spec], since, until))
|
||||
if show:
|
||||
_hide_report_status()
|
||||
return collected
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
command = args.command or "report"
|
||||
@@ -150,7 +192,7 @@ def main() -> None:
|
||||
if not specs:
|
||||
raise SystemExit(f"error: {path} enables no sources; run `weekly-activity config`.")
|
||||
since, until = resolve_period(args)
|
||||
collected = collect_specs(specs, since, until)
|
||||
collected = _collect_report_sources(specs, since, until)
|
||||
print(format_combined(collected, since, until))
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user