Skip to content

Commit 7af38e6

Browse files
committed
Print newline after progress on failure
1 parent e8da686 commit 7af38e6

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/dev/check.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
cmd::CmdRunner,
1616
exercise::{OUTPUT_CAPACITY, RunnableExercise},
1717
info_file::{ExerciseInfo, InfoFile},
18+
term::ProgressCounter,
1819
};
1920

2021
const 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, "\rProgress: {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, "\rProgress: {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()

src/term.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ impl<'a, 'lock> CheckProgressVisualizer<'a, 'lock> {
160160
}
161161
}
162162

163+
pub struct ProgressCounter<'a, 'lock> {
164+
stdout: &'a mut StdoutLock<'lock>,
165+
total: usize,
166+
counter: usize,
167+
}
168+
169+
impl<'a, 'lock> ProgressCounter<'a, 'lock> {
170+
pub fn new(stdout: &'a mut StdoutLock<'lock>, total: usize) -> io::Result<Self> {
171+
write!(stdout, "Progress: 0/{total}")?;
172+
stdout.flush()?;
173+
174+
Ok(Self {
175+
stdout,
176+
total,
177+
counter: 0,
178+
})
179+
}
180+
181+
pub fn increment(&mut self) -> io::Result<()> {
182+
self.counter += 1;
183+
write!(self.stdout, "\rProgress: {}/{}", self.counter, self.total)?;
184+
self.stdout.flush()
185+
}
186+
}
187+
188+
impl Drop for ProgressCounter<'_, '_> {
189+
fn drop(&mut self) {
190+
let _ = self.stdout.write_all(b"\n\n");
191+
let _ = self.stdout.flush();
192+
}
193+
}
194+
163195
pub fn progress_bar<'a>(
164196
writer: &mut impl CountedWrite<'a>,
165197
progress: u16,

0 commit comments

Comments
 (0)