Skip to content

Commit

Permalink
feat: Show a completion message when watching
Browse files Browse the repository at this point in the history
The completion message is shown only once all exercises succeed and are
not annotated with "I AM NOT DONE." The watch command will also exit

closes #251
  • Loading branch information
codehearts committed Dec 27, 2019
1 parent 426a7bb commit d25ee55
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::exercise::{Exercise, ExerciseList};
use crate::run::run;
use crate::verify::verify;
use clap::{crate_version, App, Arg, SubCommand};
use console::Emoji;
use notify::DebouncedEvent;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::ffi::OsStr;
Expand Down Expand Up @@ -102,7 +103,21 @@ fn main() {
}

if matches.subcommand_matches("watch").is_some() {
watch(&exercises).unwrap();
if watch(&exercises).is_ok() {
println!(
"{emoji} All exercises completed! {emoji}",
emoji = Emoji("🎉", "★")
);
println!("");
println!("We hope you enjoyed learning about the various aspects of Rust!");
println!(
"If you noticed any issues, please don't hesitate to report them to our repo."
);
println!("You can also contribute your own exercises to help the greater community!");
println!("");
println!("Before reporting an issue or contributing, please read our guidelines:");
println!("https://github.com/rust-lang/rustlings/blob/master/CONTRIBUTING.md");
}
}

if matches.subcommand_name().is_none() {
Expand Down Expand Up @@ -144,10 +159,12 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
watcher.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;

clear_screen();
let verify_result = verify(exercises.iter());

let to_owned_hint = |t: &Exercise| t.hint.to_owned();
let failed_exercise_hint = Arc::new(Mutex::new(verify_result.map_err(to_owned_hint).err()));
let failed_exercise_hint = match verify(exercises.iter()) {
Ok(_) => return Ok(()),
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
};
spawn_watch_shell(&failed_exercise_hint);
loop {
match rx.recv() {
Expand All @@ -159,9 +176,13 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
.iter()
.skip_while(|e| !filepath.ends_with(&e.path));
clear_screen();
let verify_result = verify(pending_exercises);
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
*failed_exercise_hint = verify_result.map_err(to_owned_hint).err();
match verify(pending_exercises) {
Ok(_) => return Ok(()),
Err(exercise) => {
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
*failed_exercise_hint = Some(to_owned_hint(exercise));
}
}
}
}
_ => {}
Expand Down

0 comments on commit d25ee55

Please sign in to comment.