Compare commits

...

19 commits

Author SHA1 Message Date
Enrico
c8f04c4366
Merge e59c65cf45 into baeeff389c 2024-10-13 16:41:19 -07:00
Mo
baeeff389c
Merge pull request #2122 from Nahor/check_all
Some checks are pending
Rustlings Tests / clippy (push) Waiting to run
Rustlings Tests / fmt (push) Waiting to run
Rustlings Tests / test (macOS-latest) (push) Waiting to run
Rustlings Tests / test (ubuntu-latest) (push) Waiting to run
Rustlings Tests / test (windows-latest) (push) Waiting to run
Rustlings Tests / dev-check (push) Waiting to run
Web / Build and deploy site and docs (push) Waiting to run
Improvement to "check all exercises"
2024-10-14 01:29:25 +02:00
mo8it
932bc25d88 Remove unneeded line 2024-10-14 01:28:34 +02:00
mo8it
bdc6dad8de Update names 2024-10-14 01:28:12 +02:00
mo8it
ea73af9ba3 Separate initialization with a struct 2024-10-14 01:06:11 +02:00
mo8it
fc5fc0920f Remove outdated comments 2024-10-14 00:48:12 +02:00
mo8it
9705c161b4 Remove the tracking of done and pending 2024-10-14 00:45:41 +02:00
mo8it
8cac21511c Small improvements to showing progress 2024-10-14 00:42:49 +02:00
mo8it
396ee4d618 Show progress with exercise numbers 2024-10-13 23:28:17 +02:00
mo8it
326169a7fa Improve check-all command 2024-10-13 22:02:41 +02:00
mo8it
685e069c58 First PR review changes 2024-10-10 19:43:35 +02:00
Nahor
d3f819f86f Add command line command to check all exercises 2024-10-04 14:36:36 -07:00
Nahor
aa83fd6bc4 Show a progress bar when running check_all
Replace the "Progress: xxx/yyy" with a progress bar when checking all
the exercises
2024-10-02 15:28:42 -07:00
Nahor
e2f7734f37 Limit the amount of parallelism in check_all
Don't create more threads than there are CPU cores.
2024-10-02 14:42:50 -07:00
Nahor
5c17abd1bf Use a channel to update the check_all progress
The previous code was checking the threads in the order they were
created. So the progress update would be blocked on an earlier thread
even if later thread were already done.

Add to that that multiple instances of `cargo build` cannot run in
parallel, they will be serialized instead. So if the exercises needs to
be recompiled, depending on the order those `cargo build` are run,
the first update can be a long time coming.

So instead of relying on the thread terminating, use a channel to get
notified when an exercise check is done, regardless of the order they
finish in.
2024-10-02 14:10:26 -07:00
Nahor
c52867eb8b Add command to check all the exercises
This allows for skipping repeating "next" when multiple exercises
are done at once, or when earlier exercises have been updated/changed
(and thus must be redone) while still working of the whole set (i.e.
the final check_all is not yet available to flag those undone exercises)
2024-10-02 13:40:32 -07:00
Nahor
26fd97a209 Update all exercises during the final check
The previous code run the check on all exercises but only updates one
exercise (the first that failed) even if multiple failed. The user won't
be able to see all the failed exercises when viewing the list, and will
have to run check_all after each fixed exercise.

This change will update all the exercises so the user can see all that
failed, fix them all, and only then need run check_all again.
2024-10-02 11:45:55 -07:00
Enrico
e59c65cf45 chore: Refactored to adhere to new rustlings version 2024-09-29 20:58:33 +02:00
Enrico
1c27aeead9 feat: add functions6.rs and move_semantics6.rs exercises about closures 2024-09-28 11:59:09 +02:00
16 changed files with 404 additions and 89 deletions

View file

@ -13,4 +13,6 @@ disallowed-methods = [
# Use `thread::Builder::spawn` instead and handle the error. # Use `thread::Builder::spawn` instead and handle the error.
"std::thread::spawn", "std::thread::spawn",
"std::thread::Scope::spawn", "std::thread::Scope::spawn",
# Return `ExitCode` instead.
"std::process::exit",
] ]

