Skip to content

Commit 9be012d

Browse files
committed
feat!: Add progress indicator
closes #360 BREAKING CHANGE: verify() has a new function signature so it can know the current completion progress
1 parent cd2b5e8 commit 9be012d

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn main() {
214214
}
215215

216216
Subcommands::Verify(_subargs) => {
217-
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
217+
verify(&exercises, (0, exercises.len()), verbose).unwrap_or_else(|_| std::process::exit(1));
218218
}
219219

220220
Subcommands::Watch(_subargs) => match watch(&exercises, verbose) {
@@ -351,7 +351,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
351351
clear_screen();
352352

353353
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
354-
let failed_exercise_hint = match verify(exercises.iter(), verbose) {
354+
let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose) {
355355
Ok(_) => return Ok(WatchStatus::Finished),
356356
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
357357
};
@@ -362,17 +362,16 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
362362
DebouncedEvent::Create(b) | DebouncedEvent::Chmod(b) | DebouncedEvent::Write(b) => {
363363
if b.extension() == Some(OsStr::new("rs")) && b.exists() {
364364
let filepath = b.as_path().canonicalize().unwrap();
365-
let pending_exercises = exercises
366-
.iter()
367-
.skip_while(|e| !filepath.ends_with(&e.path))
368-
// .filter(|e| filepath.ends_with(&e.path))
365+
let pending_exercises = exercises.iter()
366+
.find(|e| filepath.ends_with(&e.path)).into_iter()
369367
.chain(
370368
exercises
371369
.iter()
372370
.filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)),
373371
);
372+
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
374373
clear_screen();
375-
match verify(pending_exercises, verbose) {
374+
match verify(pending_exercises, (num_done, exercises.len()), verbose) {
376375
Ok(_) => return Ok(WatchStatus::Finished),
377376
Err(exercise) => {
378377
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();

src/verify.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::exercise::{CompiledExercise, Exercise, Mode, State};
22
use console::style;
3-
use indicatif::ProgressBar;
3+
use indicatif::{ProgressBar, ProgressStyle};
44
use std::env;
55

66
// Verify that the provided container of Exercise objects
@@ -9,10 +9,18 @@ use std::env;
99
// If the Exercise being verified is a test, the verbose boolean
1010
// determines whether or not the test harness outputs are displayed.
1111
pub fn verify<'a>(
12-
start_at: impl IntoIterator<Item = &'a Exercise>,
12+
exercises: impl IntoIterator<Item = &'a Exercise>,
13+
progress: (usize, usize),
1314
verbose: bool,
1415
) -> Result<(), &'a Exercise> {
15-
for exercise in start_at {
16+
let (num_done, total) = progress;
17+
let bar = ProgressBar::new(total as u64);
18+
bar.set_style(ProgressStyle::default_bar()
19+
.template("Progress: [{bar:60.green/red}] {pos}/{len}")
20+
.progress_chars("#>-")
21+
);
22+
bar.set_position(num_done as u64);
23+
for exercise in exercises {
1624
let compile_result = match exercise.mode {
1725
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose),
1826
Mode::Compile => compile_and_run_interactively(exercise),
@@ -21,6 +29,7 @@ pub fn verify<'a>(
2129
if !compile_result.unwrap_or(false) {
2230
return Err(exercise);
2331
}
32+
bar.inc(1);
2433
}
2534
Ok(())
2635
}
@@ -45,7 +54,6 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
4554
let _ = compile(exercise, &progress_bar)?;
4655
progress_bar.finish_and_clear();
4756

48-
success!("Successfully compiled {}!", exercise);
4957
Ok(prompt_for_completion(exercise, None))
5058
}
5159

@@ -71,8 +79,6 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
7179
}
7280
};
7381

74-
success!("Successfully ran {}!", exercise);
75-
7682
Ok(prompt_for_completion(exercise, Some(output.stdout)))
7783
}
7884

@@ -92,7 +98,6 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode, verbose: bool) -> Re
9298
if verbose {
9399
println!("{}", output.stdout);
94100
}
95-
success!("Successfully tested {}", &exercise);
96101
if let RunMode::Interactive = run_mode {
97102
Ok(prompt_for_completion(exercise, None))
98103
} else {
@@ -138,6 +143,12 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
138143
State::Pending(context) => context,
139144
};
140145

146+
match exercise.mode {
147+
Mode::Compile => success!("Successfully ran {}!", exercise),
148+
Mode::Test => success!("Successfully tested {}!", exercise),
149+
Mode::Clippy => success!("Successfully compiled {}!", exercise),
150+
}
151+
141152
let no_emoji = env::var("NO_EMOJI").is_ok();
142153

143154
let clippy_success_msg = if no_emoji {

0 commit comments

Comments
 (0)