mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 00:00:05 +03:00
Show a progress bar when running check_all
Replace the "Progress: xxx/yyy" with a progress bar when checking all the exercises
This commit is contained in:
parent
e2f7734f37
commit
aa83fd6bc4
|
@ -1,4 +1,9 @@
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
|
use crossterm::{
|
||||||
|
queue,
|
||||||
|
style::{Print, ResetColor, SetForegroundColor},
|
||||||
|
terminal,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
fs::{File, OpenOptions},
|
fs::{File, OpenOptions},
|
||||||
|
@ -16,7 +21,7 @@ use crate::{
|
||||||
embedded::EMBEDDED_FILES,
|
embedded::EMBEDDED_FILES,
|
||||||
exercise::{Exercise, RunnableExercise},
|
exercise::{Exercise, RunnableExercise},
|
||||||
info_file::ExerciseInfo,
|
info_file::ExerciseInfo,
|
||||||
term,
|
term::{self, progress_bar_with_success},
|
||||||
};
|
};
|
||||||
|
|
||||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||||
|
@ -428,10 +433,16 @@ impl AppState {
|
||||||
// No more exercises
|
// No more exercises
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
if tx
|
|
||||||
.send((exercise_ind, exercise.run_exercise(None, &this.cmd_runner)))
|
// Notify the progress bar that this exercise is pending
|
||||||
.is_err()
|
if tx.send((exercise_ind, None)).is_err() {
|
||||||
{
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = exercise.run_exercise(None, &this.cmd_runner);
|
||||||
|
|
||||||
|
// Notify the progress bar that this exercise is done
|
||||||
|
if tx.send((exercise_ind, Some(result))).is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -443,28 +454,68 @@ impl AppState {
|
||||||
// there are `tx` clones, i.e. threads)
|
// there are `tx` clones, i.e. threads)
|
||||||
drop(tx);
|
drop(tx);
|
||||||
|
|
||||||
let mut results = vec![AllExercisesResult::Pending; n_exercises];
|
// Print the legend
|
||||||
let mut checked_count = 0;
|
queue!(
|
||||||
write!(stdout, "\rProgress: {checked_count}/{n_exercises}")?;
|
stdout,
|
||||||
stdout.flush()?;
|
Print("Color legend: "),
|
||||||
while let Ok((exercise_ind, result)) = rx.recv() {
|
SetForegroundColor(term::PROGRESS_FAILED_COLOR),
|
||||||
results[exercise_ind] = result.map_or_else(
|
Print("Failure"),
|
||||||
|_| AllExercisesResult::Error,
|
ResetColor,
|
||||||
|success| {
|
Print(" - "),
|
||||||
checked_count += 1;
|
SetForegroundColor(term::PROGRESS_SUCCESS_COLOR),
|
||||||
if success {
|
Print("Success"),
|
||||||
AllExercisesResult::Success
|
ResetColor,
|
||||||
} else {
|
Print(" - "),
|
||||||
AllExercisesResult::Failed
|
SetForegroundColor(term::PROGRESS_PENDING_COLOR),
|
||||||
}
|
Print("Checking"),
|
||||||
},
|
ResetColor,
|
||||||
);
|
Print("\n"),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
// We expect at least a few "pending" notifications shortly, so don't
|
||||||
|
// bother printing the initial state of the progress bar and flushing
|
||||||
|
// stdout
|
||||||
|
|
||||||
write!(stdout, "\rProgress: {checked_count}/{n_exercises}")?;
|
let line_width = terminal::size().unwrap().0;
|
||||||
|
let mut results = vec![AllExercisesResult::Pending; n_exercises];
|
||||||
|
let mut pending = 0;
|
||||||
|
let mut success = 0;
|
||||||
|
let mut failed = 0;
|
||||||
|
|
||||||
|
while let Ok((exercise_ind, result)) = rx.recv() {
|
||||||
|
match result {
|
||||||
|
None => {
|
||||||
|
pending += 1;
|
||||||
|
}
|
||||||
|
Some(Err(_)) => {
|
||||||
|
results[exercise_ind] = AllExercisesResult::Error;
|
||||||
|
}
|
||||||
|
Some(Ok(true)) => {
|
||||||
|
results[exercise_ind] = AllExercisesResult::Success;
|
||||||
|
pending -= 1;
|
||||||
|
success += 1;
|
||||||
|
}
|
||||||
|
Some(Ok(false)) => {
|
||||||
|
results[exercise_ind] = AllExercisesResult::Failed;
|
||||||
|
pending -= 1;
|
||||||
|
failed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(stdout, "\r").unwrap();
|
||||||
|
progress_bar_with_success(
|
||||||
|
stdout,
|
||||||
|
pending,
|
||||||
|
failed,
|
||||||
|
success,
|
||||||
|
n_exercises as u16,
|
||||||
|
line_width,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok::<_, io::Error>((checked_count, results))
|
Ok::<_, io::Error>((success, results))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// If we got an error while checking all exercises in parallel,
|
// If we got an error while checking all exercises in parallel,
|
||||||
|
|
75
src/term.rs
75
src/term.rs
|
@ -9,6 +9,10 @@ use std::{
|
||||||
io::{self, BufRead, StdoutLock, Write},
|
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;
|
||||||
|
|
||||||
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,15 +89,26 @@ impl<'a> CountedWrite<'a> for StdoutLock<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Terminal progress bar to be used when not using Ratataui.
|
/// 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,
|
||||||
line_width: u16,
|
line_width: u16,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
progress_bar_with_success(writer, 0, 0, progress, total, line_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,
|
||||||
|
line_width: u16,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
debug_assert!(total < 1000);
|
debug_assert!(total < 1000);
|
||||||
debug_assert!(progress <= total);
|
debug_assert!((pending + failed + success) <= 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;
|
||||||
|
@ -104,25 +119,67 @@ pub fn progress_bar<'a>(
|
||||||
if line_width < MIN_LINE_WIDTH {
|
if line_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!("{}/{total}", failed + success).as_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
let stdout = writer.stdout();
|
let stdout = writer.stdout();
|
||||||
stdout.write_all(PREFIX)?;
|
stdout.write_all(PREFIX)?;
|
||||||
|
|
||||||
let width = line_width - WRAPPER_WIDTH;
|
let width = line_width - WRAPPER_WIDTH;
|
||||||
let filled = (width * progress) / total;
|
let mut failed_end = (width * failed) / total;
|
||||||
|
let mut success_end = (width * (failed + success)) / total;
|
||||||
|
let mut pending_end = (width * (failed + success + pending)) / total;
|
||||||
|
|
||||||
stdout.queue(SetForegroundColor(Color::Green))?;
|
// In case the range boundaries overlap, "pending" has priority over both
|
||||||
for _ in 0..filled {
|
// "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.write_all(b"#")?;
|
stdout.write_all(b"#")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if filled < width {
|
if pending > 0 {
|
||||||
|
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 - filled;
|
let width_minus_filled = width - pending_end;
|
||||||
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))?;
|
||||||
|
@ -133,7 +190,7 @@ pub fn progress_bar<'a>(
|
||||||
|
|
||||||
stdout.queue(SetForegroundColor(Color::Reset))?;
|
stdout.queue(SetForegroundColor(Color::Reset))?;
|
||||||
|
|
||||||
write!(stdout, "] {progress:>3}/{total}")
|
write!(stdout, "] {:>3}/{}", failed + success, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue