mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Separate event handlers
This commit is contained in:
parent
256c4013b7
commit
4bb6bda9f6
123
src/watch.rs
123
src/watch.rs
|
@ -1,38 +1,27 @@
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
|
||||||
use notify_debouncer_mini::{
|
use notify_debouncer_mini::{
|
||||||
new_debouncer,
|
new_debouncer,
|
||||||
notify::{self, RecursiveMode},
|
notify::{self, RecursiveMode},
|
||||||
DebounceEventResult, DebouncedEventKind,
|
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::mpsc::{channel, Sender},
|
sync::mpsc::channel,
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod debounce_event;
|
||||||
mod state;
|
mod state;
|
||||||
|
mod terminal_event;
|
||||||
|
|
||||||
use crate::{exercise::Exercise, state_file::StateFile};
|
use crate::{exercise::Exercise, state_file::StateFile};
|
||||||
|
|
||||||
use self::state::WatchState;
|
use self::{
|
||||||
|
debounce_event::DebounceEventHandler,
|
||||||
/// Returned by the watch mode to indicate what to do afterwards.
|
state::WatchState,
|
||||||
pub enum WatchExit {
|
terminal_event::{terminal_event_handler, InputEvent},
|
||||||
/// Exit the program.
|
};
|
||||||
Shutdown,
|
|
||||||
/// Enter the list mode and restart the watch mode afterwards.
|
|
||||||
List,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum InputEvent {
|
|
||||||
Hint,
|
|
||||||
List,
|
|
||||||
Quit,
|
|
||||||
Unrecognized(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
enum WatchEvent {
|
enum WatchEvent {
|
||||||
Input(InputEvent),
|
Input(InputEvent),
|
||||||
|
@ -42,96 +31,12 @@ enum WatchEvent {
|
||||||
TerminalResize,
|
TerminalResize,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DebounceEventHandler {
|
/// Returned by the watch mode to indicate what to do afterwards.
|
||||||
tx: Sender<WatchEvent>,
|
pub enum WatchExit {
|
||||||
exercises: &'static [Exercise],
|
/// Exit the program.
|
||||||
}
|
Shutdown,
|
||||||
|
/// Enter the list mode and restart the watch mode afterwards.
|
||||||
impl notify_debouncer_mini::DebounceEventHandler for DebounceEventHandler {
|
List,
|
||||||
fn handle_event(&mut self, event: DebounceEventResult) {
|
|
||||||
let event = match event {
|
|
||||||
Ok(event) => {
|
|
||||||
let Some(exercise_ind) = event
|
|
||||||
.iter()
|
|
||||||
.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),
|
|
||||||
};
|
|
||||||
|
|
||||||
// An error occurs when the receiver is dropped.
|
|
||||||
// After dropping the receiver, the debouncer guard should also be dropped.
|
|
||||||
let _ = self.tx.send(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn terminal_event_handler(tx: Sender<WatchEvent>) {
|
|
||||||
let mut input = String::with_capacity(8);
|
|
||||||
|
|
||||||
let last_input_event = loop {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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,
|
|
||||||
"l" | "list" => break InputEvent::List,
|
|
||||||
"q" | "quit" => break InputEvent::Quit,
|
|
||||||
_ => InputEvent::Unrecognized(input.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = tx.send(WatchEvent::Input(last_input_event));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<WatchExit> {
|
pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<WatchExit> {
|
||||||
|
|
44
src/watch/debounce_event.rs
Normal file
44
src/watch/debounce_event.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
use crate::exercise::Exercise;
|
||||||
|
|
||||||
|
use super::WatchEvent;
|
||||||
|
|
||||||
|
pub struct DebounceEventHandler {
|
||||||
|
pub tx: Sender<WatchEvent>,
|
||||||
|
pub exercises: &'static [Exercise],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl notify_debouncer_mini::DebounceEventHandler for DebounceEventHandler {
|
||||||
|
fn handle_event(&mut self, event: DebounceEventResult) {
|
||||||
|
let event = match event {
|
||||||
|
Ok(event) => {
|
||||||
|
let Some(exercise_ind) = event
|
||||||
|
.iter()
|
||||||
|
.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),
|
||||||
|
};
|
||||||
|
|
||||||
|
// An error occurs when the receiver is dropped.
|
||||||
|
// After dropping the receiver, the debouncer guard should also be dropped.
|
||||||
|
let _ = self.tx.send(event);
|
||||||
|
}
|
||||||
|
}
|
65
src/watch/terminal_event.rs
Normal file
65
src/watch/terminal_event.rs
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
||||||
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
|
use super::WatchEvent;
|
||||||
|
|
||||||
|
pub enum InputEvent {
|
||||||
|
Hint,
|
||||||
|
List,
|
||||||
|
Quit,
|
||||||
|
Unrecognized(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn terminal_event_handler(tx: Sender<WatchEvent>) {
|
||||||
|
let mut input = String::with_capacity(8);
|
||||||
|
|
||||||
|
let last_input_event = loop {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
|
"l" | "list" => break InputEvent::List,
|
||||||
|
"q" | "quit" => break InputEvent::Quit,
|
||||||
|
_ => InputEvent::Unrecognized(input.clone()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = tx.send(WatchEvent::Input(last_input_event));
|
||||||
|
}
|
Loading…
Reference in a new issue