rustlings/src/watch.rs

100 lines
2.7 KiB
Rust
Raw Normal View History

2024-04-10 04:54:48 +03:00
use anyhow::{Error, Result};
use notify_debouncer_mini::{
2024-04-09 22:46:55 +03:00
new_debouncer,
notify::{self, RecursiveMode},
};
use std::{
2024-04-10 04:54:48 +03:00
io::{self, Write},
path::Path,
2024-04-10 17:02:12 +03:00
sync::mpsc::channel,
thread,
time::Duration,
};
2024-04-10 17:02:12 +03:00
mod debounce_event;
2024-04-07 20:29:16 +03:00
mod state;
2024-04-10 17:02:12 +03:00
mod terminal_event;
2024-04-07 20:29:16 +03:00
use crate::app_state::AppState;
2024-04-07 20:29:16 +03:00
2024-04-10 17:02:12 +03:00
use self::{
debounce_event::DebounceEventHandler,
state::WatchState,
terminal_event::{terminal_event_handler, InputEvent},
};
enum WatchEvent {
Input(InputEvent),
FileChange { exercise_ind: usize },
2024-04-12 16:27:29 +03:00
TerminalResize,
2024-04-09 22:46:55 +03:00
NotifyErr(notify::Error),
2024-04-10 04:54:48 +03:00
TerminalEventErr(io::Error),
}
2024-04-10 17:02:12 +03:00
/// 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,
}
pub fn watch(app_state: &mut AppState) -> Result<WatchExit> {
let (tx, rx) = channel();
let mut debouncer = new_debouncer(
Duration::from_secs(1),
2024-04-10 05:10:05 +03:00
DebounceEventHandler {
tx: tx.clone(),
exercises: app_state.exercises(),
},
)?;
debouncer
.watcher()
.watch(Path::new("exercises"), RecursiveMode::Recursive)?;
let mut watch_state = WatchState::new(app_state);
watch_state.run_current_exercise()?;
2024-04-10 04:54:48 +03:00
thread::spawn(move || terminal_event_handler(tx));
while let Ok(event) = rx.recv() {
match event {
2024-04-12 16:27:29 +03:00
WatchEvent::Input(InputEvent::Next) => {
watch_state.next_exercise()?;
}
WatchEvent::Input(InputEvent::Hint) => {
watch_state.show_hint()?;
}
WatchEvent::Input(InputEvent::List) => {
return Ok(WatchExit::List);
}
WatchEvent::Input(InputEvent::Quit) => break,
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
watch_state.handle_invalid_cmd(&cmd)?;
}
WatchEvent::FileChange { exercise_ind } => {
watch_state.run_exercise_with_ind(exercise_ind)?;
}
2024-04-12 16:27:29 +03:00
WatchEvent::TerminalResize => {
watch_state.render()?;
}
2024-04-10 04:54:48 +03:00
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"))
}
}
}
2024-04-12 02:24:01 +03:00
watch_state.into_writer().write_all(QUIT_MSG)?;
Ok(WatchExit::Shutdown)
}
2024-04-12 02:24:01 +03:00
const QUIT_MSG: &[u8] = 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.
";