2024-04-09 20:07:53 +01:00
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
use notify_debouncer_mini::{
|
|
|
|
new_debouncer, notify::RecursiveMode, DebounceEventResult, DebouncedEventKind,
|
|
|
|
};
|
2024-04-07 00:17:53 +01:00
|
|
|
use std::{
|
2024-04-07 18:29:16 +01:00
|
|
|
io::{self, BufRead, Write},
|
2024-04-07 00:17:53 +01:00
|
|
|
path::Path,
|
2024-04-09 20:07:53 +01:00
|
|
|
sync::mpsc::{channel, Sender},
|
2024-04-07 00:17:53 +01:00
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
2024-04-07 18:29:16 +01:00
|
|
|
mod state;
|
|
|
|
|
|
|
|
use crate::{exercise::Exercise, state_file::StateFile};
|
|
|
|
|
|
|
|
use self::state::WatchState;
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
enum InputEvent {
|
2024-04-07 00:17:53 +01:00
|
|
|
Hint,
|
|
|
|
Clear,
|
|
|
|
Quit,
|
2024-04-09 20:07:53 +01:00
|
|
|
Unrecognized,
|
2024-04-07 00:17:53 +01:00
|
|
|
}
|
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
enum WatchEvent {
|
|
|
|
Input(InputEvent),
|
|
|
|
FileChange { exercise_ind: usize },
|
|
|
|
TerminalResize,
|
|
|
|
}
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
struct DebouceEventHandler {
|
|
|
|
tx: Sender<WatchEvent>,
|
|
|
|
exercises: &'static [Exercise],
|
|
|
|
}
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
impl notify_debouncer_mini::DebounceEventHandler for DebouceEventHandler {
|
|
|
|
fn handle_event(&mut self, event: DebounceEventResult) {
|
|
|
|
let Ok(event) = event else {
|
|
|
|
// TODO
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
self.exercises
|
|
|
|
.iter()
|
|
|
|
.position(|exercise| event.path.ends_with(&exercise.path))
|
|
|
|
})
|
|
|
|
.min()
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
self.tx.send(WatchEvent::FileChange { exercise_ind });
|
|
|
|
}
|
|
|
|
}
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
fn input_handler(tx: Sender<WatchEvent>) -> Result<()> {
|
|
|
|
let mut stdin = io::stdin().lock();
|
|
|
|
let mut stdin_buf = String::with_capacity(8);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
stdin
|
|
|
|
.read_line(&mut stdin_buf)
|
|
|
|
.context("Failed to read the user's input from stdin")?;
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
let event = match stdin_buf.trim() {
|
|
|
|
"h" | "hint" => InputEvent::Hint,
|
|
|
|
"c" | "clear" => InputEvent::Clear,
|
|
|
|
"q" | "quit" => InputEvent::Quit,
|
|
|
|
_ => InputEvent::Unrecognized,
|
|
|
|
};
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
stdin_buf.clear();
|
|
|
|
|
|
|
|
if tx.send(WatchEvent::Input(event)).is_err() {
|
|
|
|
return Ok(());
|
2024-04-07 00:17:53 +01:00
|
|
|
}
|
2024-04-09 20:07:53 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
pub fn watch(state_file: &StateFile, exercises: &'static [Exercise]) -> Result<()> {
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let mut debouncer = new_debouncer(
|
|
|
|
Duration::from_secs(1),
|
|
|
|
DebouceEventHandler {
|
|
|
|
tx: tx.clone(),
|
|
|
|
exercises,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
debouncer
|
|
|
|
.watcher()
|
|
|
|
.watch(Path::new("exercises"), RecursiveMode::Recursive)?;
|
2024-04-07 00:17:53 +01:00
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
let mut watch_state = WatchState::new(state_file, exercises);
|
|
|
|
|
|
|
|
// TODO: bool
|
|
|
|
watch_state.run_exercise()?;
|
|
|
|
watch_state.render()?;
|
|
|
|
|
|
|
|
let input_thread = thread::spawn(move || input_handler(tx));
|
|
|
|
|
|
|
|
while let Ok(event) = rx.recv() {
|
|
|
|
match event {
|
|
|
|
WatchEvent::Input(InputEvent::Hint) => {
|
|
|
|
watch_state.show_hint()?;
|
|
|
|
}
|
|
|
|
WatchEvent::Input(InputEvent::Clear) | WatchEvent::TerminalResize => {
|
|
|
|
watch_state.render()?;
|
|
|
|
}
|
|
|
|
WatchEvent::Input(InputEvent::Quit) => break,
|
|
|
|
WatchEvent::Input(InputEvent::Unrecognized) => {
|
|
|
|
watch_state.handle_invalid_cmd()?;
|
|
|
|
}
|
|
|
|
WatchEvent::FileChange { exercise_ind } => {
|
|
|
|
// TODO: bool
|
|
|
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
|
|
|
watch_state.render()?;
|
2024-04-07 00:17:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
// Drop the receiver for the sender threads to exit.
|
|
|
|
drop(rx);
|
|
|
|
|
2024-04-07 18:29:16 +01:00
|
|
|
watch_state.into_writer().write_all(b"
|
2024-04-07 00:17:53 +01:00
|
|
|
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.
|
|
|
|
")?;
|
|
|
|
|
2024-04-09 20:07:53 +01:00
|
|
|
match input_thread.join() {
|
|
|
|
Ok(res) => res?,
|
|
|
|
Err(_) => bail!("The input thread panicked"),
|
|
|
|
}
|
|
|
|
|
2024-04-07 00:17:53 +01:00
|
|
|
Ok(())
|
|
|
|
}
|