| use notify::{RecommendedWatcher, RecursiveMode, Watcher}; | |
| use std::path::Path; | |
| fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> { | |
| let (tx, rx) = std::sync::mpsc::channel(); | |
| // Automatically select the best implementation for your platform. | |
| // You can also access each implementation directly e.g. INotifyWatcher. | |
| let mut watcher: RecommendedWatcher = Watcher::new_immediate(move |res| tx.send(res).unwrap())?; | |
| // Add a path to be watched. All files and directories at that path and | |
| // below will be monitored for changes. | |
| watcher.watch(path, RecursiveMode::Recursive)?; | |
| for res in rx { | |
| match res { | |
| Ok(event) => println!("changed: {:?}", event), | |
| Err(e) => println!("watch error: {:?}", e), | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn main() { | |
| let path = std::env::args() | |
| .nth(1) | |
| .expect("Argument 1 needs to be a path"); | |
| println!("watching {}", path); | |
| if let Err(e) = watch(path) { | |
| println!("error: {:?}", e) | |
| } | |
| } |