Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watch): show hint while watching #241

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use notify::DebouncedEvent;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

mod exercise;
Expand Down Expand Up @@ -108,6 +111,26 @@ fn main() {
}
}

fn spawn_watch_shell(failed_exercise_hint: &Arc<Mutex<Option<String>>>) {
vyaslav marked this conversation as resolved.
Show resolved Hide resolved
let failed_exercise_hint = Arc::clone(failed_exercise_hint);
println!("Type 'hint' to get help");
thread::spawn(move || loop {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
if input.trim().eq("hint") {
if let Some(hint) = &*failed_exercise_hint.lock().unwrap() {
println!("{}", hint);
}
} else {
println!("unknown command: {}", input);
}
}
Err(error) => println!("error reading command: {}", error),
}
});
}

fn watch(exercises: &[Exercise]) -> notify::Result<()> {
/* Clears the terminal with an ANSI escape code.
Works in UNIX and newer Windows terminals. */
Expand All @@ -121,8 +144,11 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
watcher.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;

clear_screen();
let _ignored = verify(exercises.iter());
let verify_result = verify(exercises.iter());

let to_owned_hint = |t: &Exercise| t.hint.to_owned();
let failed_exercise_hint = Arc::new(Mutex::new(verify_result.map_err(to_owned_hint).err()));
spawn_watch_shell(&failed_exercise_hint);
loop {
match rx.recv() {
Ok(event) => match event {
Expand All @@ -133,7 +159,9 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
.iter()
.skip_while(|e| !filepath.ends_with(&e.path));
clear_screen();
let _ignored = verify(pending_exercises);
let verify_result = verify(pending_exercises);
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
*failed_exercise_hint = verify_result.map_err(to_owned_hint).err();
}
}
_ => {}
Expand Down
12 changes: 6 additions & 6 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::exercise::{Exercise, Mode, State};
use console::{style, Emoji};
use indicatif::ProgressBar;

pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), ()> {
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), &'a Exercise> {
for exercise in start_at {
let is_done = match exercise.mode {
Mode::Test => compile_and_test_interactively(&exercise)?,
Mode::Compile => compile_only(&exercise)?,
let compile_result = match exercise.mode {
Mode::Test => compile_and_test_interactively(&exercise),
Mode::Compile => compile_only(&exercise),
};
if !is_done {
return Err(());
if !compile_result.unwrap_or(false) {
return Err(exercise);
}
}
Ok(())
Expand Down