rustlings/src/watch.rs

129 lines
3.9 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,
};
use crate::app_state::{AppState, ExercisesProgress};
2024-04-07 20:29:16 +03:00
2024-04-10 17:02:12 +03:00
use self::{
2024-05-14 02:49:22 +03:00
notify_event::NotifyEventHandler,
2024-04-10 17:02:12 +03:00
state::WatchState,
terminal_event::{terminal_event_handler, InputEvent},
};
2024-04-18 12:31:08 +03:00
mod notify_event;
mod state;
mod terminal_event;
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.
#[must_use]
2024-04-10 17:02:12 +03:00
pub enum WatchExit {
/// Exit the program.
Shutdown,
/// Enter the list mode and restart the watch mode afterwards.
List,
}
2024-05-14 02:49:22 +03:00
/// `notify_exercise_names` as None activates the manual run mode.
2024-04-14 02:15:43 +03:00
pub fn watch(
app_state: &mut AppState,
2024-04-25 15:44:12 +03:00
notify_exercise_names: Option<&'static [&'static [u8]]>,
2024-04-14 02:15:43 +03:00
) -> Result<WatchExit> {
let (tx, rx) = channel();
2024-04-14 18:10:53 +03:00
let mut manual_run = false;
// Prevent dropping the guard until the end of the function.
// Otherwise, the file watcher exits.
2024-04-25 15:44:12 +03:00
let _debouncer_guard = if let Some(exercise_names) = notify_exercise_names {
2024-04-14 18:10:53 +03:00
let mut debouncer = new_debouncer(
2024-04-25 16:22:11 +03:00
Duration::from_millis(200),
2024-05-14 02:49:22 +03:00
NotifyEventHandler {
2024-04-14 18:10:53 +03:00
tx: tx.clone(),
2024-04-25 15:44:12 +03:00
exercise_names,
2024-04-14 18:10:53 +03:00
},
)
.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 18:10:53 +03: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,
2024-05-13 03:32:25 +03:00
ExercisesProgress::CurrentPending => watch_state.render()?,
ExercisesProgress::NewPending => 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 18:10:53 +03:00
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise()?,
2024-05-13 03:32:25 +03:00
WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render()?,
WatchEvent::FileChange { exercise_ind } => {
2024-05-13 18:06:11 +03:00
watch_state.handle_file_change(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) => {
2024-04-14 18:10:53 +03:00
watch_state.into_writer().write_all(NOTIFY_ERR.as_bytes())?;
return Err(Error::from(e));
2024-04-10 04:54:48 +03:00
}
WatchEvent::TerminalEventErr(e) => {
2024-04-14 02:15:43 +03:00
return Err(Error::from(e).context("Terminal event listener failed"));
2024-04-10 04:54:48 +03:00
}
}
}
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.
";
2024-04-14 18:10:53 +03: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.
2024-05-13 03:37:32 +03:00
You need to manually trigger running the current exercise using `r` then.
2024-04-14 18:10:53 +03:00
";