feat: add notify

This commit is contained in:
2020-10-22 21:49:04 +08:00
parent 6e8bb9e33f
commit 93ff5a519e
3 changed files with 322 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
use std::env;
use notify::{Watcher, RecursiveMode, RawEvent, raw_watcher};
use std::sync::mpsc::channel;
fn main() {
// Create a channel to receive the events.
let (tx, rx) = channel();
// Create a watcher object, delivering raw events.
// The notification back-end is selected based on the platform.
let mut watcher = raw_watcher(tx).unwrap();
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher.watch(env::var("HOME").unwrap(), RecursiveMode::Recursive).unwrap();
loop {
match rx.recv() {
Ok(RawEvent{path: Some(path), op: Ok(op), cookie}) => {
println!("{:?} {:?} ({:?})", op, path, cookie)
},
Ok(event) => println!("broken event: {:?}", event),
Err(e) => println!("watch error: {:?}", e),
}
}
}