1use std::path::Path;
9use std::sync::mpsc;
10
11use console::style;
12use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
13
14use crate::config::Config as AcpConfig;
15use crate::error::Result;
16
17pub struct FileWatcher {
19 _config: AcpConfig,
20}
21
22impl FileWatcher {
23 pub fn new(config: AcpConfig) -> Self {
24 Self { _config: config }
25 }
26
27 pub fn watch<P: AsRef<Path>>(&self, root: P) -> Result<()> {
29 let (tx, rx) = mpsc::channel();
30
31 let mut watcher = RecommendedWatcher::new(tx, Config::default())
32 .map_err(|e| crate::error::AcpError::Other(e.to_string()))?;
33
34 watcher
35 .watch(root.as_ref(), RecursiveMode::Recursive)
36 .map_err(|e| crate::error::AcpError::Other(e.to_string()))?;
37
38 println!("Watching for changes...");
39
40 loop {
41 match rx.recv() {
42 Ok(event) => {
43 match event {
44 Ok(event) => {
45 println!("Change detected: {:?}", event);
46 }
48 Err(e) => {
49 eprintln!("{} Watch error: {}", style("✗").red(), e);
50 }
51 }
52 }
53 Err(e) => {
54 eprintln!("{} Channel error: {}", style("✗").red(), e);
55 break;
56 }
57 }
58 }
59
60 Ok(())
61 }
62}