mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 14:57:32 -05:00
Capture terminal resize events
This commit is contained in:
parent
c9a5fa6097
commit
f034899c7f
1 changed files with 60 additions and 31 deletions
61
src/watch.rs
61
src/watch.rs
|
@ -1,11 +1,12 @@
|
||||||
use anyhow::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,
|
DebounceEventResult, DebouncedEventKind,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, BufRead, Write},
|
io::{self, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::mpsc::{channel, Sender},
|
sync::mpsc::{channel, Sender},
|
||||||
thread,
|
thread,
|
||||||
|
@ -39,7 +40,7 @@ enum WatchEvent {
|
||||||
Input(InputEvent),
|
Input(InputEvent),
|
||||||
FileChange { exercise_ind: usize },
|
FileChange { exercise_ind: usize },
|
||||||
NotifyErr(notify::Error),
|
NotifyErr(notify::Error),
|
||||||
StdinErr(io::Error),
|
TerminalEventErr(io::Error),
|
||||||
TerminalResize,
|
TerminalResize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,19 +82,30 @@ impl notify_debouncer_mini::DebounceEventHandler for DebouceEventHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_handler(tx: Sender<WatchEvent>) {
|
fn terminal_event_handler(tx: Sender<WatchEvent>) {
|
||||||
let mut stdin = io::stdin().lock();
|
let mut input = String::with_capacity(8);
|
||||||
let mut stdin_buf = String::with_capacity(8);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = stdin.read_line(&mut stdin_buf) {
|
let terminal_event = match event::read() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
// If `send` returns an error, then the receiver is dropped and
|
// If `send` returns an error, then the receiver is dropped and
|
||||||
// a shutdown has been already initialized.
|
// a shutdown has been already initialized.
|
||||||
let _ = tx.send(WatchEvent::StdinErr(e));
|
let _ = tx.send(WatchEvent::TerminalEventErr(e));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let event = match stdin_buf.trim() {
|
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,
|
"h" | "hint" => InputEvent::Hint,
|
||||||
"c" | "clear" => InputEvent::Clear,
|
"c" | "clear" => InputEvent::Clear,
|
||||||
"l" | "list" => InputEvent::List,
|
"l" | "list" => InputEvent::List,
|
||||||
|
@ -101,17 +113,30 @@ fn input_handler(tx: Sender<WatchEvent>) {
|
||||||
_ => InputEvent::Unrecognized,
|
_ => InputEvent::Unrecognized,
|
||||||
};
|
};
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(event)).is_err() {
|
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
// The receiver was dropped.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
match event {
|
match input_event {
|
||||||
InputEvent::List | InputEvent::Quit => return,
|
InputEvent::List | InputEvent::Quit => return,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
stdin_buf.clear();
|
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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +159,7 @@ pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Resu
|
||||||
watch_state.run_exercise()?;
|
watch_state.run_exercise()?;
|
||||||
watch_state.render()?;
|
watch_state.render()?;
|
||||||
|
|
||||||
thread::spawn(move || input_handler(tx));
|
thread::spawn(move || terminal_event_handler(tx));
|
||||||
|
|
||||||
while let Ok(event) = rx.recv() {
|
while let Ok(event) = rx.recv() {
|
||||||
match event {
|
match event {
|
||||||
|
@ -156,8 +181,12 @@ pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Resu
|
||||||
watch_state.run_exercise_with_ind(exercise_ind)?;
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
||||||
watch_state.render()?;
|
watch_state.render()?;
|
||||||
}
|
}
|
||||||
WatchEvent::NotifyErr(e) => return Err(e.into()),
|
WatchEvent::NotifyErr(e) => {
|
||||||
WatchEvent::StdinErr(e) => return Err(e.into()),
|
return Err(Error::from(e).context("Exercise file watcher failed"))
|
||||||
|
}
|
||||||
|
WatchEvent::TerminalEventErr(e) => {
|
||||||
|
return Err(Error::from(e).context("Terminal event listener failed"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue