feat!: Add progress indicator

closes #360

BREAKING CHANGE: verify() has a new function signature so it can
know the current completion progress
This commit is contained in:
Ryan Lowe 2022-02-05 16:54:11 -05:00
parent cd2b5e8e3b
commit 9be012dda0
2 changed files with 24 additions and 14 deletions

View File

@ -214,7 +214,7 @@ fn main() {
} }
Subcommands::Verify(_subargs) => { Subcommands::Verify(_subargs) => {
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1)); verify(&exercises, (0, exercises.len()), verbose).unwrap_or_else(|_| std::process::exit(1));
} }
Subcommands::Watch(_subargs) => match watch(&exercises, verbose) { Subcommands::Watch(_subargs) => match watch(&exercises, verbose) {
@ -351,7 +351,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
clear_screen(); clear_screen();
let to_owned_hint = |t: &Exercise| t.hint.to_owned(); let to_owned_hint = |t: &Exercise| t.hint.to_owned();
let failed_exercise_hint = match verify(exercises.iter(), verbose) { let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose) {
Ok(_) => return Ok(WatchStatus::Finished), Ok(_) => return Ok(WatchStatus::Finished),
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))), Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
}; };
@ -362,17 +362,16 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
DebouncedEvent::Create(b) | DebouncedEvent::Chmod(b) | DebouncedEvent::Write(b) => { DebouncedEvent::Create(b) | DebouncedEvent::Chmod(b) | DebouncedEvent::Write(b) => {
if b.extension() == Some(OsStr::new("rs")) && b.exists() { if b.extension() == Some(OsStr::new("rs")) && b.exists() {
let filepath = b.as_path().canonicalize().unwrap(); let filepath = b.as_path().canonicalize().unwrap();
let pending_exercises = exercises let pending_exercises = exercises.iter()
.iter() .find(|e| filepath.ends_with(&e.path)).into_iter()
.skip_while(|e| !filepath.ends_with(&e.path))
// .filter(|e| filepath.ends_with(&e.path))
.chain( .chain(
exercises exercises
.iter() .iter()
.filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)), .filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)),
); );
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
clear_screen(); clear_screen();
match verify(pending_exercises, verbose) { match verify(pending_exercises, (num_done, exercises.len()), verbose) {
Ok(_) => return Ok(WatchStatus::Finished), Ok(_) => return Ok(WatchStatus::Finished),
Err(exercise) => { Err(exercise) => {
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap(); let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();

View File

@ -1,6 +1,6 @@
use crate::exercise::{CompiledExercise, Exercise, Mode, State}; use crate::exercise::{CompiledExercise, Exercise, Mode, State};
use console::style; use console::style;
use indicatif::ProgressBar; use indicatif::{ProgressBar, ProgressStyle};
use std::env; use std::env;
// Verify that the provided container of Exercise objects // Verify that the provided container of Exercise objects
@ -9,10 +9,18 @@ use std::env;
// If the Exercise being verified is a test, the verbose boolean // If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed. // determines whether or not the test harness outputs are displayed.
pub fn verify<'a>( pub fn verify<'a>(
start_at: impl IntoIterator<Item = &'a Exercise>, exercises: impl IntoIterator<Item = &'a Exercise>,
progress: (usize, usize),
verbose: bool, verbose: bool,
) -> Result<(), &'a Exercise> { ) -> Result<(), &'a Exercise> {
for exercise in start_at { let (num_done, total) = progress;
let bar = ProgressBar::new(total as u64);
bar.set_style(ProgressStyle::default_bar()
.template("Progress: [{bar:60.green/red}] {pos}/{len}")
.progress_chars("#>-")
);
bar.set_position(num_done as u64);
for exercise in exercises {
let compile_result = match exercise.mode { let compile_result = match exercise.mode {
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose), Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose),
Mode::Compile => compile_and_run_interactively(exercise), Mode::Compile => compile_and_run_interactively(exercise),
@ -21,6 +29,7 @@ pub fn verify<'a>(
if !compile_result.unwrap_or(false) { if !compile_result.unwrap_or(false) {
return Err(exercise); return Err(exercise);
} }
bar.inc(1);
} }
Ok(()) Ok(())
} }
@ -45,7 +54,6 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
let _ = compile(exercise, &progress_bar)?; let _ = compile(exercise, &progress_bar)?;
progress_bar.finish_and_clear(); progress_bar.finish_and_clear();
success!("Successfully compiled {}!", exercise);
Ok(prompt_for_completion(exercise, None)) Ok(prompt_for_completion(exercise, None))
} }
@ -71,8 +79,6 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
} }
}; };
success!("Successfully ran {}!", exercise);
Ok(prompt_for_completion(exercise, Some(output.stdout))) Ok(prompt_for_completion(exercise, Some(output.stdout)))
} }
@ -92,7 +98,6 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
if verbose { if verbose {
println!("{}", output.stdout); println!("{}", output.stdout);
} }
success!("Successfully tested {}", &exercise);
if let RunMode::Interactive = run_mode { if let RunMode::Interactive = run_mode {
Ok(prompt_for_completion(exercise, None)) Ok(prompt_for_completion(exercise, None))
} else { } else {
@ -138,6 +143,12 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
State::Pending(context) => context, State::Pending(context) => context,
}; };
match exercise.mode {
Mode::Compile => success!("Successfully ran {}!", exercise),
Mode::Test => success!("Successfully tested {}!", exercise),
Mode::Clippy => success!("Successfully compiled {}!", exercise),
}
let no_emoji = env::var("NO_EMOJI").is_ok(); let no_emoji = env::var("NO_EMOJI").is_ok();
let clippy_success_msg = if no_emoji { let clippy_success_msg = if no_emoji {