-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathdebouncer_mini.rs
More file actions
60 lines (53 loc) · 2.12 KB
/
debouncer_mini.rs
File metadata and controls
60 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::{path::Path, time::Duration};
use notify::{EventKindMask, RecommendedWatcher, RecursiveMode};
use notify_debouncer_mini::{new_debouncer_opt, Config};
/// Example for debouncer mini with event filtering.
///
/// This demonstrates using Config::with_notify_config() to pass a custom notify::Config
/// that filters events at the kernel level (on Linux), reducing noise.
fn main() {
env_logger::Builder::from_env(
env_logger::Env::default().default_filter_or("debouncer_mini=trace"),
)
.init();
// emit some events by changing a file
std::thread::spawn(|| {
let path = Path::new("test.txt");
let _ = std::fs::remove_file(path);
// log::info!("running 250ms events");
for _ in 0..20 {
log::trace!("writing..");
std::fs::write(path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(250));
}
// log::debug!("waiting 20s");
std::thread::sleep(Duration::from_millis(20000));
// log::info!("running 3s events");
for _ in 0..20 {
// log::debug!("writing..");
std::fs::write(path, b"Lorem ipsum").unwrap();
std::thread::sleep(Duration::from_millis(3000));
}
});
// setup debouncer with custom event filtering
let (tx, rx) = std::sync::mpsc::channel();
// Configure debouncer with notify config that excludes access events
// CORE mask: CREATE, REMOVE, MODIFY_DATA, MODIFY_META, MODIFY_NAME
let config = Config::default()
.with_timeout(Duration::from_secs(1))
.with_notify_config(notify::Config::default().with_event_kinds(EventKindMask::CORE));
let mut debouncer = new_debouncer_opt::<_, RecommendedWatcher>(config, tx).unwrap();
debouncer
.watcher()
.watch(Path::new("."), RecursiveMode::Recursive)
.unwrap();
// print all events, non returning
for result in rx {
match result {
Ok(events) => events
.iter()
.for_each(|event| log::info!("Event {event:?}")),
Err(error) => log::info!("Error {error:?}"),
}
}
}