Skip to content

Commit

Permalink
fix: watch stdout (#163)
Browse files Browse the repository at this point in the history
* fix: command line prints
* Update main.rs
  • Loading branch information
shramee authored Nov 3, 2023
1 parent 0d7aec1 commit a702f02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ enum WatchStatus {
Unfinished,
}

fn watch(exercises: &[Exercise]) -> notify::Result<WatchStatus> {
/* Clears the terminal with an ANSI escape code.
Works in UNIX and newer Windows terminals. */
fn clear_screen() {
println!("\x1Bc");
}
// Clears the terminal with an ANSI escape code.
// Works in UNIX and newer Windows terminals.
pub fn clear_screen() {
println!("\x1Bc");
}

fn watch(exercises: &[Exercise]) -> notify::Result<WatchStatus> {
let (tx, rx) = channel();
let should_quit = Arc::new(AtomicBool::new(false));

Expand Down Expand Up @@ -419,7 +419,6 @@ fn watch(exercises: &[Exercise]) -> notify::Result<WatchStatus> {
.filter(|e| !e.looks_done() && !filepath.ends_with(&e.path)),
);
let num_done = exercises.iter().filter(|e| e.looks_done()).count();
clear_screen();
match verify(pending_exercises, (num_done, exercises.len())) {
Ok(_) => return Ok(WatchStatus::Finished),
Err(exercise) => {
Expand Down
24 changes: 14 additions & 10 deletions src/verify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::exercise::{Exercise, Mode, State};
use crate::{
clear_screen,
exercise::{Exercise, Mode, State},
};
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use std::env;
Expand All @@ -12,15 +15,16 @@ pub fn verify<'a>(
exercises: impl IntoIterator<Item = &'a Exercise>,
progress: (usize, usize),
) -> Result<(), &'a Exercise> {
let (num_done, total) = progress;
let bar = ProgressBar::new(total as u64);
bar.set_style(
ProgressStyle::default_bar()
.template("Progress: [{bar:60.green/red}] {pos}/{len} {msg}\n")
.progress_chars("#>-"),
);
bar.set_position(num_done as u64);
let (mut num_done, total) = progress;
for exercise in exercises {
clear_screen();
let bar = ProgressBar::new(total as u64);
bar.set_style(
ProgressStyle::default_bar()
.template("Progress: [{bar:60.green/red}] {pos}/{len} {msg}\n")
.progress_chars("#>-"),
);
bar.set_position(num_done as u64);
let compile_result = match exercise.mode {
Mode::Compile => compile_and_run_interactively(exercise),
Mode::Test => compile_and_test_interactively(exercise),
Expand All @@ -30,7 +34,7 @@ pub fn verify<'a>(
}
let percentage = num_done as f32 / total as f32 * 100.0;
bar.set_message(format!("({percentage:.1} %)"));
bar.inc(1);
num_done += 1;
}
Ok(())
}
Expand Down

0 comments on commit a702f02

Please sign in to comment.