Skip to content

feat: add --watch live-reload preview for the TUI#24

Merged
rrbe merged 3 commits into
masterfrom
feat/watch-live-preview
Jun 16, 2026
Merged

feat: add --watch live-reload preview for the TUI#24
rrbe merged 3 commits into
masterfrom
feat/watch-live-preview

Conversation

@rrbe

@rrbe rrbe commented Jun 16, 2026

Copy link
Copy Markdown
Owner

What & why

Adds termdown --watch FILE (and a watch config option) so the interactive TUI re-renders whenever the file changes on disk. Built for a two-pane workflow: edit the Markdown in your editor (e.g. vim) on one side, keep termdown --watch open on the other for a live preview.

Motivated by the "edit on the left, preview on the right" request — implemented with performance as the primary constraint.

How it works

  • Watcher (notify): watches the file's parent directory non-recursively and filters events to the target path, so editor atomic saves (write-temp-then-rename, which swaps the inode) keep firing. The watcher pings an mpsc channel that the existing 50 ms event::poll loop drains each tick (burst events from one save are coalesced into a single reload).
  • reload_active_doc: re-reads the file and rebuilds the doc in place, preserving:
    • scroll position — via a new Viewport::top_logical() anchor + the existing visual_line_for_logical()
    • ToC open/closed and metadata fold state
    • active search (the SearchState now retains its query so matches rebuild against new content)
    • A transient read error mid-rename keeps the last good render and retries on the follow-up event.
  • Heading-image cache (the performance core): a process-global cache keyed on (level, theme, text), mirroring the existing FONT_DATA_CACHE. PNG rasterization is the dominant render cost (~150–240 ms on a heading-heavy doc), so a save that only touches body text now re-renders in ~5–35 ms; only headings whose text actually changed are re-rasterized.
  • UX: a [watch] marker in the status bar; renumber_doc_images extracted from push_new_doc and reused by reload.

--watch is TUI-only — it warns and falls back to cat mode for piped/stdin input. Default off; opt in via -w/--watch or watch = true in config.

Tests

make check is green (clippy -D warnings, 119 tests). New unit tests:

  • heading cache: keyed by (level, theme, text), byte-identical on hit
  • renumber_doc_images: id remap + span/LineKind ref patching, allocator advance
  • Viewport::top_logical: round-trips with visual_line_for_logical

Manual verification (needs a Kitty-graphics terminal)

termdown --watch README.md in one pane; edit + save in another. Confirm: body-only edit reloads instantly with scroll kept; editing one heading re-rasterizes only that heading; works with both :set backupcopy=yes (in-place) and default (atomic rename); rm + recreate recovers without crashing.

🤖 Generated with Claude Code

rrbe added 3 commits June 16, 2026 17:22
Adds `termdown --watch FILE` (and `watch` config option) so the TUI
re-renders whenever the file changes on disk — built for a two-pane
"edit in your editor, preview in termdown" workflow.

- notify-based watcher on the parent directory (survives editor
  atomic save / rename), drained in the existing 50ms event-loop tick
- reload_active_doc rebuilds the doc in place, preserving scroll
  position (via top-logical anchor), ToC/metadata fold state, and any
  active search query
- process-global heading-image cache keyed on (level, theme, text):
  a body-only edit re-renders near-instantly; only changed headings
  are re-rasterized
- [watch] status-bar indicator; extract renumber_doc_images helper

Tests: heading cache keying/determinism, renumber_doc_images id remap,
viewport top_logical round-trip.
Address review findings on the --watch live-reload feature:

- Watcher now retargets on link/back/forward navigation (new LiveWatch
  with an Arc<Mutex> target shared with the callback thread), so live
  reload follows the on-screen doc instead of staying bound to the
  initial file.
- Preserve the current search match across reload via
  SearchState::rebuilt_for / nearest_match_index, rather than resetting
  focus to the top.
- Include a font fingerprint in the heading-image cache key so distinct
  font configs can't alias the same cached PNG.
reload_active_doc reads the active file via read_to_string; on
Linux/inotify that surfaces as Access(Open) (and, via atime,
Modify(Metadata)) for the target, re-triggering reload and spinning
a self-sustaining loop. Filter both kinds in the watch callback;
real content changes (Create/Modify(Data/Name/Any)/Remove and the
coarse Any/Other macOS FSEvents emits) still reload.
@rrbe rrbe merged commit afa43cc into master Jun 16, 2026
5 checks passed
@rrbe rrbe deleted the feat/watch-live-preview branch June 16, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant