2024-04-09 21:54:48 -04:00
|
|
|
use anyhow::{Error, Result};
|
|
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
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},
|
|
|
|
DebounceEventResult, DebouncedEventKind,
|
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-09 15:07:53 -04:00
|
|
|
sync::mpsc::{channel, Sender},
|
2024-04-06 19:17:53 -04:00
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
2024-04-07 13:29:16 -04:00
|
|
|
mod state;
|
|
|
|
|
|
|
|
use crate::{exercise::Exercise, state_file::StateFile};
|
|
|
|
|
|
|
|
use self::state::WatchState;
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 20:12:50 -04:00
|
|
|
/// Returned by the watch mode to indicate what to do afterwards.
|
|
|
|
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
|
|
|
enum InputEvent {
|
2024-04-06 19:17:53 -04:00
|
|
|
Hint,
|
2024-04-09 20:12:50 -04:00
|
|
|
List,
|
2024-04-06 19:17:53 -04:00
|
|
|
Quit,
|
2024-04-09 22:08:40 -04:00
|
|
|
Unrecognized(String),
|
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-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
|
|
|
TerminalResize,
|
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 22:10:05 -04:00
|
|
|
struct DebounceEventHandler {
|
2024-04-09 15:07:53 -04:00
|
|
|
tx: Sender<WatchEvent>,
|
|
|
|
exercises: &'static [Exercise],
|
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 22:10:05 -04:00
|
|
|
impl notify_debouncer_mini::DebounceEventHandler for DebounceEventHandler {
|
2024-04-09 15:07:53 -04:00
|
|
|
fn handle_event(&mut self, event: DebounceEventResult) {
|
2024-04-09 15:46:55 -04:00
|
|
|
let event = match event {
|
|
|
|
Ok(event) => {
|
|
|
|
let Some(exercise_ind) = event
|
2024-04-09 15:07:53 -04:00
|
|
|
.iter()
|
2024-04-09 15:46:55 -04:00
|
|
|
.filter_map(|event| {
|
|
|
|
if event.kind != DebouncedEventKind::Any
|
|
|
|
|| !event.path.extension().is_some_and(|ext| ext == "rs")
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.exercises
|
|
|
|
.iter()
|
|
|
|
.position(|exercise| event.path.ends_with(&exercise.path))
|
|
|
|
})
|
|
|
|
.min()
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
WatchEvent::FileChange { exercise_ind }
|
|
|
|
}
|
|
|
|
Err(e) => WatchEvent::NotifyErr(e),
|
2024-04-09 15:07:53 -04:00
|
|
|
};
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 16:04:10 -04:00
|
|
|
// An error occurs when the receiver is dropped.
|
|
|
|
// After dropping the receiver, the debouncer guard should also be dropped.
|
2024-04-09 15:46:55 -04:00
|
|
|
let _ = self.tx.send(event);
|
2024-04-09 15:07:53 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 21:54:48 -04:00
|
|
|
fn terminal_event_handler(tx: Sender<WatchEvent>) {
|
|
|
|
let mut input = String::with_capacity(8);
|
2024-04-09 15:07:53 -04:00
|
|
|
|
2024-04-09 22:08:40 -04:00
|
|
|
let last_input_event = loop {
|
2024-04-09 21:54:48 -04:00
|
|
|
let terminal_event = match event::read() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
// If `send` returns an error, then the receiver is dropped and
|
|
|
|
// a shutdown has been already initialized.
|
|
|
|
let _ = tx.send(WatchEvent::TerminalEventErr(e));
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 15:07:53 -04:00
|
|
|
};
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 21:54:48 -04:00
|
|
|
match terminal_event {
|
|
|
|
Event::Key(key) => {
|
|
|
|
match key.kind {
|
|
|
|
KeyEventKind::Release => continue,
|
|
|
|
KeyEventKind::Press | KeyEventKind::Repeat => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Enter => {
|
|
|
|
let input_event = match input.trim() {
|
|
|
|
"h" | "hint" => InputEvent::Hint,
|
2024-04-09 22:08:40 -04:00
|
|
|
"l" | "list" => break InputEvent::List,
|
|
|
|
"q" | "quit" => break InputEvent::Quit,
|
|
|
|
_ => InputEvent::Unrecognized(input.clone()),
|
2024-04-09 21:54:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-09 20:12:50 -04:00
|
|
|
|
2024-04-09 21:54:48 -04:00
|
|
|
input.clear();
|
|
|
|
}
|
|
|
|
KeyCode::Char(c) => {
|
|
|
|
input.push(c);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Resize(_, _) => {
|
|
|
|
if tx.send(WatchEvent::TerminalResize).is_err() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue,
|
|
|
|
}
|
2024-04-09 22:08:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let _ = tx.send(WatchEvent::Input(last_input_event));
|
2024-04-09 15:07:53 -04:00
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 20:12:50 -04:00
|
|
|
pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> 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(),
|
|
|
|
exercises,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
debouncer
|
|
|
|
.watcher()
|
|
|
|
.watch(Path::new("exercises"), RecursiveMode::Recursive)?;
|
2024-04-06 19:17:53 -04:00
|
|
|
|
2024-04-09 15:07:53 -04:00
|
|
|
let mut watch_state = WatchState::new(state_file, exercises);
|
|
|
|
|
|
|
|
// TODO: bool
|
|
|
|
watch_state.run_exercise()?;
|
|
|
|
watch_state.render()?;
|
|
|
|
|
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 {
|
|
|
|
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-10 09:56:38 -04:00
|
|
|
WatchEvent::TerminalResize => {
|
2024-04-09 15:07:53 -04:00
|
|
|
watch_state.render()?;
|
|
|
|
}
|
|
|
|
WatchEvent::Input(InputEvent::Quit) => 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 } => {
|
|
|
|
// TODO: bool
|
|
|
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
|
|
|
watch_state.render()?;
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|
2024-04-09 21:54:48 -04:00
|
|
|
WatchEvent::NotifyErr(e) => {
|
|
|
|
return Err(Error::from(e).context("Exercise file watcher failed"))
|
|
|
|
}
|
|
|
|
WatchEvent::TerminalEventErr(e) => {
|
|
|
|
return Err(Error::from(e).context("Terminal event listener failed"))
|
|
|
|
}
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-07 13:29:16 -04:00
|
|
|
watch_state.into_writer().write_all(b"
|
2024-04-06 19:17:53 -04:00
|
|
|
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-09 20:12:50 -04:00
|
|
|
Ok(WatchExit::Shutdown)
|
2024-04-06 19:17:53 -04:00
|
|
|
}
|