Skip to content

Commit 79aa062

Browse files
author
yourname
committed
Auto merge of rust-lang#241 - vyaslav:master, r=fmoko
feat(watch): show hint while watching `rustlings hint ...` command is not convenient when doing exercises with `rustlings watch`. This PR makes it possible for user to type `hint` while running `watch` and get hint text for exercise which is currently failing. e.g. ```rust ... --> exercises/variables/variables1.rs:13:36 | 13 | println!("x has the value {}", x); | ^ not found in this scope error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0425`. type 'hint' to get help: hint Hint: The declaration on line 12 is missing a keyword that is needed in Rust to create a new variable binding. ```
2 parents 9805429 + a7f1cc5 commit 79aa062

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/main.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use notify::DebouncedEvent;
66
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
77
use std::ffi::OsStr;
88
use std::fs;
9+
use std::io;
910
use std::path::Path;
1011
use std::process::{Command, Stdio};
1112
use std::sync::mpsc::channel;
13+
use std::sync::{Arc, Mutex};
14+
use std::thread;
1215
use std::time::Duration;
1316

1417
mod exercise;
@@ -108,6 +111,26 @@ fn main() {
108111
}
109112
}
110113

114+
fn spawn_watch_shell(failed_exercise_hint: &Arc<Mutex<Option<String>>>) {
115+
let failed_exercise_hint = Arc::clone(failed_exercise_hint);
116+
println!("Type 'hint' to get help");
117+
thread::spawn(move || loop {
118+
let mut input = String::new();
119+
match io::stdin().read_line(&mut input) {
120+
Ok(_) => {
121+
if input.trim().eq("hint") {
122+
if let Some(hint) = &*failed_exercise_hint.lock().unwrap() {
123+
println!("{}", hint);
124+
}
125+
} else {
126+
println!("unknown command: {}", input);
127+
}
128+
}
129+
Err(error) => println!("error reading command: {}", error),
130+
}
131+
});
132+
}
133+
111134
fn watch(exercises: &[Exercise]) -> notify::Result<()> {
112135
/* Clears the terminal with an ANSI escape code.
113136
Works in UNIX and newer Windows terminals. */
@@ -121,8 +144,11 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
121144
watcher.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;
122145

123146
clear_screen();
124-
let _ignored = verify(exercises.iter());
147+
let verify_result = verify(exercises.iter());
125148

149+
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
150+
let failed_exercise_hint = Arc::new(Mutex::new(verify_result.map_err(to_owned_hint).err()));
151+
spawn_watch_shell(&failed_exercise_hint);
126152
loop {
127153
match rx.recv() {
128154
Ok(event) => match event {
@@ -133,7 +159,9 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
133159
.iter()
134160
.skip_while(|e| !filepath.ends_with(&e.path));
135161
clear_screen();
136-
let _ignored = verify(pending_exercises);
162+
let verify_result = verify(pending_exercises);
163+
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();
164+
*failed_exercise_hint = verify_result.map_err(to_owned_hint).err();
137165
}
138166
}
139167
_ => {}

src/verify.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use crate::exercise::{Exercise, Mode, State};
22
use console::{style, Emoji};
33
use indicatif::ProgressBar;
44

5-
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), ()> {
5+
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), &'a Exercise> {
66
for exercise in start_at {
7-
let is_done = match exercise.mode {
8-
Mode::Test => compile_and_test_interactively(&exercise)?,
9-
Mode::Compile => compile_only(&exercise)?,
7+
let compile_result = match exercise.mode {
8+
Mode::Test => compile_and_test_interactively(&exercise),
9+
Mode::Compile => compile_only(&exercise),
1010
};
11-
if !is_done {
12-
return Err(());
11+
if !compile_result.unwrap_or(false) {
12+
return Err(exercise);
1313
}
1414
}
1515
Ok(())

0 commit comments

Comments
 (0)