2024-09-12 18:45:42 +03:00
|
|
|
use anyhow::{Error, Result};
|
2024-09-18 02:43:48 +03:00
|
|
|
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
|
2024-04-07 02:17:53 +03:00
|
|
|
use std::{
|
2024-04-10 04:54:48 +03:00
|
|
|
io::{self, Write},
|
2024-04-07 02:17:53 +03:00
|
|
|
path::Path,
|
2024-09-18 02:43:48 +03:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering::Relaxed},
|
|
|
|
mpsc::channel,
|
|
|
|
},
|
2024-04-07 02:17:53 +03:00
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
2024-09-05 03:11:19 +03:00
|
|
|
use crate::{
|
|
|
|
app_state::{AppState, ExercisesProgress},
|
|
|
|
list,
|
|
|
|
};
|
2024-04-07 20:29:16 +03:00
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
use self::{notify_event::NotifyEventHandler, state::WatchState, terminal_event::InputEvent};
|
2024-04-07 02:17:53 +03:00
|
|
|
|
2024-04-18 12:31:08 +03:00
|
|
|
mod notify_event;
|
|
|
|
mod state;
|
|
|
|
mod terminal_event;
|
|
|
|
|
2024-09-18 02:43:48 +03:00
|
|
|
static EXERCISE_RUNNING: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
// Private unit type to force using the constructor function.
|
|
|
|
#[must_use = "When the guard is dropped, the input is unpaused"]
|
|
|
|
pub struct InputPauseGuard(());
|
|
|
|
|
|
|
|
impl InputPauseGuard {
|
|
|
|
#[inline]
|
|
|
|
pub fn scoped_pause() -> Self {
|
|
|
|
EXERCISE_RUNNING.store(true, Relaxed);
|
|
|
|
Self(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for InputPauseGuard {
|
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
EXERCISE_RUNNING.store(false, Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:07:53 +03:00
|
|
|
enum WatchEvent {
|
|
|
|
Input(InputEvent),
|
|
|
|
FileChange { exercise_ind: usize },
|
2024-09-05 18:45:27 +03:00
|
|
|
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-09 22:07:53 +03:00
|
|
|
}
|
2024-04-07 02:17:53 +03:00
|
|
|
|
2024-04-10 17:02:12 +03:00
|
|
|
/// Returned by the watch mode to indicate what to do afterwards.
|
2024-04-12 19:57:04 +03:00
|
|
|
#[must_use]
|
2024-09-05 03:11:19 +03:00
|
|
|
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,
|
2024-04-09 22:07:53 +03:00
|
|
|
}
|
2024-04-07 02:17:53 +03:00
|
|
|
|
2024-09-05 03:11:19 +03:00
|
|
|
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> {
|
2024-09-12 18:45:42 +03:00
|
|
|
let (watch_event_sender, watch_event_receiver) = 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-09-18 02:43:48 +03:00
|
|
|
let _watcher_guard = if let Some(exercise_names) = notify_exercise_names {
|
2024-09-26 13:26:24 +03:00
|
|
|
let notify_event_handler =
|
|
|
|
NotifyEventHandler::build(watch_event_sender.clone(), exercise_names)?;
|
|
|
|
|
2024-09-18 02:43:48 +03:00
|
|
|
let mut watcher = RecommendedWatcher::new(
|
2024-09-26 13:26:24 +03:00
|
|
|
notify_event_handler,
|
2024-09-18 02:43:48 +03:00
|
|
|
Config::default().with_poll_interval(Duration::from_secs(1)),
|
2024-04-14 18:10:53 +03:00
|
|
|
)
|
|
|
|
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
|
2024-09-18 02:43:48 +03:00
|
|
|
|
|
|
|
watcher
|
2024-04-14 18:10:53 +03:00
|
|
|
.watch(Path::new("exercises"), RecursiveMode::Recursive)
|
|
|
|
.inspect_err(|_| eprintln!("{NOTIFY_ERR}"))?;
|
|
|
|
|
2024-09-18 02:43:48 +03:00
|
|
|
Some(watcher)
|
2024-04-14 18:10:53 +03:00
|
|
|
} else {
|
|
|
|
manual_run = true;
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
let mut watch_state = WatchState::build(app_state, watch_event_sender, manual_run)?;
|
2024-08-26 01:09:04 +03:00
|
|
|
let mut stdout = io::stdout().lock();
|
2024-04-09 22:07:53 +03:00
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
watch_state.run_current_exercise(&mut stdout)?;
|
2024-04-09 22:07:53 +03:00
|
|
|
|
2024-09-12 18:45:42 +03:00
|
|
|
while let Ok(event) = watch_event_receiver.recv() {
|
2024-04-09 22:07:53 +03:00
|
|
|
match event {
|
2024-08-26 01:09:04 +03:00
|
|
|
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
|
2024-04-12 19:57:04 +03:00
|
|
|
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-04-12 19:57:04 +03:00
|
|
|
},
|
2024-09-26 19:15:45 +03:00
|
|
|
WatchEvent::Input(InputEvent::Run) => 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)?,
|
2024-09-05 18:32:59 +03:00
|
|
|
WatchEvent::Input(InputEvent::List) => return Ok(WatchExit::List),
|
2024-10-02 23:40:32 +03:00
|
|
|
WatchEvent::Input(InputEvent::CheckAll) => match watch_state
|
|
|
|
.check_all_exercises(&mut stdout)?
|
|
|
|
{
|
|
|
|
ExercisesProgress::AllDone => break,
|
|
|
|
ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?,
|
2024-10-10 20:43:35 +03:00
|
|
|
ExercisesProgress::CurrentPending => watch_state.render(&mut stdout)?,
|
2024-10-02 23:40:32 +03:00
|
|
|
},
|
2024-09-26 19:15:45 +03:00
|
|
|
WatchEvent::Input(InputEvent::Reset) => watch_state.reset_exercise(&mut stdout)?,
|
2024-04-12 19:57:04 +03:00
|
|
|
WatchEvent::Input(InputEvent::Quit) => {
|
2024-08-26 01:09:04 +03:00
|
|
|
stdout.write_all(QUIT_MSG)?;
|
2024-04-12 19:57:04 +03:00
|
|
|
break;
|
|
|
|
}
|
2024-04-09 22:07:53 +03:00
|
|
|
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-09-05 18:45:27 +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
|
|
|
}
|
2024-04-07 02:17:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 03:12:50 +03:00
|
|
|
Ok(WatchExit::Shutdown)
|
2024-04-07 02:17:53 +03:00
|
|
|
}
|
2024-04-12 02:24:01 +03:00
|
|
|
|
2024-09-05 03:11:19 +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"
|
2024-09-12 17:34:33 +03:00
|
|
|
|
2024-04-12 02:24:01 +03:00
|
|
|
We hope you're enjoying learning Rust!
|
2024-09-12 17:34:33 +03:00
|
|
|
If you want to continue working on the exercises at a later point, you can simply run `rustlings` again in this directory.
|
2024-04-12 02:24:01 +03:00
|
|
|
";
|
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
|
|
|
";
|