Skip to content

feat: add watch mode for auto-refreshing diffs#96

Open
vertti wants to merge 8 commits intodlvhdr:mainfrom
vertti:feat/watch-mode
Open

feat: add watch mode for auto-refreshing diffs#96
vertti wants to merge 8 commits intodlvhdr:mainfrom
vertti:feat/watch-mode

Conversation

@vertti
Copy link

@vertti vertti commented Mar 5, 2026

Summary

When working on a branch, it's useful to leave diffnav open in a terminal tab and have it automatically reflect the latest changes. This adds --watch mode, which periodically re-runs a diff command and refreshes the TUI.

diffnav --watch
diffnav --watch --watch-cmd "git diff main..." --watch-interval 5s

Details

  • New flags: --watch (-w), --watch-cmd (default: git diff), --watch-interval (default: 2s)
  • Watch tick/result message loop with in-flight guard to prevent overlapping fetches
  • Stderr discarded in watch command execution to avoid corrupting the TUI
  • ClearCache on diffviewer for clean re-renders on refresh
  • Active watch command shown in footer bar
  • README updated with watch mode flags and usage examples

vertti added 8 commits March 5, 2026 13:54
Add --watch/-w, --watch-cmd, and --watch-interval flags to enable
periodic diff command execution and UI refresh.
RunCmd executes a shell command and returns stdout with ANSI stripped.
Allows resetting the diff cache when input changes (needed for watch mode).
Schedule periodic ticks that re-run the watch command, compare output,
and refresh the file tree and diff viewer when changes are detected.
Preserves cursor position across refreshes. Empty diffs no longer quit
when watch mode is enabled.
In watch mode, skip stdin and run the watch command for initial data.
Allow empty initial output so the TUI starts and keeps ticking.
Displays "watching: <cmd>" in the footer when watch mode is active.
- Add watchInFlight guard to prevent overlapping fetches on slow commands
- Discard stderr in watch.RunCmd to avoid corrupting TUI display
- Use log.Warn consistently for transient watch command failures
- Add watch mode flags and usage section to README
- Add watch example to cobra Example string
@dlvhdr
Copy link
Owner

dlvhdr commented Mar 6, 2026

hmm I a bit wary of introducing this complexity. How do the alternatives look? e.g. using the watch command or entr

@vertti
Copy link
Author

vertti commented Mar 6, 2026

Totally fair concern! I looked into this before going the built-in route.

The core problem is that watch, entr, and watchexec all work by restarting the process. That means stdin gets closed and all TUI state (cursor position, selected file, scroll offset) is lost on every change. There's no way for an external tool to preserve in-memory state across restarts.

For what it's worth, every TUI app I checked builds refresh in for the same reason:

  • lazygit — polls file changes on a time.Ticker (10s default)
  • k9s — built-in refreshRate config (2s default)
  • gitui — built-in filesystem watcher with polling fallback

The implementation here follows the standard Bubble Tea tick -> fetch -> message pattern (~60 lines of actual logic), so it shouldn't add much maintenance surface.

That said, if you had a specific approach in mind that I haven't considered, happy to explore it!

@dlvhdr
Copy link
Owner

dlvhdr commented Mar 6, 2026

ok thanks for the detailed response - looks like you did your research :)
I'll go over the code in the next few days and thanks for the contrib!

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.

2 participants