@@ -15,6 +15,7 @@ use crate::{
1515 cmd:: CmdRunner ,
1616 exercise:: { OUTPUT_CAPACITY , RunnableExercise } ,
1717 info_file:: { ExerciseInfo , InfoFile } ,
18+ term:: ProgressCounter ,
1819} ;
1920
2021const MAX_N_EXERCISES : usize = 999 ;
@@ -217,10 +218,7 @@ fn check_exercises_unsolved(
217218 . collect :: < Result < Vec < _ > , _ > > ( )
218219 . context ( "Failed to spawn a thread to check if an exercise is already solved" ) ?;
219220
220- let n_handles = handles. len ( ) ;
221- write ! ( stdout, "Progress: 0/{n_handles}" ) ?;
222- stdout. flush ( ) ?;
223- let mut handle_num = 1 ;
221+ let mut progress_counter = ProgressCounter :: new ( & mut stdout, handles. len ( ) ) ?;
224222
225223 for ( exercise_name, handle) in handles {
226224 let Ok ( result) = handle. join ( ) else {
@@ -235,11 +233,8 @@ fn check_exercises_unsolved(
235233 Err ( e) => return Err ( e) ,
236234 }
237235
238- write ! ( stdout, "\r Progress: {handle_num}/{n_handles}" ) ?;
239- stdout. flush ( ) ?;
240- handle_num += 1 ;
236+ progress_counter. increment ( ) ?;
241237 }
242- stdout. write_all ( b"\n " ) ?;
243238
244239 Ok ( ( ) )
245240}
@@ -318,10 +313,7 @@ fn check_solutions(
318313 . arg ( "always" )
319314 . stdin ( Stdio :: null ( ) ) ;
320315
321- let n_handles = handles. len ( ) ;
322- write ! ( stdout, "Progress: 0/{n_handles}" ) ?;
323- stdout. flush ( ) ?;
324- let mut handle_num = 1 ;
316+ let mut progress_counter = ProgressCounter :: new ( & mut stdout, handles. len ( ) ) ?;
325317
326318 for ( exercise_info, handle) in info_file. exercises . iter ( ) . zip ( handles) {
327319 let Ok ( check_result) = handle. join ( ) else {
@@ -338,7 +330,7 @@ fn check_solutions(
338330 }
339331 SolutionCheck :: MissingOptional => ( ) ,
340332 SolutionCheck :: RunFailure { output } => {
341- stdout . write_all ( b" \n \n " ) ? ;
333+ drop ( progress_counter ) ;
342334 stdout. write_all ( & output) ?;
343335 bail ! (
344336 "Running the solution of the exercise {} failed with the error above" ,
@@ -348,11 +340,8 @@ fn check_solutions(
348340 SolutionCheck :: Err ( e) => return Err ( e) ,
349341 }
350342
351- write ! ( stdout, "\r Progress: {handle_num}/{n_handles}" ) ?;
352- stdout. flush ( ) ?;
353- handle_num += 1 ;
343+ progress_counter. increment ( ) ?;
354344 }
355- stdout. write_all ( b"\n " ) ?;
356345
357346 let n_solutions = sol_paths. len ( ) ;
358347 let handle = thread:: Builder :: new ( )
0 commit comments