ratatui_toolkit/services/file_watcher/methods/
watch.rs

1//! Watch a path for changes.
2
3use notify::{RecursiveMode, Watcher};
4use std::path::Path;
5
6use crate::services::file_watcher::{FileWatcher, WatchMode};
7
8impl FileWatcher {
9    /// Start watching a path for changes.
10    ///
11    /// The watch mode (recursive or non-recursive) is determined by the
12    /// configuration used when creating the watcher.
13    ///
14    /// # Arguments
15    ///
16    /// * `path` - Path to the file or directory to watch.
17    ///
18    /// # Errors
19    ///
20    /// Returns a `notify::Error` if the path cannot be watched.
21    ///
22    /// # Example
23    ///
24    /// ```no_run
25    /// use ratatui_toolkit::services::file_watcher::FileWatcher;
26    /// use std::path::Path;
27    ///
28    /// let mut watcher = FileWatcher::new().unwrap();
29    /// watcher.watch(Path::new("README.md")).unwrap();
30    /// ```
31    pub fn watch(&mut self, path: &Path) -> Result<(), notify::Error> {
32        let mode = match self.config.mode {
33            WatchMode::File => RecursiveMode::NonRecursive,
34            WatchMode::Recursive => RecursiveMode::Recursive,
35        };
36        self.watcher.watch(path, mode)
37    }
38}