1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-07-05 00:48:47 +02:00

Print newline after progress on failure

This commit is contained in:
mo8it
2025-06-28 01:44:36 +02:00
parent e8da6869f8
commit 7af38e684d
2 changed files with 38 additions and 17 deletions

View File

@ -15,6 +15,7 @@ use crate::{
cmd::CmdRunner, cmd::CmdRunner,
exercise::{OUTPUT_CAPACITY, RunnableExercise}, exercise::{OUTPUT_CAPACITY, RunnableExercise},
info_file::{ExerciseInfo, InfoFile}, info_file::{ExerciseInfo, InfoFile},
term::ProgressCounter,
}; };
const MAX_N_EXERCISES: usize = 999; const MAX_N_EXERCISES: usize = 999;
@ -217,10 +218,7 @@ fn check_exercises_unsolved(
.collect::<Result<Vec<_>, _>>() .collect::<Result<Vec<_>, _>>()
.context("Failed to spawn a thread to check if an exercise is already solved")?; .context("Failed to spawn a thread to check if an exercise is already solved")?;
let n_handles = handles.len(); let mut progress_counter = ProgressCounter::new(&mut stdout, handles.len())?;
write!(stdout, "Progress: 0/{n_handles}")?;
stdout.flush()?;
let mut handle_num = 1;
for (exercise_name, handle) in handles { for (exercise_name, handle) in handles {
let Ok(result) = handle.join() else { let Ok(result) = handle.join() else {
@ -235,11 +233,8 @@ fn check_exercises_unsolved(
Err(e) => return Err(e), Err(e) => return Err(e),
} }
write!(stdout, "\rProgress: {handle_num}/{n_handles}")?; progress_counter.increment()?;
stdout.flush()?;
handle_num += 1;
} }
stdout.write_all(b"\n")?;
Ok(()) Ok(())
} }
@ -318,10 +313,7 @@ fn check_solutions(
.arg("always") .arg("always")
.stdin(Stdio::null()); .stdin(Stdio::null());
let n_handles = handles.len(); let mut progress_counter = ProgressCounter::new(&mut stdout, handles.len())?;
write!(stdout, "Progress: 0/{n_handles}")?;
stdout.flush()?;
let mut handle_num = 1;
for (exercise_info, handle) in info_file.exercises.iter().zip(handles) { for (exercise_info, handle) in info_file.exercises.iter().zip(handles) {
let Ok(check_result) = handle.join() else { let Ok(check_result) = handle.join() else {
@ -338,7 +330,7 @@ fn check_solutions(
} }
SolutionCheck::MissingOptional => (), SolutionCheck::MissingOptional => (),
SolutionCheck::RunFailure { output } => { SolutionCheck::RunFailure { output } => {
stdout.write_all(b"\n\n")?; drop(progress_counter);
stdout.write_all(&output)?; stdout.write_all(&output)?;
bail!( bail!(
"Running the solution of the exercise {} failed with the error above", "Running the solution of the exercise {} failed with the error above",
@ -348,11 +340,8 @@ fn check_solutions(
SolutionCheck::Err(e) => return Err(e), SolutionCheck::Err(e) => return Err(e),
} }
write!(stdout, "\rProgress: {handle_num}/{n_handles}")?; progress_counter.increment()?;
stdout.flush()?;
handle_num += 1;
} }
stdout.write_all(b"\n")?;
let n_solutions = sol_paths.len(); let n_solutions = sol_paths.len();
let handle = thread::Builder::new() let handle = thread::Builder::new()

View File

@ -160,6 +160,38 @@ impl<'a, 'lock> CheckProgressVisualizer<'a, 'lock> {
} }
} }
pub struct ProgressCounter<'a, 'lock> {
stdout: &'a mut StdoutLock<'lock>,
total: usize,
counter: usize,
}
impl<'a, 'lock> ProgressCounter<'a, 'lock> {
pub fn new(stdout: &'a mut StdoutLock<'lock>, total: usize) -> io::Result<Self> {
write!(stdout, "Progress: 0/{total}")?;
stdout.flush()?;
Ok(Self {
stdout,
total,
counter: 0,
})
}
pub fn increment(&mut self) -> io::Result<()> {
self.counter += 1;
write!(self.stdout, "\rProgress: {}/{}", self.counter, self.total)?;
self.stdout.flush()
}
}
impl Drop for ProgressCounter<'_, '_> {
fn drop(&mut self) {
let _ = self.stdout.write_all(b"\n\n");
let _ = self.stdout.flush();
}
}
pub fn progress_bar<'a>( pub fn progress_bar<'a>(
writer: &mut impl CountedWrite<'a>, writer: &mut impl CountedWrite<'a>,
progress: u16, progress: u16,