| 336 | } |
| 337 | |
| 338 | pub fn watch(&mut self, path: &str) -> Result<(), Box<dyn std::error::Error>> { |
| 339 | let (tx, rx) = channel(); |
| 340 | let watcher_tx = tx.clone(); |
| 341 | let ignore_paths = self.ignore_paths.clone(); |
| 342 | |
| 343 | let status = self.status.clone(); |
| 344 | let handle = std::thread::spawn(move || { |
| 345 | for event in rx { |
| 346 | // Filter out events from ignored directories |
| 347 | let notify::Event { paths, .. } = &event; |
| 348 | if paths.iter().any(|p| { |
| 349 | ignore_paths.iter().any(|ignore| p.to_string_lossy().contains(ignore)) |
| 350 | }) { |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | if let Ok(mut status) = status.lock() { |
| 355 | status.in_progress = true; |
| 356 | println!("File changed: {:?}", paths); |
| 357 | } |
| 358 | } |
| 359 | }); |
| 360 | |
| 361 | let mut watcher = notify::recommended_watcher(move |res| { |
| 362 | if let Ok(event) = res { |
| 363 | let _ = tx.send(event); |
| 364 | } |
| 365 | })?; |
| 366 | |
| 367 | watcher.watch(path.as_ref(), RecursiveMode::Recursive)?; |
| 368 | self.watcher = Some(watcher); |
| 369 | self.watcher_tx = Some(watcher_tx); |
| 370 | self.file_watcher_thread = Some(handle); |
| 371 | Ok(()) |
| 372 | } |
| 373 | |
| 374 | fn cleanup(&mut self) { |
| 375 | if let Some(handle) = self.file_watcher_thread.take() { |