ratatui_toolkit/services/file_watcher/methods/
get_changed_paths.rs

1//! Get paths that have changed.
2
3use std::path::PathBuf;
4
5use crate::services::file_watcher::FileWatcher;
6
7impl FileWatcher {
8    /// Get the paths that have changed since the last call.
9    ///
10    /// This returns and clears the internal list of changed paths.
11    /// Call [`check_for_changes`](Self::check_for_changes) first to
12    /// populate this list.
13    ///
14    /// # Returns
15    ///
16    /// A vector of paths that have changed.
17    ///
18    /// # Example
19    ///
20    /// ```no_run
21    /// use ratatui_toolkit::services::file_watcher::FileWatcher;
22    /// use std::path::Path;
23    ///
24    /// let mut watcher = FileWatcher::for_directory().unwrap();
25    /// watcher.watch(Path::new("./src")).unwrap();
26    ///
27    /// // In your event loop:
28    /// if watcher.check_for_changes() {
29    ///     for path in watcher.get_changed_paths() {
30    ///         println!("Changed: {}", path.display());
31    ///     }
32    /// }
33    /// ```
34    pub fn get_changed_paths(&mut self) -> Vec<PathBuf> {
35        std::mem::take(&mut self.changed_paths)
36    }
37
38    /// Peek at the changed paths without clearing them.
39    ///
40    /// Unlike [`get_changed_paths`](Self::get_changed_paths), this does
41    /// not clear the internal list.
42    ///
43    /// # Returns
44    ///
45    /// A slice of paths that have changed.
46    pub fn peek_changed_paths(&self) -> &[PathBuf] {
47        &self.changed_paths
48    }
49}