@@ -6,9 +6,12 @@ use notify::DebouncedEvent;
6
6
use notify:: { RecommendedWatcher , RecursiveMode , Watcher } ;
7
7
use std:: ffi:: OsStr ;
8
8
use std:: fs;
9
+ use std:: io;
9
10
use std:: path:: Path ;
10
11
use std:: process:: { Command , Stdio } ;
11
12
use std:: sync:: mpsc:: channel;
13
+ use std:: sync:: { Arc , Mutex } ;
14
+ use std:: thread;
12
15
use std:: time:: Duration ;
13
16
14
17
mod exercise;
@@ -108,6 +111,26 @@ fn main() {
108
111
}
109
112
}
110
113
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
+
111
134
fn watch ( exercises : & [ Exercise ] ) -> notify:: Result < ( ) > {
112
135
/* Clears the terminal with an ANSI escape code.
113
136
Works in UNIX and newer Windows terminals. */
@@ -121,8 +144,11 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
121
144
watcher. watch ( Path :: new ( "./exercises" ) , RecursiveMode :: Recursive ) ?;
122
145
123
146
clear_screen ( ) ;
124
- let _ignored = verify ( exercises. iter ( ) ) ;
147
+ let verify_result = verify ( exercises. iter ( ) ) ;
125
148
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) ;
126
152
loop {
127
153
match rx. recv ( ) {
128
154
Ok ( event) => match event {
@@ -133,7 +159,9 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
133
159
. iter ( )
134
160
. skip_while ( |e| !filepath. ends_with ( & e. path ) ) ;
135
161
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 ( ) ;
137
165
}
138
166
}
139
167
_ => { }
0 commit comments