rustlings/src/watch.rs

168 lines
5.3 KiB
Rust
Raw Normal View History

use anyhow::{Context, 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},
list,
};
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 },
TerminalResize { width: u16 },
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]
enum WatchExit {
2024-04-10 17:02:12 +03:00
/// Exit the program.
Shutdown,
/// Enter the list mode and restart the watch mode afterwards.
List,
}
fn run_watch(
2024-04-14 02:15:43 +03:00
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::build(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)?;
thread::Builder::new()
.spawn(move || terminal_event_handler(tx, manual_run))
.context("Failed to spawn a thread to handle terminal events")?;
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::NewPending => watch_state.run_current_exercise(&mut stdout)?,
2024-09-05 18:32:59 +03:00
ExercisesProgress::CurrentPending => (),
},
2024-08-26 01:09:04 +03:00
WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?,
2024-09-05 18:32:59 +03:00
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::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
}
WatchEvent::TerminalResize { width } => {
watch_state.update_term_width(width, &mut stdout)?;
}
2024-09-05 18:32:59 +03:00
WatchEvent::NotifyErr(e) => 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
fn watch_list_loop(
app_state: &mut AppState,
notify_exercise_names: Option<&'static [&'static [u8]]>,
) -> Result<()> {
loop {
match run_watch(app_state, notify_exercise_names)? {
WatchExit::Shutdown => break Ok(()),
// It is much easier to exit the watch mode, launch the list mode and then restart
// the watch mode instead of trying to pause the watch threads and correct the
// watch state.
WatchExit::List => list::list(app_state)?,
}
}
}
/// `notify_exercise_names` as None activates the manual run mode.
pub fn watch(
app_state: &mut AppState,
notify_exercise_names: Option<&'static [&'static [u8]]>,
) -> Result<()> {
#[cfg(not(windows))]
{
let stdin_fd = rustix::stdio::stdin();
let mut termios = rustix::termios::tcgetattr(stdin_fd)?;
let original_local_modes = termios.local_modes;
// Disable stdin line buffering and hide input.
termios.local_modes -=
rustix::termios::LocalModes::ICANON | rustix::termios::LocalModes::ECHO;
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
let res = watch_list_loop(app_state, notify_exercise_names);
termios.local_modes = original_local_modes;
rustix::termios::tcsetattr(stdin_fd, rustix::termios::OptionalActions::Now, &termios)?;
res
}
#[cfg(windows)]
watch_list_loop(app_state, notify_exercise_names)
}
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
";