watch/
watch.rs

1use std::path::Path;
2
3use calloop::EventLoop;
4use calloop_notify::NotifySource;
5use calloop_notify::notify::{RecursiveMode, Watcher};
6
7fn main() {
8    // Create calloop event loop.
9    let mut event_loop = EventLoop::try_new().unwrap();
10    let loop_handle = event_loop.handle();
11
12    // Watch current directory recursively.
13    let mut notify_source = NotifySource::new().unwrap();
14    notify_source.watch(Path::new("."), RecursiveMode::Recursive).unwrap();
15
16    // Insert notify source into calloop.
17    loop_handle
18        .insert_source(notify_source, |event, _, _| {
19            println!("Notify Event: {event:?}");
20        })
21        .unwrap();
22
23    // Dispatch event loop.
24    loop {
25        event_loop.dispatch(None, &mut ()).unwrap();
26    }
27}