1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
use anyhow::{Error, Result};
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
use notify_debouncer_mini::{
new_debouncer,
notify::{self, RecursiveMode},
DebounceEventResult, DebouncedEventKind,
};
use std::{
io::{self, Write},
path::Path,
sync::mpsc::{channel, Sender},
thread,
time::Duration,
};
mod state;
use crate::{exercise::Exercise, state_file::StateFile};
use self::state::WatchState;
/// Returned by the watch mode to indicate what to do afterwards.
pub enum WatchExit {
/// Exit the program.
Shutdown,
/// Enter the list mode and restart the watch mode afterwards.
List,
}
enum InputEvent {
Hint,
List,
Quit,
Unrecognized(String),
}
enum WatchEvent {
Input(InputEvent),
FileChange { exercise_ind: usize },
NotifyErr(notify::Error),
TerminalEventErr(io::Error),
TerminalResize,
}
struct DebounceEventHandler {
tx: Sender<WatchEvent>,
exercises: &'static [Exercise],
}
impl notify_debouncer_mini::DebounceEventHandler for DebounceEventHandler {
fn handle_event(&mut self, event: DebounceEventResult) {
let event = match event {
Ok(event) => {
let Some(exercise_ind) = event
.iter()
.filter_map(|event| {
if event.kind != DebouncedEventKind::Any
|| !event.path.extension().is_some_and(|ext| ext == "rs")
{
return None;
}
self.exercises
.iter()
.position(|exercise| event.path.ends_with(&exercise.path))
})
.min()
else {
return;
};
WatchEvent::FileChange { exercise_ind }
}
Err(e) => WatchEvent::NotifyErr(e),
};
// An error occurs when the receiver is dropped.
// After dropping the receiver, the debouncer guard should also be dropped.
let _ = self.tx.send(event);
}
}
fn terminal_event_handler(tx: Sender<WatchEvent>) {
let mut input = String::with_capacity(8);
let last_input_event = loop {
let terminal_event = match event::read() {
Ok(v) => v,
Err(e) => {
// If `send` returns an error, then the receiver is dropped and
// a shutdown has been already initialized.
let _ = tx.send(WatchEvent::TerminalEventErr(e));
return;
}
};
match terminal_event {
Event::Key(key) => {
match key.kind {
KeyEventKind::Release => continue,
KeyEventKind::Press | KeyEventKind::Repeat => (),
}
match key.code {
KeyCode::Enter => {
let input_event = match input.trim() {
"h" | "hint" => InputEvent::Hint,
"l" | "list" => break InputEvent::List,
"q" | "quit" => break InputEvent::Quit,
_ => InputEvent::Unrecognized(input.clone()),
};
if tx.send(WatchEvent::Input(input_event)).is_err() {
return;
}
input.clear();
}
KeyCode::Char(c) => {
input.push(c);
}
_ => (),
}
}
Event::Resize(_, _) => {
if tx.send(WatchEvent::TerminalResize).is_err() {
return;
}
}
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue,
}
};
let _ = tx.send(WatchEvent::Input(last_input_event));
}
pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<WatchExit> {
let (tx, rx) = channel();
let mut debouncer = new_debouncer(
Duration::from_secs(1),
DebounceEventHandler {
tx: tx.clone(),
exercises,
},
)?;
debouncer
.watcher()
.watch(Path::new("exercises"), RecursiveMode::Recursive)?;
let mut watch_state = WatchState::new(state_file, exercises);
// TODO: bool
watch_state.run_exercise()?;
watch_state.render()?;
thread::spawn(move || terminal_event_handler(tx));
while let Ok(event) = rx.recv() {
match event {
WatchEvent::Input(InputEvent::Hint) => {
watch_state.show_hint()?;
}
WatchEvent::Input(InputEvent::List) => {
return Ok(WatchExit::List);
}
WatchEvent::TerminalResize => {
watch_state.render()?;
}
WatchEvent::Input(InputEvent::Quit) => break,
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
watch_state.handle_invalid_cmd(&cmd)?;
}
WatchEvent::FileChange { exercise_ind } => {
// TODO: bool
watch_state.run_exercise_with_ind(exercise_ind)?;
watch_state.render()?;
}
WatchEvent::NotifyErr(e) => {
return Err(Error::from(e).context("Exercise file watcher failed"))
}
WatchEvent::TerminalEventErr(e) => {
return Err(Error::from(e).context("Terminal event listener failed"))
}
}
}
watch_state.into_writer().write_all(b"
We hope you're enjoying learning Rust!
If you want to continue working on the exercises at a later point, you can simply run `rustlings` again.
")?;
Ok(WatchExit::Shutdown)
}
|