rustlings/src/watch.rs

129 lines
3.8 KiB
Rust
Raw Normal View History

2024-04-10 02:54:48 +01:00
use anyhow::{Error, Result};
use notify_debouncer_mini::{
2024-04-09 20:46:55 +01:00
new_debouncer,
notify::{self, RecursiveMode},
};
use std::{
2024-04-10 02:54:48 +01:00
io::{self, Write},
path::Path,
2024-04-10 15:02:12 +01:00
sync::mpsc::channel,
thread,
time::Duration,
};
use crate::app_state::{AppState, ExercisesProgress};
2024-04-07 18:29:16 +01:00
2024-04-10 15:02:12 +01:00
use self::{
2024-04-14 00:15:43 +01:00
notify_event::DebounceEventHandler,
2024-04-10 15:02:12 +01:00
state::WatchState,
terminal_event::{terminal_event_handler, InputEvent},
};
2024-04-18 10:31:08 +01:00
mod notify_event;
mod state;
mod terminal_event;
enum WatchEvent {
Input(InputEvent),
FileChange { exercise_ind: usize },
2024-04-12 14:27:29 +01:00
TerminalResize,
2024-04-09 20:46:55 +01:00
NotifyErr(notify::Error),
2024-04-10 02:54:48 +01:00
TerminalEventErr(io::Error),
}
2024-04-10 15:02:12 +01:00
/// Returned by the watch mode to indicate what to do afterwards.
#[must_use]
2024-04-10 15:02:12 +01:00
pub enum WatchExit {
/// Exit the program.
Shutdown,
/// Enter the list mode and restart the watch mode afterwards.
List,
}
2024-04-14 00:15:43 +01:00
pub fn watch(
app_state: &mut AppState,
2024-04-14 16:10:53 +01:00
notify_exercise_paths: Option<&'static [&'static str]>,
2024-04-14 00:15:43 +01:00
) -> Result<WatchExit> {
let (tx, rx) = channel();
2024-04-14 16:10:53 +01:00
let mut manual_run = false;
// Prevent dropping the guard until the end of the function.
// Otherwise, the file watcher exits.
let _debouncer_guard = if let Some(exercise_paths) = notify_exercise_paths {
let mut debouncer = new_debouncer(
2024-04-25 02:28:44 +01:00
Duration::from_millis(500),
2024-04-14 16:10:53 +01:00
DebounceEventHandler {
tx: tx.clone(),
exercise_paths,
},
)
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
debouncer
.watcher()
.watch(Path::new("exercises"), RecursiveMode::Recursive)
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
Some(debouncer)
} else {
manual_run = true;
None
};
let mut watch_state = WatchState::new(app_state, manual_run);
watch_state.run_current_exercise()?;
2024-04-14 16:10:53 +01:00
thread::spawn(move || terminal_event_handler(tx, manual_run));
while let Ok(event) = rx.recv() {
match event {
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise()? {
ExercisesProgress::AllDone => break,
ExercisesProgress::Pending => watch_state.run_current_exercise()?,
},
WatchEvent::Input(InputEvent::Hint) => {
watch_state.show_hint()?;
}
WatchEvent::Input(InputEvent::List) => {
return Ok(WatchExit::List);
}
WatchEvent::Input(InputEvent::Quit) => {
watch_state.into_writer().write_all(QUIT_MSG)?;
break;
}
2024-04-14 16:10:53 +01:00
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise()?,
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 14:27:29 +01:00
WatchEvent::TerminalResize => {
watch_state.render()?;
}
2024-04-10 02:54:48 +01:00
WatchEvent::NotifyErr(e) => {
2024-04-14 16:10:53 +01:00
watch_state.into_writer().write_all(NOTIFY_ERR.as_bytes())?;
return Err(Error::from(e));
2024-04-10 02:54:48 +01:00
}
WatchEvent::TerminalEventErr(e) => {
2024-04-14 00:15:43 +01:00
return Err(Error::from(e).context("Terminal event listener failed"));
2024-04-10 02:54:48 +01:00
}
}
}
Ok(WatchExit::Shutdown)
}
2024-04-12 00:24:01 +01: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.
";
2024-04-14 16:10:53 +01:00
const NOTIFY_ERR: &str = "
The automatic detection of exercise file changes failed :(
Please try running `rustlings` again.
If you keep getting this error, run `rustlings --manual-run` to deactivate the file watcher.
You need to manually trigger running the current exercise using `r` or `run` then.
";