View file

@ -26,6 +26,8 @@ bin = [
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" }, { name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" }, { name = "functions5", path = "../exercises/02_functions/functions5.rs" },
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" }, { name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
{ name = "functions6", path = "../exercises/02_functions/functions6.rs" },
{ name = "functions6_sol", path = "../solutions/02_functions/functions6.rs" },
{ name = "if1", path = "../exercises/03_if/if1.rs" }, { name = "if1", path = "../exercises/03_if/if1.rs" },
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" }, { name = "if1_sol", path = "../solutions/03_if/if1.rs" },
{ name = "if2", path = "../exercises/03_if/if2.rs" }, { name = "if2", path = "../exercises/03_if/if2.rs" },
@ -60,6 +62,8 @@ bin = [
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" }, { name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" }, { name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" }, { name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
{ name = "move_semantics6", path = "../exercises/06_move_semantics/move_semantics6.rs" },
{ name = "move_semantics6_sol", path = "../solutions/06_move_semantics/move_semantics6.rs" },
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" }, { name = "structs1", path = "../exercises/07_structs/structs1.rs" },
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" }, { name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" }, { name = "structs2", path = "../exercises/07_structs/structs2.rs" },

View file

@ -1,8 +1,9 @@
# Functions # Functions
Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even
in more complex code. in more complex code. You will also learn what is the difference with closures.
## Further information ## Further information
- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html) - [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)
- [Closures](https://doc.rust-lang.org/book/ch13-01-closures.html)

View file

@ -0,0 +1,19 @@
// functions6.rs
//
// Here you can practice special functions called `closures`, that can capture
// variables of their parent context.
// Fix the code below to make it compile, without changing the two closure
// definitions.
//
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for
// some hints.
fn main() {
// TODO: ensure the definition of captured variable
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
println!("Closure#1 returns {}", closure_1(5));
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
closure_2(2);
closure_2("5"); // TODO: look at the captured variable type here
}

View file

@ -0,0 +1,25 @@
// move_semantics6.rs
//
// Here you will practice how mutable/immutable borrowing works in the context
// of a closure.
//
// Try to fix this code to make it compile and not panic.
// You can't change anything except removing 1 line.
//
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
// for a hint.
fn main() {
let mut counter = 0;
let mut increment = || {
counter += 1;
println!("counter equals {}", counter);
};
increment();
let _reborrowed_counter = &counter; // TODO: figure out where to put this borrowing instruction
increment();
assert_eq!(counter, 2);
}

View file

@ -3,7 +3,7 @@
| Exercise | Book Chapter | | Exercise | Book Chapter |
| ---------------------- | ------------------- | | ---------------------- | ------------------- |
| variables | §3.1 | | variables | §3.1 |
| functions | §3.3 | | functions | §3.3, §13.1 |
| if | §3.5 | | if | §3.5 |
| primitive_types | §3.2, §4.3 | | primitive_types | §3.2, §4.3 |
| vecs | §8.1 | | vecs | §8.1 |

View file

@ -187,6 +187,20 @@ There are two solutions:
1. Add the `return` keyword before `num * num;` 1. Add the `return` keyword before `num * num;`
2. Remove the semicolon `;` after `num * num`""" 2. Remove the semicolon `;` after `num * num`"""
[[exercises]]
name = "functions6"
dir = "02_functions"
test = false
hint = """
Hint FIX #1: Closures can capture variables defined in the outer context.
Hint FIX #2: Closures can infer both input and returned types, when they are not
specified in the signature. But the closure cannot be reused with different
input types.
Read more about closures in the rust book dedicated section:
https://doc.rust-lang.org/book/ch13-01-closures.html"""
# IF # IF
[[exercises]] [[exercises]]
@ -391,6 +405,18 @@ The first problem is that `get_char` is taking ownership of the string. So
Once you've fixed that, `string_uppercase`'s function signature will also need Once you've fixed that, `string_uppercase`'s function signature will also need
to be adjusted.""" to be adjusted."""
[[exercises]]
name = "move_semantics6"
dir = "06_move_semantics"
test = false
hint = """
When a closure captures a variable to modify it, it actually borrows that variable
as a mutable reference. In this exercise, the closure mutably borrows the `counter`
variable, thus, any attempt to borrow `counter` between closure calls leads to an error.
You cannot immutably borrow a variable if a mutable closure is
called later in the scope."""
# STRUCTS # STRUCTS
[[exercises]] [[exercises]]

View file

@ -0,0 +1,9 @@
fn main() {
let outer_var = 1;
let closure_1 = |input_var: u32| -> u32 { input_var + outer_var };
println!("Closure#1 returns {}", closure_1(5));
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
closure_2(2);
closure_2(5);
}

View file

@ -0,0 +1,14 @@
fn main() {
let mut counter = 0;
let mut increment = || {
counter += 1;
println!("counter equals {}", counter);
};
increment();
increment();
let _reborrowed_counter = &counter;
assert_eq!(counter, 2);
}

View file

@ -1,10 +1,15 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Error, Result};
use crossterm::{cursor, terminal, QueueableCommand};
use std::{ use std::{
env, env,
fs::{File, OpenOptions}, fs::{File, OpenOptions},
io::{self, Read, Seek, StdoutLock, Write}, io::{Read, Seek, StdoutLock, Write},
path::{Path, MAIN_SEPARATOR_STR}, path::{Path, MAIN_SEPARATOR_STR},
process::{Command, Stdio}, process::{Command, Stdio},
sync::{
atomic::{AtomicUsize, Ordering::Relaxed},
mpsc,
},
thread, thread,
}; };
@ -15,10 +20,11 @@ use crate::{
embedded::EMBEDDED_FILES, embedded::EMBEDDED_FILES,
exercise::{Exercise, RunnableExercise}, exercise::{Exercise, RunnableExercise},
info_file::ExerciseInfo, info_file::ExerciseInfo,
term, term::{self, CheckProgressVisualizer},
}; };
const STATE_FILE_NAME: &str = ".rustlings-state.txt"; const STATE_FILE_NAME: &str = ".rustlings-state.txt";
const DEFAULT_CHECK_PARALLELISM: usize = 8;
#[must_use] #[must_use]
pub enum ExercisesProgress { pub enum ExercisesProgress {
@ -35,10 +41,12 @@ pub enum StateFileStatus {
NotRead, NotRead,
} }
enum AllExercisesCheck { #[derive(Clone, Copy)]
Pending(usize), pub enum CheckProgress {
AllDone, None,
CheckedUntil(usize), Checking,
Done,
Pending,
} }
pub struct AppState { pub struct AppState {
@ -194,6 +202,11 @@ impl AppState {
self.n_done self.n_done
} }
#[inline]
pub fn n_pending(&self) -> u16 {
self.exercises.len() as u16 - self.n_done
}
#[inline] #[inline]
pub fn current_exercise(&self) -> &Exercise { pub fn current_exercise(&self) -> &Exercise {
&self.exercises[self.current_exercise_ind] &self.exercises[self.current_exercise_ind]
@ -270,15 +283,31 @@ impl AppState {
self.write() 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<bool> {
let exercise = self let exercise = self
.exercises .exercises
.get_mut(exercise_ind) .get_mut(exercise_ind)
.context(BAD_INDEX_ERR)?; .context(BAD_INDEX_ERR)?;
if exercise.done { if exercise.done == done {
exercise.done = false; return Ok(false);
}
exercise.done = done;
if done {
self.n_done += 1;
} else {
self.n_done -= 1; 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()?; self.write()?;
} }
@ -379,63 +408,114 @@ 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>> {
fn check_all_exercises(&self, stdout: &mut StdoutLock) -> Result<Option<usize>> { let term_width = terminal::size()
stdout.write_all(FINAL_CHECK_MSG)?; .context("Failed to get the terminal size")?
let n_exercises = self.exercises.len(); .0;
let mut progress_visualizer = CheckProgressVisualizer::build(stdout, term_width)?;
let status = thread::scope(|s| { let next_exercise_ind = AtomicUsize::new(0);
let handles = self let mut progresses = vec![CheckProgress::None; self.exercises.len()];
.exercises
.iter()
.map(|exercise| {
thread::Builder::new()
.spawn_scoped(s, || exercise.run_exercise(None, &self.cmd_runner))
})
.collect::<Vec<_>>();
for (exercise_ind, spawn_res) in handles.into_iter().enumerate() { thread::scope(|s| {
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?; let (exercise_progress_sender, exercise_progress_receiver) = mpsc::channel();
stdout.flush()?; let n_threads = thread::available_parallelism()
.map_or(DEFAULT_CHECK_PARALLELISM, |count| count.get());
let Ok(handle) = spawn_res else { for _ in 0..n_threads {
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind)); 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 { if exercise_progress_sender
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind)); .send((exercise_ind, CheckProgress::Checking))
}; .is_err()
{
break;
};
if !success { let success = exercise.run_exercise(None, &slf.cmd_runner);
return Ok(AllExercisesCheck::Pending(exercise_ind)); 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 { let mut first_pending_exercise_ind = None;
AllExercisesCheck::Pending(exercise_ind) => return Ok(Some(exercise_ind)), for exercise_ind in 0..progresses.len() {
AllExercisesCheck::AllDone => return Ok(None), match progresses[exercise_ind] {
AllExercisesCheck::CheckedUntil(ind) => 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. let exercise = &self.exercises[exercise_ind];
// This could be because we exceeded the limit of open file descriptors. let success = exercise.run_exercise(None, &self.cmd_runner)?;
// Therefore, try to continue the check sequentially. if success {
for exercise in &self.exercises[exercise_ind..] { progresses[exercise_ind] = CheckProgress::Done;
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?; } else {
stdout.flush()?; progresses[exercise_ind] = CheckProgress::Pending;
if first_pending_exercise_ind.is_none() {
let success = exercise.run_exercise(None, &self.cmd_runner)?; first_pending_exercise_ind = Some(exercise_ind);
if !success { }
return Ok(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<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.
@ -462,20 +542,18 @@ impl AppState {
stdout.write_all(b"\n")?; stdout.write_all(b"\n")?;
} }
if let Some(pending_exercise_ind) = self.check_all_exercises(stdout)? { if let Some(first_pending_exercise_ind) = self.check_all_exercises(stdout)? {
stdout.write_all(b"\n\n")?; 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); return Ok(ExercisesProgress::NewPending);
} }
// Write that the last exercise is done. self.render_final_message(stdout)?;
self.write()?;
Ok(ExercisesProgress::AllDone)
}
pub fn render_final_message(&self, stdout: &mut StdoutLock) -> Result<()> {
clear_terminal(stdout)?; clear_terminal(stdout)?;
stdout.write_all(FENISH_LINE.as_bytes())?; stdout.write_all(FENISH_LINE.as_bytes())?;
@ -485,15 +563,12 @@ impl AppState {
stdout.write_all(b"\n")?; 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 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 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 = "+----------------------------------------------------+ const FENISH_LINE: &str = "+----------------------------------------------------+
| You made it to the Fe-nish line! | | You made it to the Fe-nish line! |
+-------------------------- ------------------------+ +-------------------------- ------------------------+

View file

@ -4,7 +4,7 @@ use clap::{Parser, Subcommand};
use std::{ use std::{
io::{self, IsTerminal, Write}, io::{self, IsTerminal, Write},
path::Path, path::Path,
process::exit, process::ExitCode,
}; };
use term::{clear_terminal, press_enter_prompt}; use term::{clear_terminal, press_enter_prompt};
@ -47,6 +47,8 @@ enum Subcommands {
/// The name of the exercise /// The name of the exercise
name: Option<String>, name: Option<String>,
}, },
/// Check all the exercises, marking them as done or pending accordingly.
CheckAll,
/// Reset a single exercise /// Reset a single exercise
Reset { Reset {
/// The name of the exercise /// The name of the exercise
@ -62,22 +64,26 @@ enum Subcommands {
Dev(DevCommands), Dev(DevCommands),
} }
fn main() -> Result<()> { fn main() -> Result<ExitCode> {
let args = Args::parse(); let args = Args::parse();
if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() { if cfg!(not(debug_assertions)) && Path::new("dev/rustlings-repo.txt").exists() {
bail!("{OLD_METHOD_ERR}"); bail!("{OLD_METHOD_ERR}");
} }
match args.command { 'priority_cmd: {
Some(Subcommands::Init) => return init::init().context("Initialization failed"), match args.command {
Some(Subcommands::Dev(dev_command)) => return dev_command.run(), 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() { if !Path::new("exercises").is_dir() {
println!("{PRE_INIT_MSG}"); println!("{PRE_INIT_MSG}");
exit(1); return Ok(ExitCode::FAILURE);
} }
let info_file = InfoFile::parse()?; let info_file = InfoFile::parse()?;
@ -136,7 +142,35 @@ fn main() -> Result<()> {
if let Some(name) = name { if let Some(name) = name {
app_state.set_current_exercise_by_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 }) => { Some(Subcommands::Reset { name }) => {
app_state.set_current_exercise_by_name(&name)?; app_state.set_current_exercise_by_name(&name)?;
@ -153,7 +187,7 @@ fn main() -> Result<()> {
Some(Subcommands::Init | Subcommands::Dev(_)) => (), Some(Subcommands::Init | Subcommands::Dev(_)) => (),
} }
Ok(()) Ok(ExitCode::SUCCESS)
} }
const OLD_METHOD_ERR: &str = const OLD_METHOD_ERR: &str =

View file

@ -5,7 +5,7 @@ use crossterm::{
}; };
use std::{ use std::{
io::{self, Write}, io::{self, Write},
process::exit, process::ExitCode,
}; };
use crate::{ use crate::{
@ -13,7 +13,7 @@ use crate::{
exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY}, exercise::{solution_link_line, RunnableExercise, OUTPUT_CAPACITY},
}; };
pub fn run(app_state: &mut AppState) -> Result<()> { pub fn run(app_state: &mut AppState) -> Result<ExitCode> {
let exercise = app_state.current_exercise(); let exercise = app_state.current_exercise();
let mut output = Vec::with_capacity(OUTPUT_CAPACITY); let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
let success = exercise.run_exercise(Some(&mut output), app_state.cmd_runner())?; 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() .current_exercise()
.terminal_file_link(&mut stdout)?; .terminal_file_link(&mut stdout)?;
stdout.write_all(b" with errors\n")?; stdout.write_all(b" with errors\n")?;
exit(1);
return Ok(ExitCode::FAILURE);
} }
stdout.queue(SetForegroundColor(Color::Green))?; stdout.queue(SetForegroundColor(Color::Green))?;
@ -55,5 +56,5 @@ pub fn run(app_state: &mut AppState) -> Result<()> {
ExercisesProgress::AllDone => (), ExercisesProgress::AllDone => (),
} }
Ok(()) Ok(ExitCode::SUCCESS)
} }

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,6 +9,8 @@ use std::{
io::{self, BufRead, StdoutLock, Write}, io::{self, BufRead, StdoutLock, Write},
}; };
use crate::app_state::CheckProgress;
pub struct MaxLenWriter<'a, 'b> { pub struct MaxLenWriter<'a, 'b> {
pub stdout: &'a mut StdoutLock<'b>, pub stdout: &'a mut StdoutLock<'b>,
len: usize, 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<Self> {
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>( pub fn progress_bar<'a>(
writer: &mut impl CountedWrite<'a>, writer: &mut impl CountedWrite<'a>,
progress: u16, progress: u16,
total: u16, total: u16,
line_width: u16, term_width: u16,
) -> io::Result<()> { ) -> io::Result<()> {
debug_assert!(total < 1000); debug_assert!(total < 1000);
debug_assert!(progress <= total); debug_assert!(progress <= total);
@ -101,7 +175,7 @@ pub fn progress_bar<'a>(
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; 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: ")?; writer.write_ascii(b"Progress: ")?;
// Integers are in ASCII. // Integers are in ASCII.
return writer.write_ascii(format!("{progress}/{total}").as_bytes()); return writer.write_ascii(format!("{progress}/{total}").as_bytes());
@ -110,7 +184,7 @@ pub fn progress_bar<'a>(
let stdout = writer.stdout(); let stdout = writer.stdout();
stdout.write_all(PREFIX)?; stdout.write_all(PREFIX)?;
let width = line_width - WRAPPER_WIDTH; let width = term_width - WRAPPER_WIDTH;
let filled = (width * progress) / total; let filled = (width * progress) / total;
stdout.queue(SetForegroundColor(Color::Green))?; stdout.queue(SetForegroundColor(Color::Green))?;

View file

@ -103,6 +103,13 @@ fn run_watch(
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?, WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?,
WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?, WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?,
WatchEvent::Input(InputEvent::List) => return Ok(WatchExit::List), 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::Reset) => watch_state.reset_exercise(&mut stdout)?,
WatchEvent::Input(InputEvent::Quit) => { WatchEvent::Input(InputEvent::Quit) => {
stdout.write_all(QUIT_MSG)?; stdout.write_all(QUIT_MSG)?;

View file

@ -157,8 +157,9 @@ impl<'a> WatchState<'a> {
/// Move on to the next exercise if the current one is done. /// Move on to the next exercise if the current one is done.
pub fn next_exercise(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> { pub fn next_exercise(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> {
if self.done_status == DoneStatus::Pending { match self.done_status {
return Ok(ExercisesProgress::CurrentPending); DoneStatus::DoneWithSolution(_) | DoneStatus::DoneWithoutSolution => (),
DoneStatus::Pending => return Ok(ExercisesProgress::CurrentPending),
} }
self.app_state.done_current_exercise::<true>(stdout) self.app_state.done_current_exercise::<true>(stdout)
@ -195,6 +196,11 @@ impl<'a> WatchState<'a> {
stdout.queue(ResetColor)?; stdout.queue(ResetColor)?;
stdout.write_all(b":list / ")?; 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.queue(SetAttribute(Attribute::Bold))?;
stdout.write_all(b"x")?; stdout.write_all(b"x")?;
stdout.queue(ResetColor)?; stdout.queue(ResetColor)?;
@ -274,6 +280,22 @@ impl<'a> WatchState<'a> {
Ok(()) Ok(())
} }
pub fn check_all_exercises(&mut self, stdout: &mut StdoutLock) -> Result<ExercisesProgress> {
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<()> { pub fn update_term_width(&mut self, width: u16, stdout: &mut StdoutLock) -> io::Result<()> {
if self.term_width != width { if self.term_width != width {
self.term_width = width; self.term_width = width;

View file

@ -11,6 +11,7 @@ pub enum InputEvent {
Run, Run,
Hint, Hint,
List, List,
CheckAll,
Reset, Reset,
Quit, Quit,
} }
@ -37,6 +38,7 @@ pub fn terminal_event_handler(
KeyCode::Char('r') if manual_run => InputEvent::Run, KeyCode::Char('r') if manual_run => InputEvent::Run,
KeyCode::Char('h') => InputEvent::Hint, KeyCode::Char('h') => InputEvent::Hint,
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List), KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
KeyCode::Char('c') => InputEvent::CheckAll,
KeyCode::Char('x') => { KeyCode::Char('x') => {
if sender.send(WatchEvent::Input(InputEvent::Reset)).is_err() { if sender.send(WatchEvent::Input(InputEvent::Reset)).is_err() {
return; return;