2024-04-09 21:54:48 -04:00
|
|
|
use anyhow::{Error, Result};
|
2024-04-09 15:07:53 -04:00
|
|
|
use notify_debouncer_mini::{
|
2024-04-09 15:46:55 -04:00
|
|
|
new_debouncer,
|
|
|
|
notify::{self, RecursiveMode},
|
2024-04-09 15:07:53 -04:00
|
|
|
};
|
2024-04-06 19:17:53 -04:00
|
|
|
use std::{
|
2024-04-09 21:54:48 -04:00
|
|
|
io::{self, Write},
|
2024-04-06 19:17:53 -04:00
|
|
|
path::Path,
|
2024-04-10 10:02:12 -04:00
|
|
|
sync::mpsc::channel,
|
2024-04-06 19:17:53 -04:00
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
2024-04-13 19:15:43 -04:00
|
|
|
mod notify_event;
|
2024-04-07 13:29:16 -04:00
|
|
|
mod state;
|
2024-04-10 10:02:12 -04:00
|
|
|
mod terminal_event;
|
2024-04-07 13:29:16 -04:00
|
|
|
|
2024-04-12 12:57:04 -04:00
|
|
|
use crate::app_state::{AppState, ExercisesProgress};
|
2024-04-07 13:29:16 -04:00
|
|
|
|
2024-04-10 10:02:12 -04:00
|
|
|
use self::{
|
2024-04-13 19:15:43 -04:00
|
|
|
notify_event::DebounceEventHandler,
|
2024-04-10 10:02:12 -04:00
|
|
|
state::WatchState,
|
|
|
|
terminal_event::{terminal_event_handler, InputEvent},
|
|
|
|
};
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 15:07:53 -04:00
|
|
|
enum WatchEvent {
|
|
|
|
Input(InputEvent),
|
|
|
|
FileChange { exercise_ind: usize },
|
2024-04-12 09:27:29 -04:00
|
|
|
TerminalResize,
|
2024-04-09 15:46:55 -04:00
|
|
|
NotifyErr(notify::Error),
|
2024-04-09 21:54:48 -04:00
|
|
|
TerminalEventErr(io::Error),
|
2024-04-09 15:07:53 -04:00
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-10 10:02:12 -04:00
|
|
|
/// Returned by the watch mode to indicate what to do afterwards.
|
2024-04-12 12:57:04 -04:00
|
|
|
#[must_use]
|
2024-04-10 10:02:12 -04:00
|
|
|
pub enum WatchExit {
|
|
|
|
/// Exit the program.
|
|
|
|
Shutdown,
|
|
|
|
/// Enter the list mode and restart the watch mode afterwards.
|
|
|
|
List,
|
2024-04-09 15:07:53 -04:00
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-13 19:15:43 -04:00
|
|
|
pub fn watch(
|
|
|
|
app_state: &mut AppState,
|
|
|
|
exercise_paths: &'static [&'static Path],
|
|
|
|
) -> Result<WatchExit> {
|
2024-04-09 15:07:53 -04:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let mut debouncer = new_debouncer(
|
|
|
|
Duration::from_secs(1),
|
2024-04-09 22:10:05 -04:00
|
|
|
DebounceEventHandler {
|
2024-04-09 15:07:53 -04:00
|
|
|
tx: tx.clone(),
|
2024-04-13 19:15:43 -04:00
|
|
|
exercise_paths,
|
2024-04-09 15:07:53 -04:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
debouncer
|
|
|
|
.watcher()
|
|
|
|
.watch(Path::new("exercises"), RecursiveMode::Recursive)?;
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
let mut watch_state = WatchState::new(app_state);
|
2024-04-09 15:07:53 -04:00
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
watch_state.run_current_exercise()?;
|
2024-04-09 15:07:53 -04:00
|
|
|
|
2024-04-09 21:54:48 -04:00
|
|
|
thread::spawn(move || terminal_event_handler(tx));
|
2024-04-09 15:07:53 -04:00
|
|
|
|
|
|
|
while let Ok(event) = rx.recv() {
|
|
|
|
match event {
|
2024-04-12 12:57:04 -04:00
|
|
|
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise()? {
|
|
|
|
ExercisesProgress::AllDone => break,
|
|
|
|
ExercisesProgress::Pending => watch_state.run_current_exercise()?,
|
|
|
|
},
|
2024-04-09 15:07:53 -04:00
|
|
|
WatchEvent::Input(InputEvent::Hint) => {
|
|
|
|
watch_state.show_hint()?;
|
|
|
|
}
|
2024-04-09 20:12:50 -04:00
|
|
|
WatchEvent::Input(InputEvent::List) => {
|
|
|
|
return Ok(WatchExit::List);
|
|
|
|
}
|
2024-04-12 12:57:04 -04:00
|
|
|
WatchEvent::Input(InputEvent::Quit) => {
|
|
|
|
watch_state.into_writer().write_all(QUIT_MSG)?;
|
|
|
|
break;
|
|
|
|
}
|
2024-04-09 22:08:40 -04:00
|
|
|
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
|
|
|
|
watch_state.handle_invalid_cmd(&cmd)?;
|
2024-04-09 15:07:53 -04:00
|
|
|
}
|
|
|
|
WatchEvent::FileChange { exercise_ind } => {
|
|
|
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|
2024-04-12 09:27:29 -04:00
|
|
|
WatchEvent::TerminalResize => {
|
|
|
|
watch_state.render()?;
|
|
|
|
}
|
2024-04-09 21:54:48 -04:00
|
|
|
WatchEvent::NotifyErr(e) => {
|
2024-04-13 19:15:43 -04:00
|
|
|
return Err(Error::from(e).context("Exercise file watcher failed"));
|
2024-04-09 21:54:48 -04:00
|
|
|
}
|
|
|
|
WatchEvent::TerminalEventErr(e) => {
|
2024-04-13 19:15:43 -04:00
|
|
|
return Err(Error::from(e).context("Terminal event listener failed"));
|
2024-04-09 21:54:48 -04:00
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 20:12:50 -04:00
|
|
|
Ok(WatchExit::Shutdown)
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|
2024-04-11 19:24:01 -04: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.
|
|
|
|
";
|