From 396ee4d618bc5e1cd5c84495f571f9d3f79774c8 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 13 Oct 2024 23:28:17 +0200 Subject: [PATCH] Show progress with exercise numbers --- src/app_state.rs | 126 +++++++++++++++++---------------------------- src/main.rs | 15 +++--- src/term.rs | 130 ++++++++++++++++++++++------------------------- 3 files changed, 113 insertions(+), 158 deletions(-) diff --git a/src/app_state.rs b/src/app_state.rs index c399842..db9d1f1 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -1,8 +1,5 @@ use anyhow::{bail, Context, Error, Result}; -use crossterm::{ - style::{ResetColor, SetForegroundColor}, - terminal, QueueableCommand, -}; +use crossterm::{cursor, terminal, QueueableCommand}; use std::{ env, fs::{File, OpenOptions}, @@ -23,7 +20,7 @@ use crate::{ embedded::EMBEDDED_FILES, exercise::{Exercise, RunnableExercise}, info_file::ExerciseInfo, - term::{self, progress_bar_with_success}, + term::{self, show_exercises_check_progress}, }; const STATE_FILE_NAME: &str = ".rustlings-state.txt"; @@ -44,18 +41,12 @@ pub enum StateFileStatus { NotRead, } -enum ExerciseCheckProgress { +#[derive(Clone, Copy)] +pub enum ExerciseCheckProgress { + None, Checking, Done, Pending, - Error, -} - -#[derive(Clone, Copy)] -enum ExerciseCheckResult { - Done, - Pending, - Error, } pub struct AppState { @@ -417,27 +408,25 @@ impl AppState { } } - // Return the exercise index of the first pending exercise found. - pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result> { + fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result> { stdout.write_all("Checking all exercises…\n".as_bytes())?; - let n_exercises = self.exercises.len() as u16; let next_exercise_ind = AtomicUsize::new(0); let term_width = terminal::size() .context("Failed to get the terminal size")? .0; + clear_terminal(stdout)?; - let mut results = vec![ExerciseCheckResult::Error; self.exercises.len()]; + let mut progresses = vec![ExerciseCheckProgress::None; self.exercises.len()]; let mut done = 0; let mut pending = 0; thread::scope(|s| { - let mut checking = 0; - let (exercise_result_sender, exercise_result_receiver) = mpsc::channel(); + let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel(); let n_threads = thread::available_parallelism() .map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get()); for _ in 0..n_threads { - let exercise_result_sender = exercise_result_sender.clone(); + let exercise_progress_sender = exercise_progress_sender.clone(); let next_exercise_ind = &next_exercise_ind; let slf = &self; thread::Builder::new() @@ -449,7 +438,7 @@ impl AppState { }; // Notify the progress bar that this exercise is pending. - if exercise_result_sender + if exercise_progress_sender .send((exercise_ind, ExerciseCheckProgress::Checking)) .is_err() { @@ -457,14 +446,17 @@ impl AppState { }; let success = exercise.run_exercise(None, &slf.cmd_runner); - let result = match success { + let progress = match success { Ok(true) => ExerciseCheckProgress::Done, Ok(false) => ExerciseCheckProgress::Pending, - Err(_) => ExerciseCheckProgress::Error, + Err(_) => ExerciseCheckProgress::None, }; // Notify the progress bar that this exercise is done. - if exercise_result_sender.send((exercise_ind, result)).is_err() { + if exercise_progress_sender + .send((exercise_ind, progress)) + .is_err() + { break; } }) @@ -472,102 +464,76 @@ impl AppState { } // Drop this sender to detect when the last thread is done. - drop(exercise_result_sender); + drop(exercise_progress_sender); - // Print the legend. - stdout.write_all(b"Color legend: ")?; - stdout.queue(SetForegroundColor(term::PROGRESS_FAILED_COLOR))?; - stdout.write_all(b"Pending")?; - stdout.queue(ResetColor)?; - stdout.write_all(b" - ")?; - stdout.queue(SetForegroundColor(term::PROGRESS_SUCCESS_COLOR))?; - stdout.write_all(b"Done")?; - stdout.queue(ResetColor)?; - stdout.write_all(b" - ")?; - stdout.queue(SetForegroundColor(term::PROGRESS_PENDING_COLOR))?; - stdout.write_all(b"Checking")?; - stdout.queue(ResetColor)?; - stdout.write_all(b"\n")?; + while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() { + progresses[exercise_ind] = progress; - while let Ok((exercise_ind, result)) = exercise_result_receiver.recv() { - match result { - ExerciseCheckProgress::Checking => checking += 1, - ExerciseCheckProgress::Done => { - results[exercise_ind] = ExerciseCheckResult::Done; - checking -= 1; - done += 1; - } - ExerciseCheckProgress::Pending => { - results[exercise_ind] = ExerciseCheckResult::Pending; - checking -= 1; - pending += 1; - } - ExerciseCheckProgress::Error => checking -= 1, + match progress { + ExerciseCheckProgress::None | ExerciseCheckProgress::Checking => (), + ExerciseCheckProgress::Done => done += 1, + ExerciseCheckProgress::Pending => pending += 1, } - stdout.write_all(b"\r")?; - progress_bar_with_success( - stdout, - checking, - pending, - done, - n_exercises, - term_width, - )?; - stdout.flush()?; + show_exercises_check_progress(stdout, &progresses, term_width)?; } Ok::<_, Error>(()) })?; let mut first_pending_exercise_ind = None; - for (exercise_ind, result) in results.into_iter().enumerate() { - match result { - ExerciseCheckResult::Done => { + for exercise_ind in 0..progresses.len() { + match progresses[exercise_ind] { + ExerciseCheckProgress::Done => { self.set_status(exercise_ind, true)?; } - ExerciseCheckResult::Pending => { + ExerciseCheckProgress::Pending => { self.set_status(exercise_ind, false)?; if first_pending_exercise_ind.is_none() { first_pending_exercise_ind = Some(exercise_ind); } } - ExerciseCheckResult::Error => { + ExerciseCheckProgress::None | ExerciseCheckProgress::Checking => { // If we got an error while checking all exercises in parallel, // it could be because we exceeded the limit of open file descriptors. // Therefore, try running exercises with errors sequentially. + progresses[exercise_ind] = ExerciseCheckProgress::Checking; + show_exercises_check_progress(stdout, &progresses, term_width)?; + let exercise = &self.exercises[exercise_ind]; let success = exercise.run_exercise(None, &self.cmd_runner)?; if success { done += 1; + progresses[exercise_ind] = ExerciseCheckProgress::Done; } else { pending += 1; if first_pending_exercise_ind.is_none() { first_pending_exercise_ind = Some(exercise_ind); } + progresses[exercise_ind] = ExerciseCheckProgress::Pending; } self.set_status(exercise_ind, success)?; - stdout.write_all(b"\r")?; - progress_bar_with_success( - stdout, - u16::from(pending + done < n_exercises), - pending, - done, - n_exercises, - term_width, - )?; - stdout.flush()?; + show_exercises_check_progress(stdout, &progresses, term_width)?; } } } self.write()?; - stdout.write_all(b"\n\n")?; + stdout.write_all(b"\n")?; Ok(first_pending_exercise_ind) } + // Return the exercise index of the first pending exercise found. + pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result> { + stdout.queue(cursor::Hide)?; + let res = self.check_all_exercises_impl(stdout); + stdout.queue(cursor::Show)?; + + res + } + /// Mark the current exercise as done and move on to the next pending exercise if one exists. /// If all exercises are marked as done, run all of them to make sure that they are actually /// done. If an exercise which is marked as done fails, mark it as pending and continue on it. diff --git a/src/main.rs b/src/main.rs index f40bb89..075e726 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,6 @@ use anyhow::{bail, Context, Result}; use app_state::StateFileStatus; use clap::{Parser, Subcommand}; -use crossterm::{ - style::{Color, Print, ResetColor, SetForegroundColor}, - QueueableCommand, -}; use std::{ io::{self, IsTerminal, Write}, path::Path, @@ -157,12 +153,13 @@ fn main() -> Result { let pending = app_state.n_pending(); if pending == 1 { - stdout.queue(Print("One exercise pending: "))?; + stdout.write_all(b"One exercise pending: ")?; } else { - stdout.queue(SetForegroundColor(Color::Red))?; - write!(stdout, "{pending}")?; - stdout.queue(ResetColor)?; - stdout.queue(Print(" exercises are pending. The first: "))?; + write!( + stdout, + "{pending}/{} exercises are pending. The first: ", + app_state.exercises().len(), + )?; } app_state .current_exercise() diff --git a/src/term.rs b/src/term.rs index 31a951d..0294017 100644 --- a/src/term.rs +++ b/src/term.rs @@ -1,6 +1,6 @@ use crossterm::{ cursor::MoveTo, - style::{Attribute, Color, SetAttribute, SetForegroundColor}, + style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, terminal::{Clear, ClearType}, Command, QueueableCommand, }; @@ -9,9 +9,7 @@ use std::{ io::{self, BufRead, StdoutLock, Write}, }; -pub const PROGRESS_FAILED_COLOR: Color = Color::Red; -pub const PROGRESS_SUCCESS_COLOR: Color = Color::Green; -pub const PROGRESS_PENDING_COLOR: Color = Color::Blue; +use crate::app_state::ExerciseCheckProgress; pub struct MaxLenWriter<'a, 'b> { pub stdout: &'a mut StdoutLock<'b>, @@ -89,98 +87,43 @@ impl<'a> CountedWrite<'a> for StdoutLock<'a> { } } -/// Simple terminal progress bar. pub fn progress_bar<'a>( writer: &mut impl CountedWrite<'a>, progress: u16, total: u16, term_width: u16, -) -> io::Result<()> { - progress_bar_with_success(writer, 0, 0, progress, total, term_width) -} - -/// Terminal progress bar with three states (pending + failed + success). -pub fn progress_bar_with_success<'a>( - writer: &mut impl CountedWrite<'a>, - pending: u16, - failed: u16, - success: u16, - total: u16, - term_width: u16, ) -> io::Result<()> { debug_assert!(total < 1000); - debug_assert!(pending + failed + success <= total); + debug_assert!(progress <= total); const PREFIX: &[u8] = b"Progress: ["; const PREFIX_WIDTH: u16 = PREFIX.len() as u16; const POSTFIX_WIDTH: u16 = "] xxx/xxx".len() as u16; const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; - const MIN_TERM_WIDTH: u16 = WRAPPER_WIDTH + 4; + const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; - if term_width < MIN_TERM_WIDTH { + if term_width < MIN_LINE_WIDTH { writer.write_ascii(b"Progress: ")?; // Integers are in ASCII. - return writer.write_ascii(format!("{}/{total}", failed + success).as_bytes()); + return writer.write_ascii(format!("{progress}/{total}").as_bytes()); } let stdout = writer.stdout(); stdout.write_all(PREFIX)?; let width = term_width - WRAPPER_WIDTH; - let mut failed_end = (width * failed) / total; - let mut success_end = (width * (failed + success)) / total; - let mut pending_end = (width * (failed + success + pending)) / total; + let filled = (width * progress) / total; - // In case the range boundaries overlap, "pending" has priority over both - // "failed" and "success" (don't show the bar as "complete" when we are - // still checking some things). - // "Failed" has priority over "success" (don't show 100% success if we - // have some failures, at the risk of showing 100% failures even with - // a few successes). - // - // "Failed" already has priority over "success" because it's displayed - // first. But "pending" is last so we need to fix "success"/"failed". - if pending > 0 { - pending_end = pending_end.max(1); - if pending_end == success_end { - success_end -= 1; - } - if pending_end == failed_end { - failed_end -= 1; - } - - // This will replace the last character of the "pending" range with - // the arrow char ('>'). This ensures that even if the progress bar - // is filled (everything either done or pending), we'll still see - // the '>' as long as we are not fully done. - pending_end -= 1; - } - - if failed > 0 { - stdout.queue(SetForegroundColor(PROGRESS_FAILED_COLOR))?; - for _ in 0..failed_end { - stdout.write_all(b"#")?; - } - } - - stdout.queue(SetForegroundColor(PROGRESS_SUCCESS_COLOR))?; - for _ in failed_end..success_end { + stdout.queue(SetForegroundColor(Color::Green))?; + for _ in 0..filled { stdout.write_all(b"#")?; } - if pending > 0 { - stdout.queue(SetForegroundColor(PROGRESS_PENDING_COLOR))?; - - for _ in success_end..pending_end { - stdout.write_all(b"#")?; - } - } - - if pending_end < width { + if filled < width { stdout.write_all(b">")?; } - let width_minus_filled = width - pending_end; + let width_minus_filled = width - filled; if width_minus_filled > 1 { let red_part_width = width_minus_filled - 1; stdout.queue(SetForegroundColor(Color::Red))?; @@ -191,7 +134,56 @@ pub fn progress_bar_with_success<'a>( stdout.queue(SetForegroundColor(Color::Reset))?; - write!(stdout, "] {:>3}/{}", failed + success, total) + write!(stdout, "] {progress:>3}/{total}") +} + +pub fn show_exercises_check_progress( + stdout: &mut StdoutLock, + progresses: &[ExerciseCheckProgress], + term_width: u16, +) -> io::Result<()> { + stdout.queue(MoveTo(0, 0))?; + + // Legend + stdout.write_all(b"Color of exercise number: ")?; + stdout.queue(SetForegroundColor(Color::Blue))?; + stdout.write_all(b"Checking")?; + stdout.queue(ResetColor)?; + stdout.write_all(b" - ")?; + stdout.queue(SetForegroundColor(Color::Green))?; + stdout.write_all(b"Done")?; + stdout.queue(ResetColor)?; + stdout.write_all(b" - ")?; + stdout.queue(SetForegroundColor(Color::Red))?; + stdout.write_all(b"Pending")?; + stdout.queue(ResetColor)?; + stdout.write_all(b"\n")?; + + // Exercise numbers with up to 3 digits. + let n_cols = usize::from(term_width + 1) / 4; + + let mut exercise_num = 1; + for exercise_progress in progresses { + let color = match exercise_progress { + ExerciseCheckProgress::None => Color::Reset, + ExerciseCheckProgress::Checking => Color::Blue, + ExerciseCheckProgress::Done => Color::Green, + ExerciseCheckProgress::Pending => Color::Red, + }; + + stdout.queue(SetForegroundColor(color))?; + write!(stdout, "{exercise_num:<3}")?; + + if exercise_num % n_cols == 0 { + stdout.write_all(b"\n")?; + } else { + stdout.write_all(b" ")?; + } + + exercise_num += 1; + } + + stdout.queue(ResetColor)?.flush() } pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {