feat!: Add progress indicator

closes , 

BREAKING CHANGE: verify() has a new function signature so it can
know the current completion progress and how many exercises to skip
This commit is contained in:
Ryan Lowe 2020-07-31 12:28:58 -04:00
parent 66ec916b3d
commit fce23513fa
2 changed files with 25 additions and 14 deletions

View file

@ -116,7 +116,7 @@ fn main() {
}
if matches.subcommand_matches("verify").is_some() {
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
verify(&exercises, 0, verbose).unwrap_or_else(|_| std::process::exit(1));
}
if matches.subcommand_matches("watch").is_some() && watch(&exercises, verbose).is_ok() {
@ -177,7 +177,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<()> {
clear_screen();
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, 0, verbose) {
Ok(_) => return Ok(()),
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
};
@ -188,11 +188,12 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<()> {
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()
.skip_while(|e| !filepath.ends_with(&e.path));
let skip_n = match exercises.iter().position(|e| filepath.ends_with(&e.path)) {
Some(index) => index,
None => continue,
};
clear_screen();
match verify(pending_exercises, verbose) {
match verify(exercises, skip_n, verbose) {
Ok(_) => return Ok(()),
Err(exercise) => {
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();

View file

@ -1,6 +1,6 @@
use crate::exercise::{CompiledExercise, Exercise, Mode, State};
use console::style;
use indicatif::ProgressBar;
use indicatif::{ProgressBar, ProgressStyle};
// Verify that the provided container of Exercise objects
// can be compiled and run without any failures.
@ -8,10 +8,17 @@ use indicatif::ProgressBar;
// If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed.
pub fn verify<'a>(
start_at: impl IntoIterator<Item = &'a Exercise>,
verbose: bool,
exercises: &'a [Exercise],
skip_n: usize,
verbose: bool
) -> Result<(), &'a Exercise> {
for exercise in start_at {
let bar = ProgressBar::new(exercises.len() as u64);
bar.set_style(ProgressStyle::default_bar()
.template("Progress: [{bar:60.green/red}] {pos}/{len}")
.progress_chars("#>-")
);
bar.set_position(skip_n as u64);
for exercise in exercises.iter().skip(skip_n) {
let compile_result = match exercise.mode {
Mode::Test => compile_and_test(&exercise, RunMode::Interactive, verbose),
Mode::Compile => compile_and_run_interactively(&exercise),
@ -20,6 +27,7 @@ pub fn verify<'a>(
if !compile_result.unwrap_or(false) {
return Err(exercise);
}
bar.inc(1);
}
Ok(())
}
@ -44,7 +52,6 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
let _ = compile(&exercise, &progress_bar)?;
progress_bar.finish_and_clear();
success!("Successfully compiled {}!", exercise);
Ok(prompt_for_completion(&exercise, None))
}
@ -70,8 +77,6 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
}
};
success!("Successfully ran {}!", exercise);
Ok(prompt_for_completion(&exercise, Some(output.stdout)))
}
@ -91,7 +96,6 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
if verbose {
println!("{}", output.stdout);
}
success!("Successfully tested {}", &exercise);
if let RunMode::Interactive = run_mode {
Ok(prompt_for_completion(&exercise, None))
} else {
@ -137,6 +141,12 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
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 success_msg = match exercise.mode {
Mode::Compile => "The code is compiling!",
Mode::Test => "The code is compiling, and the tests pass!",