use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
use notify::{recommended_watcher, Event, RecursiveMode, Watcher};
pub fn watch_file(path: PathBuf, changed: Arc<Mutex<bool>>) {
thread::spawn(move || {
let changed_inner = changed.clone();
let mut watcher = recommended_watcher(move |res: notify::Result<Event>| {
if let Ok(event) = res {
use notify::EventKind;
match event.kind {
EventKind::Modify(_) | EventKind::Create(_) => {
if let Ok(mut flag) = changed_inner.lock() {
*flag = true;
}
}
_ => {}
}
}
});
match watcher {
Ok(ref mut w) => {
if let Err(e) = w.watch(Path::new(&path), RecursiveMode::NonRecursive) {
eprintln!("[crepuscularity-dev] Failed to watch {:?}: {}", path, e);
return;
}
loop {
thread::sleep(std::time::Duration::from_secs(3600));
}
}
Err(e) => {
eprintln!("[crepuscularity-dev] Could not create watcher: {}", e);
}
}
});
}