ratatui_toolkit/services/git_watcher/mod.rs
1//! Git watching service for detecting git repository changes.
2//!
3//! A reusable, configurable git watcher that monitors the `.git` directory
4//! for changes and can be used to trigger updates only when git state changes.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use ratatui_toolkit::services::git_watcher::GitWatcher;
10//! use std::path::Path;
11//!
12//! // Create a watcher for a git repository
13//! let mut watcher = GitWatcher::new().unwrap();
14//! watcher.watch(Path::new("/path/to/repo")).unwrap();
15//!
16//! // In your event loop:
17//! if watcher.check_for_changes() {
18//! println!("Git state changed!");
19//! }
20//! ```
21
22mod constructors;
23mod helpers;
24mod methods;
25mod traits;
26
27use notify::{Event, RecommendedWatcher};
28use std::path::PathBuf;
29use std::sync::mpsc::Receiver;
30
31pub use constructors::*;
32
33/// Configuration for the git watcher.
34#[derive(Debug, Clone)]
35pub struct GitWatchConfig {
36 /// Debounce interval in milliseconds.
37 pub debounce_ms: u64,
38}
39
40impl Default for GitWatchConfig {
41 fn default() -> Self {
42 Self { debounce_ms: 100 }
43 }
44}
45
46impl GitWatchConfig {
47 /// Create a new config with default settings.
48 pub fn new() -> Self {
49 Self::default()
50 }
51
52 /// Set the debounce interval in milliseconds.
53 pub fn debounce_ms(mut self, ms: u64) -> Self {
54 self.debounce_ms = ms;
55 self
56 }
57}
58
59/// A git watcher for detecting git repository state changes.
60///
61/// Uses the `notify` crate internally to watch the `.git` directory
62/// for changes. Provides a non-blocking interface suitable for use
63/// in TUI event loops.
64///
65/// This is useful for caching git statistics and only recomputing
66/// them when the git state actually changes, rather than polling
67/// at a fixed interval.
68pub struct GitWatcher {
69 /// The underlying file system watcher.
70 pub(crate) watcher: RecommendedWatcher,
71 /// Receiver for file change events.
72 pub(crate) rx: Receiver<Result<Event, notify::Error>>,
73 /// Configuration for the watcher.
74 pub(crate) config: GitWatchConfig,
75 /// Path to the repository root being watched.
76 pub(crate) repo_path: Option<PathBuf>,
77 /// Whether changes have been detected since last check.
78 pub(crate) has_pending_changes: bool,
79}