Skip to content

Commit a6f50fd

Browse files
authored
Merge pull request #484 from rytheo/progress-indicator
feat!: Add progress indicator
2 parents d2179d3 + 6fae5d6 commit a6f50fd

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn main() {
203203
}
204204

205205
Subcommands::Verify(_subargs) => {
206-
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
206+
verify(&exercises, (0, exercises.len()), verbose).unwrap_or_else(|_| std::process::exit(1));
207207
}
208208

209209
Subcommands::Watch(_subargs) => match watch(&exercises, verbose) {
@@ -295,7 +295,7 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
295295
clear_screen();
296296

297297
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
298-
let failed_exercise_hint = match verify(exercises.iter(), verbose) {
298+
let failed_exercise_hint = match verify(exercises.iter(), (0, exercises.len()), verbose) {
299299
Ok(_) => return Ok(WatchStatus::Finished),
300300
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
301301
};
@@ -308,11 +308,11 @@ fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<WatchStatus> {
308308
let filepath = b.as_path().canonicalize().unwrap();
309309
let pending_exercises = exercises
310310
.iter()
311-
.skip_while(|e| !filepath.ends_with(&e.path))
312-
// .filter(|e| filepath.ends_with(&e.path))
311+
.find(|e| filepath.ends_with(&e.path)).into_iter()
313312
.chain(exercises.iter().filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)));
313+
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
314314
clear_screen();
315-
match verify(pending_exercises, verbose) {
315+
match verify(pending_exercises, (num_done, exercises.len()), verbose) {
316316
Ok(_) => return Ok(WatchStatus::Finished),
317317
Err(exercise) => {
318318
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)