-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.rs
More file actions
56 lines (50 loc) · 2.09 KB
/
Copy pathwatch.rs
File metadata and controls
56 lines (50 loc) · 2.09 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
use crate::analyser::Analyser;
use crate::formatter::{default, info};
use notify::event::ModifyKind;
use notify::{EventKind, RecursiveMode, Watcher, recommended_watcher};
use std::sync::mpsc::channel;
use std::time::{Duration, Instant};
pub async fn watch_for_changes(path: Option<String>) {
let dir_path = path.unwrap_or_else(|| "./definitions".to_string());
info(format!("Watching directory: {dir_path}"));
info(String::from("Press Ctrl+C to stop watching..."));
{
Analyser::new(dir_path.as_str()).report(false);
}
// Set up file watcher
let (tx, rx) = channel();
let mut watcher = recommended_watcher(tx).unwrap();
watcher
.watch(std::path::Path::new(&dir_path), RecursiveMode::Recursive)
.unwrap();
let mut last_run = Instant::now();
loop {
if let Ok(Ok(event)) = rx.recv() {
match event.kind {
EventKind::Modify(modify) => {
if let ModifyKind::Data(_) = modify
&& last_run.elapsed() > Duration::from_millis(500)
{
default(String::from(
"\n\n\n--------------------------------------------------------------------------\n\n",
));
info(String::from("Change detected! Regenerating report..."));
Analyser::new(dir_path.as_str()).report(false);
last_run = Instant::now();
}
}
EventKind::Remove(_) => {
if last_run.elapsed() > Duration::from_millis(500) {
default(String::from(
"\n\n\n--------------------------------------------------------------------------\n\n",
));
info(String::from("Change detected! Regenerating report..."));
Analyser::new(dir_path.as_str()).report(false);
last_run = Instant::now();
}
}
_ => {}
}
}
}
}