rustlings/src/watch.rs

125 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);
2024-08-26 01:09:04 +03:00
let mut stdout = io::stdout().lock();
watch_state.run_current_exercise(&mut stdout)?;
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 {
2024-08-26 01:09:04 +03:00
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
ExercisesProgress::AllDone => break,
2024-08-26 01:09:04 +03:00
ExercisesProgress::CurrentPending => watch_state.render(&mut stdout)?,
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,
},
2024-08-26 01:09:04 +03:00
WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?,
WatchEvent::Input(InputEvent::List) => {
return Ok(WatchExit::List);
}
WatchEvent::Input(InputEvent::Quit) => {
2024-08-26 01:09:04 +03:00
stdout.write_all(QUIT_MSG)?;
break;
}
2024-08-26 01:09:04 +03:00
WatchEvent::Input(InputEvent::Run) => watch_state.run_current_exercise(&mut stdout)?,
WatchEvent::Input(InputEvent::Unrecognized) => watch_state.render(&mut stdout)?,
WatchEvent::FileChange { exercise_ind } => {
2024-08-26 01:09:04 +03:00
watch_state.handle_file_change(exercise_ind, &mut stdout)?;
2024-04-12 16:27:29 +03:00
}
2024-08-26 01:09:04 +03:00
WatchEvent::TerminalResize => watch_state.render(&mut stdout)?,
2024-04-10 04:54:48 +03:00
WatchEvent::NotifyErr(e) => {
2024-08-08 23:37:56 +03:00
return Err(Error::from(e).context(NOTIFY_ERR));
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
";