ratatui_toolkit/services/file_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::file_watcher::{FileWatcher, WatchConfig};
8
9impl FileWatcher {
10 /// Create a new file 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::file_watcher::{FileWatcher, WatchConfig, WatchMode};
24 ///
25 /// let config = WatchConfig::new()
26 /// .mode(WatchMode::Recursive)
27 /// .debounce_ms(200);
28 ///
29 /// let watcher = FileWatcher::with_config(config).unwrap();
30 /// ```
31 pub fn with_config(config: WatchConfig) -> Result<Self, notify::Error> {
32 let (tx, rx) = channel();
33
34 let watcher = RecommendedWatcher::new(
35 move |res| {
36 let _ = tx.send(res);
37 },
38 Config::default().with_poll_interval(Duration::from_millis(config.debounce_ms)),
39 )?;
40
41 Ok(Self {
42 watcher,
43 rx,
44 config,
45 changed_paths: Vec::new(),
46 })
47 }
48}