feat: add --watch live-reload preview for the TUI#24
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Adds
termdown --watch FILE(and awatchconfig 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, keeptermdown --watchopen 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
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 anmpscchannel that the existing 50 msevent::pollloop 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:Viewport::top_logical()anchor + the existingvisual_line_for_logical()SearchStatenow retains its query so matches rebuild against new content)(level, theme, text), mirroring the existingFONT_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.[watch]marker in the status bar;renumber_doc_imagesextracted frompush_new_docand reused by reload.--watchis TUI-only — it warns and falls back to cat mode for piped/stdin input. Default off; opt in via-w/--watchorwatch = truein config.Tests
make checkis green (clippy-D warnings, 119 tests). New unit tests:(level, theme, text), byte-identical on hitrenumber_doc_images: id remap + span/LineKindref patching, allocator advanceViewport::top_logical: round-trips withvisual_line_for_logicalManual verification (needs a Kitty-graphics terminal)
termdown --watch README.mdin 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