From 090132f39ea150733e089e07340e0ca06dc8d79b Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Jun 2026 18:31:00 +0200 Subject: [PATCH] feat(cli): Add interactive book selection Allows users to pick specific books for download via a new `--select` CLI flag. This provides granular control over downloads, addressing limitations for users with large libraries or limited storage by superseding automatic filtering. --- CONTEXT.md | 19 ++- ...-select-mode-interactive-book-selection.md | 123 ++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 docs/adr/0006-select-mode-interactive-book-selection.md diff --git a/CONTEXT.md b/CONTEXT.md index c56bd21..0a3488d 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -75,8 +75,8 @@ via HTTP Range header on next run. Renamed to final filename on completion. `config.yaml` at runtime. Script errors if credentials found in config.yaml. ### **Sync Run** -One execution of the script: auth → fetch library → filter undownloaded -→ download each → write history → print summary → exit. +One execution of the script: auth → fetch library → (select mode or filter undownloaded) +→ download each (sequentially or in parallel) → write history → print summary → exit. ## API Surface (Libro.fm) @@ -90,3 +90,18 @@ Endpoints: - GET /api/v10/download-manifest?isbn= — MP3 download manifest (parts[], tracks[]) - GET /api/v10/explore/audiobook_details/{isbn} — Full book details - GET /api/v10/library/{isbn}/pdf_extra_url?filename= — PDF extra download URL + +## Select Mode + +### **Select Mode** +Optional interactive book selection, activated by `--select` CLI flag (off by default). +Replaces the automatic history-filter + `--limit` pipeline stage with a user-driven TUI. +Requires a TTY; exits with error if stdout is not a terminal. +Powered by `questionary` library. + +When active, the pipeline becomes: +auth → fetch library → hide already-downloaded → open checkbox TUI +→ user picks books → confirm summary (y/N) → download(selected) → write history. + +Selection rows show: `Title — First Author [Series #]`. All books unchecked by default. +Empty selection → exit 0. All books already downloaded → same "caught up" message as non-select mode. diff --git a/docs/adr/0006-select-mode-interactive-book-selection.md b/docs/adr/0006-select-mode-interactive-book-selection.md new file mode 100644 index 0000000..53453cf --- /dev/null +++ b/docs/adr/0006-select-mode-interactive-book-selection.md @@ -0,0 +1,123 @@ +# ADR 0006: Select Mode — Interactive Book Selection + +## Status + +Accepted (2026-06-03) + +## Context + +Libro.fm-downloader downloads **all** undownloaded books from the user's library on every run. The only controls are `--limit N` (cap count) and download-history filtering (skip already-downloaded). Users with large libraries (50+ books) or limited storage want to choose specific books rather than downloading everything. + +The tool already supports two output modes: TTY (Rich progress bars) and non-TTY/cron (plain log lines). Any interactive feature must respect this duality. + +## Decision + +Add a `--select` CLI flag (off by default, CLI-only — no config.yaml option) that opens an interactive checkbox TUI before downloading. When active, select mode supersedes both the history filter and `--limit`. + +### Library: questionary + +| Library | Verdict | +|---|---| +| **questionary** | **Chosen** — mature, Rich-compatible styling, checkbox prompt with fuzzy search, most popular Python interactive CLI library | +| inquirer | Rejected — lighter but fewer features, less active maintenance | +| Custom Rich-based | Rejected — more code to write/maintain, Rich's Live is powerful but not designed for form-style interaction | + +### Non-TTY behavior + +**Hard exit with error.** Select mode requires interactivity by definition. If stdout is not a terminal (piped, cron, redirected), print `"--select requires an interactive terminal (TTY)"` and exit 1. + +Rationale: silent degradation would be surprising (user expects to pick books, gets all-or-nothing instead). A fallback to numbered-list input was considered but adds a second codepath to test and a degraded experience that most users won't encounter. + +### Row format + +`[ ] N. Title — First Author [Series #]` + +- Number for easy reference +- Title + first author (full author list available elsewhere if needed) +- Series + number when present (helps with series ordering decisions) +- Duration deliberately omitted: not available from `/api/v10/library` endpoint; fetching per-book from `/api/v10/explore/audiobook_details/{isbn}` adds N API calls and latency before the UI renders + +### Default selection state + +**All unchecked.** User must actively opt-in each book. Prevents accidental bulk downloads of large libraries. + +### Already-downloaded books + +**Hidden completely.** The selection list shows only undownloaded books. Already-downloaded books are filtered out before the TUI opens, same as today's automatic pipeline stage. If all books are downloaded, print the existing `"All caught up! No new books to download."` message and exit 0 — identical to non-select behavior. + +### Post-selection confirmation + +Always show a y/N summary after selection: + +``` +3 books selected for download: + • The Great Gatsby — F. Scott Fitzgerald + • Dune — Frank Herbert + • Project Hail Mary — Andy Weir + +Proceed? [y/N] +``` + +Prevents fat-finger mistakes on large libraries. No configurable threshold — always confirm. + +### Empty selection + +If user presses Enter without checking any boxes: print `"No books selected. Exiting."` and exit 0. Not an error — choosing nothing is valid. + +### Pipeline integration + +``` +BEFORE: auth → fetch_library → filter_downloaded → [limit] → download_all +AFTER: auth → fetch_library → [--select? → select_books() : filter_downloaded+limit] → download_all +``` + +When `--select` is on: +1. Fetch full library +2. Remove already-downloaded books (same `DownloadHistory` check) +3. Verify TTY availability → error if not +4. Convert raw dicts to `Book` objects +5. Open questionary checkbox prompt +6. On confirm: show summary, ask y/N +7. Pass selected books to existing `download_all_books()` orchestrator +8. Continue with normal progress reporting, history writing, summary + +### Code location + +New module: `librofm_downloader/selector.py` + +Single public function: +```python +def select_books(books: list[Book]) -> list[Book]: + """Open interactive TUI, return user-selected books.""" +``` + +CLI integration in `cli.py`: add `--select` argument, branch between select mode and existing auto-filter path before the download stage. + +## Consequences + +### Positive + +- Users control exactly what downloads — no more bulk downloads of unwanted books +- Works naturally with existing parallel download infrastructure (`download_all_books` accepts any book list) +- Zero changes to download pipeline, history, or progress reporting +- Clear mental model: `--select` means "I'll pick"; no flag means "auto-download everything new" + +### Negative + +- New runtime dependency: `questionary` (adds ~200KB installed, pulls in `prompt_toolkit`) +- Interactive-only feature: cannot be used in cron or piped contexts +- `--limit` becomes meaningless when `--select` is on (documented, not silently ignored) +- Test complexity: questionary's TUI requires mocking/patching for unit tests + +### Risks + +- **questionary abandonment**: mitigated by it being the most popular choice; thin wrapper around `prompt_toolkit`; could swap to custom Rich implementation if needed +- **Large libraries (100+ books)**: questionary's checkbox handles scrolling natively; row format is compact; fuzzy search helps narrow down +- **Terminal width**: rows are ~60-80 chars average; should fit 80-column terminals + +## Alternatives Considered + +1. **Numbered list + comma input**: Zero dependencies, works anywhere. Rejected — poor UX for 20+ books, no visual feedback, hard to correct mistakes. +2. **Per-book y/n prompts**: Simplest possible interactive mode. Rejected — unbearable for 30+ books ("Download this? [y/n]" × 30). +3. **Config-driven include/exclude lists**: `only_isbns: [...]` or `skip_titles: [...]` in config.yaml. Rejected — requires editing config before each run; defeats the purpose of "pick what I want right now." +4. **Web UI / separate selector tool**: Overengineered for a CLI-first tool.