diff --git a/clippy.toml b/clippy.toml index 4a5dd06..11ec6cc 100644 --- a/clippy.toml +++ b/clippy.toml @@ -13,4 +13,6 @@ disallowed-methods = [ # Use `thread::Builder::spawn` instead and handle the error. "std::thread::spawn", "std::thread::Scope::spawn", + # Return `ExitCode` instead. + "std::process::exit", ] diff --git a/src/app_state.rs b/src/app_state.rs index c879955..5f84d35 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -1,10 +1,15 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Context, Error, Result}; +use crossterm::{cursor, terminal, QueueableCommand}; use std::{ env, fs::{File, OpenOptions}, - io::{self, Read, Seek, StdoutLock, Write}, + io::{Read, Seek, StdoutLock, Write}, path::{Path, MAIN_SEPARATOR_STR}, process::{Command, Stdio}, + sync::{ + atomic::{AtomicUsize, Ordering::Relaxed}, + mpsc, + }, thread, }; @@ -15,10 +20,11 @@ use crate::{ embedded::EMBEDDED_FILES, exercise::{Exercise, RunnableExercise}, info_file::ExerciseInfo, - term, + term::{self, CheckProgressVisualizer}, }; const STATE_FILE_NAME: &str = ".rustlings-state.txt"; +const DEFAULT_CHECK_PARALLELISM: usize = 8; #[must_use] pub enum ExercisesProgress { @@ -35,10 +41,12 @@ pub enum StateFileStatus { NotRead, } -enum AllExercisesCheck { - Pending(usize), - AllDone, - CheckedUntil(usize), +#[derive(Clone, Copy)] +pub enum CheckProgress { + None, + Checking, + Done, + Pending, } pub struct AppState { @@ -194,6 +202,11 @@ impl AppState { self.n_done } + #[inline] + pub fn n_pending(&self) -> u16 { + self.exercises.len() as u16 - self.n_done + } + #[inline] pub fn current_exercise(&self) -> &Exercise { &self.exercises[self.current_exercise_ind] @@ -270,15 +283,31 @@ impl AppState { self.write() } - pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> { + // Set the status of an exercise without saving. Returns `true` if the + // status actually changed (and thus needs saving later). + pub fn set_status(&mut self, exercise_ind: usize, done: bool) -> Result { let exercise = self .exercises .get_mut(exercise_ind) .context(BAD_INDEX_ERR)?; - if exercise.done { - exercise.done = false; + if exercise.done == done { + return Ok(false); + } + + exercise.done = done; + if done { + self.n_done += 1; + } else { self.n_done -= 1; + } + + Ok(true) + } + + // Set the status of an exercise to "pending" and save. + pub fn set_pending(&mut self, exercise_ind: usize) -> Result<()> { + if self.set_status(exercise_ind, false)? { self.write()?; } @@ -379,63 +408,114 @@ impl AppState { } } - // Return the exercise index of the first pending exercise found. - fn check_all_exercises(&self, stdout: &mut StdoutLock) -> Result> { - stdout.write_all(FINAL_CHECK_MSG)?; - let n_exercises = self.exercises.len(); + fn check_all_exercises_impl(&mut self, stdout: &mut StdoutLock) -> Result> { + let term_width = terminal::size() + .context("Failed to get the terminal size")? + .0; + let mut progress_visualizer = CheckProgressVisualizer::build(stdout, term_width)?; - let status = thread::scope(|s| { - let handles = self - .exercises - .iter() - .map(|exercise| { - thread::Builder::new() - .spawn_scoped(s, || exercise.run_exercise(None, &self.cmd_runner)) - }) - .collect::>(); + let next_exercise_ind = AtomicUsize::new(0); + let mut progresses = vec![CheckProgress::None; self.exercises.len()]; - for (exercise_ind, spawn_res) in handles.into_iter().enumerate() { - write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?; - stdout.flush()?; + thread::scope(|s| { + let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel(); + let n_threads = thread::available_parallelism() + .map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get()); - let Ok(handle) = spawn_res else { - return Ok(AllExercisesCheck::CheckedUntil(exercise_ind)); - }; + for _ in 0..n_threads { + let exercise_progress_sender = exercise_progress_sender.clone(); + let next_exercise_ind = &next_exercise_ind; + let slf = &self; + thread::Builder::new() + .spawn_scoped(s, move || loop { + let exercise_ind = next_exercise_ind.fetch_add(1, Relaxed); + let Some(exercise) = slf.exercises.get(exercise_ind) else { + // No more exercises. + break; + }; - let Ok(success) = handle.join().unwrap() else { - return Ok(AllExercisesCheck::CheckedUntil(exercise_ind)); - }; + if exercise_progress_sender + .send((exercise_ind, CheckProgress::Checking)) + .is_err() + { + break; + }; - if !success { - return Ok(AllExercisesCheck::Pending(exercise_ind)); - } + let success = exercise.run_exercise(None, &slf.cmd_runner); + let progress = match success { + Ok(true) => CheckProgress::Done, + Ok(false) => CheckProgress::Pending, + Err(_) => CheckProgress::None, + }; + + if exercise_progress_sender + .send((exercise_ind, progress)) + .is_err() + { + break; + } + }) + .context("Failed to spawn a thread to check all exercises")?; } - Ok::<_, io::Error>(AllExercisesCheck::AllDone) + // Drop this sender to detect when the last thread is done. + drop(exercise_progress_sender); + + while let Ok((exercise_ind, progress)) = exercise_progress_receiver.recv() { + progresses[exercise_ind] = progress; + progress_visualizer.update(&progresses)?; + } + + Ok::<_, Error>(()) })?; - let mut exercise_ind = match status { - AllExercisesCheck::Pending(exercise_ind) => return Ok(Some(exercise_ind)), - AllExercisesCheck::AllDone => return Ok(None), - AllExercisesCheck::CheckedUntil(ind) => ind, - }; + let mut first_pending_exercise_ind = None; + for exercise_ind in 0..progresses.len() { + match progresses[exercise_ind] { + CheckProgress::Done => { + self.set_status(exercise_ind, true)?; + } + CheckProgress::Pending => { + self.set_status(exercise_ind, false)?; + if first_pending_exercise_ind.is_none() { + first_pending_exercise_ind = Some(exercise_ind); + } + } + CheckProgress::None | CheckProgress::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] = CheckProgress::Checking; + progress_visualizer.update(&progresses)?; - // We got an error while checking all exercises in parallel. - // This could be because we exceeded the limit of open file descriptors. - // Therefore, try to continue the check sequentially. - for exercise in &self.exercises[exercise_ind..] { - write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?; - stdout.flush()?; - - let success = exercise.run_exercise(None, &self.cmd_runner)?; - if !success { - return Ok(Some(exercise_ind)); + let exercise = &self.exercises[exercise_ind]; + let success = exercise.run_exercise(None, &self.cmd_runner)?; + if success { + progresses[exercise_ind] = CheckProgress::Done; + } else { + progresses[exercise_ind] = CheckProgress::Pending; + if first_pending_exercise_ind.is_none() { + first_pending_exercise_ind = Some(exercise_ind); + } + } + self.set_status(exercise_ind, success)?; + progress_visualizer.update(&progresses)?; + } } - - exercise_ind += 1; } - Ok(None) + self.write()?; + + 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. @@ -462,20 +542,18 @@ impl AppState { stdout.write_all(b"\n")?; } - if let Some(pending_exercise_ind) = self.check_all_exercises(stdout)? { - stdout.write_all(b"\n\n")?; + if let Some(first_pending_exercise_ind) = self.check_all_exercises(stdout)? { + self.set_current_exercise_ind(first_pending_exercise_ind)?; - self.current_exercise_ind = pending_exercise_ind; - self.exercises[pending_exercise_ind].done = false; - // All exercises were marked as done. - self.n_done -= 1; - self.write()?; return Ok(ExercisesProgress::NewPending); } - // Write that the last exercise is done. - self.write()?; + self.render_final_message(stdout)?; + Ok(ExercisesProgress::AllDone) + } + + pub fn render_final_message(&self, stdout: &mut StdoutLock) -> Result<()> { clear_terminal(stdout)?; stdout.write_all(FENISH_LINE.as_bytes())?; @@ -485,15 +563,12 @@ impl AppState { stdout.write_all(b"\n")?; } - Ok(ExercisesProgress::AllDone) + Ok(()) } } const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises"; const STATE_FILE_HEADER: &[u8] = b"DON'T EDIT THIS FILE!\n\n"; -const FINAL_CHECK_MSG: &[u8] = b"All exercises seem to be done. -Recompiling and running all exercises to make sure that all of them are actually done. -"; const FENISH_LINE: &str = "+----------------------------------------------------+ | You made it to the Fe-nish line! | +-------------------------- ------------------------+ diff --git a/src/main.rs b/src/main.rs index fe4b3dc..c8bcd2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use clap::{Parser, Subcommand}; use std::{ io::{self, IsTerminal, Write}, path::Path, - process::exit, + process::ExitCode, }; use term::{clear_terminal, press_enter_prompt}; @@ -47,6 +47,8 @@ enum Subcommands { /// The name of the exercise name: Option, }, + /// Check all the exercises, marking them as done or pending accordingly. + CheckAll, /// Reset a single exercise Reset { /// The name of the exercise @@ -62,22 +64,26 @@ enum Subcommands { Dev(DevCommands), } -fn main() -> Result<()> { +fn main() -> Result { let args = Args::parse(); if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() { bail!("{OLD_METHOD_ERR}"); } - match args.command { - Some(Subcommands::Init) => return init::init().context("Initialization failed"), - Some(Subcommands::Dev(dev_command)) => return dev_command.run(), - _ => (), + 'priority_cmd: { + match args.command { + Some(Subcommands::Init) => init::init().context("Initialization failed")?, + Some(Subcommands::Dev(dev_command)) => dev_command.run()?, + _ => break 'priority_cmd, + } + + return Ok(ExitCode::SUCCESS); } if !Path::new("exercises").is_dir() { println!("{PRE_INIT_MSG}"); - exit(1); + return Ok(ExitCode::FAILURE); } let info_file = InfoFile::parse()?; @@ -136,7 +142,35 @@ fn main() -> Result<()> { if let Some(name) = name { app_state.set_current_exercise_by_name(&name)?; } - run::run(&mut app_state)?; + return run::run(&mut app_state); + } + Some(Subcommands::CheckAll) => { + let mut stdout = io::stdout().lock(); + if let Some(first_pending_exercise_ind) = app_state.check_all_exercises(&mut stdout)? { + if app_state.current_exercise().done { + app_state.set_current_exercise_ind(first_pending_exercise_ind)?; + } + + stdout.write_all(b"\n\n")?; + let pending = app_state.n_pending(); + if pending == 1 { + stdout.write_all(b"One exercise pending: ")?; + } else { + write!( + stdout, + "{pending}/{} exercises pending. The first: ", + app_state.exercises().len(), + )?; + } + app_state + .current_exercise() + .terminal_file_link(&mut stdout)?; + stdout.write_all(b"\n")?; + + return Ok(ExitCode::FAILURE); + } else { + app_state.render_final_message(&mut stdout)?; + } } Some(Subcommands::Reset { name }) => { app_state.set_current_exercise_by_name(&name)?; @@ -153,7 +187,7 @@ fn main() -> Result<()> { Some(Subcommands::Init | Subcommands::Dev(_)) => (), } - Ok(()) + Ok(ExitCode::SUCCESS) } const OLD_METHOD_ERR: &str = diff --git a/src/run.rs b/src/run.rs index 3fddcf2..ac8b26a 100644 --- a/src/run.rs +++ b/src/run.rs @@ -5,7 +5,7 @@ use crossterm::{ }; use std::{ io::{self, Write}, - process::exit, + process::ExitCode, }; use crate::{ @@ -13,7 +13,7 @@ use crate::{ exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY}, }; -pub fn run(app_state: &mut AppState) -> Result<()> { +pub fn run(app_state: &mut AppState) -> Result { let exercise = app_state.current_exercise(); let mut output = Vec::with_capacity(OUTPUT_CAPACITY); let success = exercise.run_exercise(Some(&mut output), app_state.cmd_runner())?; @@ -29,7 +29,8 @@ pub fn run(app_state: &mut AppState) -> Result<()> { .current_exercise() .terminal_file_link(&mut stdout)?; stdout.write_all(b" with errors\n")?; - exit(1); + + return Ok(ExitCode::FAILURE); } stdout.queue(SetForegroundColor(Color::Green))?; @@ -55,5 +56,5 @@ pub fn run(app_state: &mut AppState) -> Result<()> { ExercisesProgress::AllDone => (), } - Ok(()) + Ok(ExitCode::SUCCESS) } diff --git a/src/term.rs b/src/term.rs index 5b557ec..86909f0 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,6 +9,8 @@ use std::{ io::{self, BufRead, StdoutLock, Write}, }; +use crate::app_state::CheckProgress; + pub struct MaxLenWriter<'a, 'b> { pub stdout: &'a mut StdoutLock<'b>, len: usize, @@ -85,12 +87,84 @@ impl<'a> CountedWrite<'a> for StdoutLock<'a> { } } -/// Terminal progress bar to be used when not using Ratataui. +pub struct CheckProgressVisualizer<'a, 'b> { + stdout: &'a mut StdoutLock<'b>, + n_cols: usize, +} + +impl<'a, 'b> CheckProgressVisualizer<'a, 'b> { + const CHECKING_COLOR: Color = Color::Blue; + const DONE_COLOR: Color = Color::Green; + const PENDING_COLOR: Color = Color::Red; + + pub fn build(stdout: &'a mut StdoutLock<'b>, term_width: u16) -> io::Result { + clear_terminal(stdout)?; + stdout.write_all("Checking all exercises…\n".as_bytes())?; + + // Legend + stdout.write_all(b"Color of exercise number: ")?; + stdout.queue(SetForegroundColor(Self::CHECKING_COLOR))?; + stdout.write_all(b"Checking")?; + stdout.queue(ResetColor)?; + stdout.write_all(b" - ")?; + stdout.queue(SetForegroundColor(Self::DONE_COLOR))?; + stdout.write_all(b"Done")?; + stdout.queue(ResetColor)?; + stdout.write_all(b" - ")?; + stdout.queue(SetForegroundColor(Self::PENDING_COLOR))?; + stdout.write_all(b"Pending")?; + stdout.queue(ResetColor)?; + stdout.write_all(b"\n")?; + + // Exercise numbers with up to 3 digits. + // +1 because the last column doesn't end with a whitespace. + let n_cols = usize::from(term_width + 1) / 4; + + Ok(Self { stdout, n_cols }) + } + + pub fn update(&mut self, progresses: &[CheckProgress]) -> io::Result<()> { + self.stdout.queue(MoveTo(0, 2))?; + + let mut exercise_num = 1; + for exercise_progress in progresses { + match exercise_progress { + CheckProgress::None => (), + CheckProgress::Checking => { + self.stdout + .queue(SetForegroundColor(Self::CHECKING_COLOR))?; + } + CheckProgress::Done => { + self.stdout.queue(SetForegroundColor(Self::DONE_COLOR))?; + } + CheckProgress::Pending => { + self.stdout.queue(SetForegroundColor(Self::PENDING_COLOR))?; + } + } + + write!(self.stdout, "{exercise_num:<3}")?; + self.stdout.queue(ResetColor)?; + + if exercise_num != progresses.len() { + if exercise_num % self.n_cols == 0 { + self.stdout.write_all(b"\n")?; + } else { + self.stdout.write_all(b" ")?; + } + + exercise_num += 1; + } + } + + self.stdout.flush() + } +} + pub fn progress_bar<'a>( writer: &mut impl CountedWrite<'a>, progress: u16, total: u16, - line_width: u16, + term_width: u16, ) -> io::Result<()> { debug_assert!(total < 1000); debug_assert!(progress <= total); @@ -101,7 +175,7 @@ pub fn progress_bar<'a>( const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; - if line_width < MIN_LINE_WIDTH { + if term_width < MIN_LINE_WIDTH { writer.write_ascii(b"Progress: ")?; // Integers are in ASCII. return writer.write_ascii(format!("{progress}/{total}").as_bytes()); @@ -110,7 +184,7 @@ pub fn progress_bar<'a>( let stdout = writer.stdout(); stdout.write_all(PREFIX)?; - let width = line_width - WRAPPER_WIDTH; + let width = term_width - WRAPPER_WIDTH; let filled = (width * progress) / total; stdout.queue(SetForegroundColor(Color::Green))?; diff --git a/src/watch.rs b/src/watch.rs index 35533b0..6259c9d 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -103,6 +103,13 @@ fn run_watch( WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?, WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?, WatchEvent::Input(InputEvent::List) => return Ok(WatchExit::List), + WatchEvent::Input(InputEvent::CheckAll) => match watch_state + .check_all_exercises(&mut stdout)? + { + ExercisesProgress::AllDone => break, + ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?, + ExercisesProgress::CurrentPending => watch_state.render(&mut stdout)?, + }, WatchEvent::Input(InputEvent::Reset) => watch_state.reset_exercise(&mut stdout)?, WatchEvent::Input(InputEvent::Quit) => { stdout.write_all(QUIT_MSG)?; diff --git a/src/watch/state.rs b/src/watch/state.rs index 19910f0..0ac758c 100644 --- a/src/watch/state.rs +++ b/src/watch/state.rs @@ -157,8 +157,9 @@ impl<'a> WatchState<'a> { /// Move on to the next exercise if the current one is done. pub fn next_exercise(&mut self, stdout: &mut StdoutLock) -> Result { - if self.done_status == DoneStatus::Pending { - return Ok(ExercisesProgress::CurrentPending); + match self.done_status { + DoneStatus::DoneWithSolution(_) | DoneStatus::DoneWithoutSolution => (), + DoneStatus::Pending => return Ok(ExercisesProgress::CurrentPending), } self.app_state.done_current_exercise::(stdout) @@ -195,6 +196,11 @@ impl<'a> WatchState<'a> { stdout.queue(ResetColor)?; stdout.write_all(b":list / ")?; + stdout.queue(SetAttribute(Attribute::Bold))?; + stdout.write_all(b"c")?; + stdout.queue(ResetColor)?; + stdout.write_all(b":check all / ")?; + stdout.queue(SetAttribute(Attribute::Bold))?; stdout.write_all(b"x")?; stdout.queue(ResetColor)?; @@ -274,6 +280,22 @@ impl<'a> WatchState<'a> { Ok(()) } + pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result { + if let Some(first_pending_exercise_ind) = self.app_state.check_all_exercises(stdout)? { + // Only change exercise if the current one is done. + if self.app_state.current_exercise().done { + self.app_state + .set_current_exercise_ind(first_pending_exercise_ind)?; + Ok(ExercisesProgress::NewPending) + } else { + Ok(ExercisesProgress::CurrentPending) + } + } else { + self.app_state.render_final_message(stdout)?; + Ok(ExercisesProgress::AllDone) + } + } + pub fn update_term_width(&mut self, width: u16, stdout: &mut StdoutLock) -> io::Result<()> { if self.term_width != width { self.term_width = width; diff --git a/src/watch/terminal_event.rs b/src/watch/terminal_event.rs index 1ed681d..48411db 100644 --- a/src/watch/terminal_event.rs +++ b/src/watch/terminal_event.rs @@ -11,6 +11,7 @@ pub enum InputEvent { Run, Hint, List, + CheckAll, Reset, Quit, } @@ -37,6 +38,7 @@ pub fn terminal_event_handler( KeyCode::Char('r') if manual_run => InputEvent::Run, KeyCode::Char('h') => InputEvent::Hint, KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List), + KeyCode::Char('c') => InputEvent::CheckAll, KeyCode::Char('x') => { if sender.send(WatchEvent::Input(InputEvent::Reset)).is_err() { return;