Show progress with exercise numbers

This commit is contained in:
mo8it 2024-10-13 23:28:17 +02:00
parent 326169a7fa
commit 396ee4d618
3 changed files with 113 additions and 158 deletions

View file

@ -1,8 +1,5 @@
use anyhow::{bail, Context, Error, Result}; use anyhow::{bail, Context, Error, Result};
use crossterm::{ use crossterm::{cursor, terminal, QueueableCommand};
style::{ResetColor, SetForegroundColor},
terminal, QueueableCommand,
};
use std::{ use std::{
env, env,
fs::{File, OpenOptions}, fs::{File, OpenOptions},
@ -23,7 +20,7 @@ use crate::{
embedded::EMBEDDED_FILES, embedded::EMBEDDED_FILES,
exercise::{Exercise, RunnableExercise}, exercise::{Exercise, RunnableExercise},
info_file::ExerciseInfo, info_file::ExerciseInfo,
term::{self, progress_bar_with_success}, term::{self, show_exercises_check_progress},
}; };
const STATE_FILE_NAME: &str = ".rustlings-state.txt"; const STATE_FILE_NAME: &str = ".rustlings-state.txt";
@ -44,18 +41,12 @@ pub enum StateFileStatus {
NotRead, NotRead,
} }
enum ExerciseCheckProgress { #[derive(Clone, Copy)]
pub enum ExerciseCheckProgress {
None,
Checking, Checking,
Done, Done,
Pending, Pending,
Error,
}
#[derive(Clone, Copy)]
enum ExerciseCheckResult {
Done,
Pending,
Error,
} }
pub struct AppState { pub struct AppState {
@ -417,27 +408,25 @@ impl AppState {
} }
} }
// Return the exercise index of the first pending exercise found. fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<Option<usize>> {
stdout.write_all("Checking all exercises…\n".as_bytes())?; 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 next_exercise_ind = AtomicUsize::new(0);
let term_width = terminal::size() let term_width = terminal::size()
.context("Failed to get the terminal size")? .context("Failed to get the terminal size")?
.0; .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 done = 0;
let mut pending = 0; let mut pending = 0;
thread::scope(|s| { thread::scope(|s| {
let mut checking = 0; let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel();
let (exercise_result_sender, exercise_result_receiver) = mpsc::channel();
let n_threads = thread::available_parallelism() let n_threads = thread::available_parallelism()
.map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get()); .map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get());
for _ in 0..n_threads { 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 next_exercise_ind = &next_exercise_ind;
let slf = &self; let slf = &self;
thread::Builder::new() thread::Builder::new()
@ -449,7 +438,7 @@ impl AppState {
}; };
// Notify the progress bar that this exercise is pending. // Notify the progress bar that this exercise is pending.
if exercise_result_sender if exercise_progress_sender
.send((exercise_ind, ExerciseCheckProgress::Checking)) .send((exercise_ind, ExerciseCheckProgress::Checking))
.is_err() .is_err()
{ {
@ -457,14 +446,17 @@ impl AppState {
}; };
let success = exercise.run_exercise(None, &slf.cmd_runner); let success = exercise.run_exercise(None, &slf.cmd_runner);
let result = match success { let progress = match success {
Ok(true) => ExerciseCheckProgress::Done, Ok(true) => ExerciseCheckProgress::Done,
Ok(false) => ExerciseCheckProgress::Pending, Ok(false) => ExerciseCheckProgress::Pending,
Err(_) => ExerciseCheckProgress::Error, Err(_) => ExerciseCheckProgress::None,
}; };
// Notify the progress bar that this exercise is done. // 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; break;
} }
}) })
@ -472,102 +464,76 @@ impl AppState {
} }
// Drop this sender to detect when the last thread is done. // Drop this sender to detect when the last thread is done.
drop(exercise_result_sender); drop(exercise_progress_sender);
// Print the legend. while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() {
stdout.write_all(b"Color legend: ")?; progresses[exercise_ind] = progress;
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, result)) = exercise_result_receiver.recv() { match progress {
match result { ExerciseCheckProgress::None | ExerciseCheckProgress::Checking => (),
ExerciseCheckProgress::Checking => checking += 1, ExerciseCheckProgress::Done => done += 1,
ExerciseCheckProgress::Done => { ExerciseCheckProgress::Pending => pending += 1,
results[exercise_ind] = ExerciseCheckResult::Done;
checking -= 1;
done += 1;
}
ExerciseCheckProgress::Pending => {
results[exercise_ind] = ExerciseCheckResult::Pending;
checking -= 1;
pending += 1;
}
ExerciseCheckProgress::Error => checking -= 1,
} }
stdout.write_all(b"\r")?; show_exercises_check_progress(stdout, &progresses, term_width)?;
progress_bar_with_success(
stdout,
checking,
pending,
done,
n_exercises,
term_width,
)?;
stdout.flush()?;
} }
Ok::<_, Error>(()) Ok::<_, Error>(())
})?; })?;
let mut first_pending_exercise_ind = None; let mut first_pending_exercise_ind = None;
for (exercise_ind, result) in results.into_iter().enumerate() { for exercise_ind in 0..progresses.len() {
match result { match progresses[exercise_ind] {
ExerciseCheckResult::Done => { ExerciseCheckProgress::Done => {
self.set_status(exercise_ind, true)?; self.set_status(exercise_ind, true)?;
} }
ExerciseCheckResult::Pending => { ExerciseCheckProgress::Pending => {
self.set_status(exercise_ind, false)?; self.set_status(exercise_ind, false)?;
if first_pending_exercise_ind.is_none() { if first_pending_exercise_ind.is_none() {
first_pending_exercise_ind = Some(exercise_ind); first_pending_exercise_ind = Some(exercise_ind);
} }
} }
ExerciseCheckResult::Error => { ExerciseCheckProgress::None | ExerciseCheckProgress::Checking => {
// If we got an error while checking all exercises in parallel, // If we got an error while checking all exercises in parallel,
// it could be because we exceeded the limit of open file descriptors. // it could be because we exceeded the limit of open file descriptors.
// Therefore, try running exercises with errors sequentially. // 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 exercise = &self.exercises[exercise_ind];
let success = exercise.run_exercise(None, &self.cmd_runner)?; let success = exercise.run_exercise(None, &self.cmd_runner)?;
if success { if success {
done += 1; done += 1;
progresses[exercise_ind] = ExerciseCheckProgress::Done;
} else { } else {
pending += 1; pending += 1;
if first_pending_exercise_ind.is_none() { if first_pending_exercise_ind.is_none() {
first_pending_exercise_ind = Some(exercise_ind); first_pending_exercise_ind = Some(exercise_ind);
} }
progresses[exercise_ind] = ExerciseCheckProgress::Pending;
} }
self.set_status(exercise_ind, success)?; self.set_status(exercise_ind, success)?;
stdout.write_all(b"\r")?; show_exercises_check_progress(stdout, &progresses, term_width)?;
progress_bar_with_success(
stdout,
u16::from(pending + done < n_exercises),
pending,
done,
n_exercises,
term_width,
)?;
stdout.flush()?;
} }
} }
} }
self.write()?; self.write()?;
stdout.write_all(b"\n\n")?; stdout.write_all(b"\n")?;
Ok(first_pending_exercise_ind) 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<Option<usize>> {
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. /// 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 /// 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. /// done. If an exercise which is marked as done fails, mark it as pending and continue on it.

View file

@ -1,10 +1,6 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use app_state::StateFileStatus; use app_state::StateFileStatus;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use crossterm::{
style::{Color, Print, ResetColor, SetForegroundColor},
QueueableCommand,
};
use std::{ use std::{
io::{self, IsTerminal, Write}, io::{self, IsTerminal, Write},
path::Path, path::Path,
@ -157,12 +153,13 @@ fn main() -> Result<ExitCode> {
let pending = app_state.n_pending(); let pending = app_state.n_pending();
if pending == 1 { if pending == 1 {
stdout.queue(Print("One exercise pending: "))?; stdout.write_all(b"One exercise pending: ")?;
} else { } else {
stdout.queue(SetForegroundColor(Color::Red))?; write!(
write!(stdout, "{pending}")?; stdout,
stdout.queue(ResetColor)?; "{pending}/{} exercises are pending. The first: ",
stdout.queue(Print(" exercises are pending. The first: "))?; app_state.exercises().len(),
)?;
} }
app_state app_state
.current_exercise() .current_exercise()

View file

@ -1,6 +1,6 @@
use crossterm::{ use crossterm::{
cursor::MoveTo, cursor::MoveTo,
style::{Attribute, Color, SetAttribute, SetForegroundColor}, style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor},
terminal::{Clear, ClearType}, terminal::{Clear, ClearType},
Command, QueueableCommand, Command, QueueableCommand,
}; };
@ -9,9 +9,7 @@ use std::{
io::{self, BufRead, StdoutLock, Write}, io::{self, BufRead, StdoutLock, Write},
}; };
pub const PROGRESS_FAILED_COLOR: Color = Color::Red; use crate::app_state::ExerciseCheckProgress;
pub const PROGRESS_SUCCESS_COLOR: Color = Color::Green;
pub const PROGRESS_PENDING_COLOR: Color = Color::Blue;
pub struct MaxLenWriter<'a, 'b> { pub struct MaxLenWriter<'a, 'b> {
pub stdout: &'a mut StdoutLock<'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>( pub fn progress_bar<'a>(
writer: &mut impl CountedWrite<'a>, writer: &mut impl CountedWrite<'a>,
progress: u16, progress: u16,
total: u16, total: u16,
term_width: 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<()> { ) -> io::Result<()> {
debug_assert!(total < 1000); debug_assert!(total < 1000);
debug_assert!(pending + failed + success <= total); debug_assert!(progress <= total);
const PREFIX: &[u8] = b"Progress: ["; const PREFIX: &[u8] = b"Progress: [";
const PREFIX_WIDTH: u16 = PREFIX.len() as u16; const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
const POSTFIX_WIDTH: u16 = "] xxx/xxx".len() as u16; const POSTFIX_WIDTH: u16 = "] xxx/xxx".len() as u16;
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; 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: ")?; writer.write_ascii(b"Progress: ")?;
// Integers are in ASCII. // 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(); let stdout = writer.stdout();
stdout.write_all(PREFIX)?; stdout.write_all(PREFIX)?;
let width = term_width - WRAPPER_WIDTH; let width = term_width - WRAPPER_WIDTH;
let mut failed_end = (width * failed) / total; let filled = (width * progress) / total;
let mut success_end = (width * (failed + success)) / total;
let mut pending_end = (width * (failed + success + pending)) / total;
// In case the range boundaries overlap, "pending" has priority over both stdout.queue(SetForegroundColor(Color::Green))?;
// "failed" and "success" (don't show the bar as "complete" when we are for _ in 0..filled {
// 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.write_all(b"#")?; stdout.write_all(b"#")?;
} }
if pending > 0 { if filled < width {
stdout.queue(SetForegroundColor(PROGRESS_PENDING_COLOR))?;
for _ in success_end..pending_end {
stdout.write_all(b"#")?;
}
}
if pending_end < width {
stdout.write_all(b">")?; stdout.write_all(b">")?;
} }
let width_minus_filled = width - pending_end; let width_minus_filled = width - filled;
if width_minus_filled > 1 { if width_minus_filled > 1 {
let red_part_width = width_minus_filled - 1; let red_part_width = width_minus_filled - 1;
stdout.queue(SetForegroundColor(Color::Red))?; stdout.queue(SetForegroundColor(Color::Red))?;
@ -191,7 +134,56 @@ pub fn progress_bar_with_success<'a>(
stdout.queue(SetForegroundColor(Color::Reset))?; 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<()> { pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {