chore(watch): update notify dependency to v6

closes #1640
This commit is contained in:
Matthias Richter
2023-11-15 23:17:40 +01:00
parent 70f472484f
commit adf7d1b975
3 changed files with 124 additions and 207 deletions

View File

@@ -4,8 +4,8 @@ use crate::run::{reset, run};
use crate::verify::verify;
use clap::{Parser, Subcommand};
use console::Emoji;
use notify::DebouncedEvent;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_mini::notify::{self, RecursiveMode};
use notify_debouncer_mini::{new_debouncer, DebouncedEventKind};
use std::ffi::OsStr;
use std::fs;
use std::io::{self, prelude::*};
@@ -331,8 +331,10 @@ fn watch(
let (tx, rx) = channel();
let should_quit = Arc::new(AtomicBool::new(false));
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(1))?;
watcher.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;
let mut debouncer = new_debouncer(Duration::from_secs(1), tx)?;
debouncer
.watcher()
.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;
clear_screen();
@@ -350,38 +352,44 @@ fn watch(
loop {
match rx.recv_timeout(Duration::from_secs(1)) {
Ok(event) => match event {
DebouncedEvent::Create(b) | DebouncedEvent::Chmod(b) | DebouncedEvent::Write(b) => {
if b.extension() == Some(OsStr::new("rs")) && b.exists() {
let filepath = b.as_path().canonicalize().unwrap();
let pending_exercises = exercises
.iter()
.find(|e| filepath.ends_with(&e.path))
.into_iter()
.chain(
Ok(events) => {
for event in events {
let event_path = event.path;
if event.kind == DebouncedEventKind::Any
&& event_path.extension() == Some(OsStr::new("rs"))
&& event_path.exists()
{
let filepath = event_path.as_path().canonicalize().unwrap();
let pending_exercises =
exercises
.iter()
.filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)),
);
let num_done = exercises
.iter()
.filter(|e| e.looks_done() && !filepath.ends_with(&e.path))
.count();
clear_screen();
match verify(
pending_exercises,
(num_done, exercises.len()),
verbose,
success_hints,
) {
Ok(_) => return Ok(WatchStatus::Finished),
Err(exercise) => {
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
*failed_exercise_hint = Some(to_owned_hint(exercise));
.find(|e| filepath.ends_with(&e.path))
.into_iter()
.chain(exercises.iter().filter(|e| {
!e.looks_done() && !filepath.ends_with(&e.path)
}));
let num_done = exercises
.iter()
.filter(|e| e.looks_done() && !filepath.ends_with(&e.path))
.count();
clear_screen();
match verify(
pending_exercises,
(num_done, exercises.len()),
verbose,
success_hints,
) {
Ok(_) => return Ok(WatchStatus::Finished),
Err(exercise) => {
let mut failed_exercise_hint =
failed_exercise_hint.lock().unwrap();
*failed_exercise_hint = Some(to_owned_hint(exercise));
}
}
}
}
}
_ => {}
Err(e) => println!("watch error: {e:?}"),
},
Err(RecvTimeoutError::Timeout) => {
// the timeout expired, just check the `should_quit` variable below then loop again