ratatui_toolkit/services/git_watcher/constructors/with_config.rs
1//! Constructor with custom configuration.
2
3use notify::{Config, RecommendedWatcher, Watcher};
4use std::sync::mpsc::channel;
5use std::time::Duration;
6
7use crate::services::git_watcher::{GitWatchConfig, GitWatcher};
8
9impl GitWatcher {
10 /// Create a new git watcher with custom configuration.
11 ///
12 /// # Arguments
13 ///
14 /// * `config` - Configuration for the watcher.
15 ///
16 /// # Errors
17 ///
18 /// Returns a `notify::Error` if the watcher cannot be created.
19 ///
20 /// # Example
21 ///
22 /// ```no_run
23 /// use ratatui_toolkit::services::git_watcher::{GitWatcher, GitWatchConfig};
24 ///
25 /// let config = GitWatchConfig::new()
26 /// .debounce_ms(200);
27 ///
28 /// let watcher = GitWatcher::with_config(config).unwrap();
29 /// ```
30 pub fn with_config(config: GitWatchConfig) -> Result<Self, notify::Error> {
31 let (tx, rx) = channel();
32
33 let watcher = RecommendedWatcher::new(
34 move |res| {
35 let _ = tx.send(res);
36 },
37 Config::default().with_poll_interval(Duration::from_millis(config.debounce_ms)),
38 )?;
39
40 Ok(Self {
41 watcher,
42 rx,
43 config,
44 repo_path: None,
45 has_pending_changes: false,
46 })
47 }
48}