summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/app_state.rs93
-rw-r--r--src/term.rs75
2 files changed, 138 insertions, 30 deletions
diff --git a/src/app_state.rs b/src/app_state.rs
index ec79188..f4cc180 100644
--- a/src/app_state.rs
+++ b/src/app_state.rs
@@ -1,4 +1,9 @@
use anyhow::{bail, Context, Result};
+use crossterm::{
+ queue,
+ style::{Print, ResetColor, SetForegroundColor},
+ terminal,
+};
use std::{
env,
fs::{File, OpenOptions},
@@ -16,7 +21,7 @@ use crate::{
embedded::EMBEDDED_FILES,
exercise::{Exercise, RunnableExercise},
info_file::ExerciseInfo,
- term,
+ term::{self, progress_bar_with_success},
};
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
@@ -428,10 +433,16 @@ impl AppState {
// No more exercises
break;
};
- if tx
- .send((exercise_ind, exercise.run_exercise(None, &this.cmd_runner)))
- .is_err()
- {
+
+ // Notify the progress bar that this exercise is pending
+ 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;
}
}
@@ -443,28 +454,68 @@ impl AppState {
// there are `tx` clones, i.e. threads)
drop(tx);
+ // Print the legend
+ queue!(
+ stdout,
+ Print("Color legend: "),
+ SetForegroundColor(term::PROGRESS_FAILED_COLOR),
+ Print("Failure"),
+ ResetColor,
+ Print(" - "),
+ SetForegroundColor(term::PROGRESS_SUCCESS_COLOR),
+ Print("Success"),
+ ResetColor,
+ Print(" - "),
+ 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
+
+ let line_width = terminal::size().unwrap().0;
let mut results = vec![AllExercisesResult::Pending; n_exercises];
- let mut checked_count = 0;
- write!(stdout, "\rProgress: {checked_count}/{n_exercises}")?;
- stdout.flush()?;
+ let mut pending = 0;
+ let mut success = 0;
+ let mut failed = 0;
+
while let Ok((exercise_ind, result)) = rx.recv() {
- results[exercise_ind] = result.map_or_else(
- |_| AllExercisesResult::Error,
- |success| {
- checked_count += 1;
- if success {
- AllExercisesResult::Success
- } else {
- AllExercisesResult::Failed
- }
- },
- );
+ 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, "\rProgress: {checked_count}/{n_exercises}")?;
+ write!(stdout, "\r").unwrap();
+ progress_bar_with_success(
+ stdout,
+ pending,
+ failed,
+ success,
+ n_exercises as u16,
+ line_width,
+ )
+ .unwrap();
stdout.flush()?;
}
- Ok::<_, io::Error>((checked_count, results))
+ Ok::<_, io::Error>((success, results))
})?;
// If we got an error while checking all exercises in parallel,
diff --git a/src/term.rs b/src/term.rs
index 5b557ec..67ace4b 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -9,6 +9,10 @@ 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;
+
pub struct MaxLenWriter<'a, 'b> {
pub stdout: &'a mut StdoutLock<'b>,
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>(
writer: &mut impl CountedWrite<'a>,
progress: u16,
total: 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<()> {
debug_assert!(total < 1000);
- debug_assert!(progress <= total);
+ debug_assert!((pending + failed + success) <= total);
const PREFIX: &[u8] = b"Progress: [";
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
@@ -104,25 +119,67 @@ pub fn progress_bar<'a>(
if line_width < MIN_LINE_WIDTH {
writer.write_ascii(b"Progress: ")?;
// 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();
stdout.write_all(PREFIX)?;
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;
+
+ // 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;
+ }
- stdout.queue(SetForegroundColor(Color::Green))?;
- for _ in 0..filled {
+ // 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"#")?;
}
- 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">")?;
}
- let width_minus_filled = width - filled;
+ let width_minus_filled = width - pending_end;
if width_minus_filled > 1 {
let red_part_width = width_minus_filled - 1;
stdout.queue(SetForegroundColor(Color::Red))?;
@@ -133,7 +190,7 @@ pub fn progress_bar<'a>(
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<()> {