27 lines
943 B
Rust
27 lines
943 B
Rust
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
|
use std::path::Path;
|
|
use std::sync::mpsc::channel;
|
|
use std::time::Duration;
|
|
|
|
fn watch(path: &Path) -> notify::Result<()> {
|
|
// Create a channel to receive the events.
|
|
let (tx, rx) = 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(tx, Duration::from_secs(2))?;
|
|
|
|
// Add a path to be watched. All files and directories at that path and
|
|
// below will be monitored for changes.
|
|
watcher.watch(path, RecursiveMode::NonRecursive)?;
|
|
|
|
// This is a simple loop, but you may want to use more complex logic here,
|
|
// for example to handle I/O.
|
|
loop {
|
|
match rx.recv() {
|
|
Ok(event) => println!("{:?}", event),
|
|
Err(e) => println!("watch error: {:?}", e),
|
|
}
|
|
}
|
|
}
|