mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-10 06:34:20 +00:00
Pause input while running an exercise
This commit is contained in:
parent
664228ef8b
commit
3947c4de28
4 changed files with 72 additions and 42 deletions
23
src/watch.rs
23
src/watch.rs
|
@ -1,4 +1,4 @@
|
||||||
use anyhow::{Context, Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use notify_debouncer_mini::{
|
use notify_debouncer_mini::{
|
||||||
new_debouncer,
|
new_debouncer,
|
||||||
notify::{self, RecursiveMode},
|
notify::{self, RecursiveMode},
|
||||||
|
@ -7,7 +7,6 @@ use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::mpsc::channel,
|
sync::mpsc::channel,
|
||||||
thread,
|
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,11 +15,7 @@ use crate::{
|
||||||
list,
|
list,
|
||||||
};
|
};
|
||||||
|
|
||||||
use self::{
|
use self::{notify_event::NotifyEventHandler, state::WatchState, terminal_event::InputEvent};
|
||||||
notify_event::NotifyEventHandler,
|
|
||||||
state::WatchState,
|
|
||||||
terminal_event::{terminal_event_handler, InputEvent},
|
|
||||||
};
|
|
||||||
|
|
||||||
mod notify_event;
|
mod notify_event;
|
||||||
mod state;
|
mod state;
|
||||||
|
@ -47,7 +42,7 @@ fn run_watch(
|
||||||
app_state: &mut AppState,
|
app_state: &mut AppState,
|
||||||
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
notify_exercise_names: Option<&'static [&'static [u8]]>,
|
||||||
) -> Result<WatchExit> {
|
) -> Result<WatchExit> {
|
||||||
let (tx, rx) = channel();
|
let (watch_event_sender, watch_event_receiver) = channel();
|
||||||
|
|
||||||
let mut manual_run = false;
|
let mut manual_run = false;
|
||||||
// Prevent dropping the guard until the end of the function.
|
// Prevent dropping the guard until the end of the function.
|
||||||
|
@ -56,7 +51,7 @@ fn run_watch(
|
||||||
let mut debouncer = new_debouncer(
|
let mut debouncer = new_debouncer(
|
||||||
Duration::from_millis(200),
|
Duration::from_millis(200),
|
||||||
NotifyEventHandler {
|
NotifyEventHandler {
|
||||||
tx: tx.clone(),
|
sender: watch_event_sender.clone(),
|
||||||
exercise_names,
|
exercise_names,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -72,16 +67,12 @@ fn run_watch(
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut watch_state = WatchState::build(app_state, manual_run)?;
|
let mut watch_state = WatchState::build(app_state, watch_event_sender, manual_run)?;
|
||||||
|
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
|
|
||||||
watch_state.run_current_exercise(&mut stdout)?;
|
watch_state.run_current_exercise(&mut stdout)?;
|
||||||
|
|
||||||
thread::Builder::new()
|
while let Ok(event) = watch_event_receiver.recv() {
|
||||||
.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 {
|
match event {
|
||||||
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
|
WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? {
|
||||||
ExercisesProgress::AllDone => break,
|
ExercisesProgress::AllDone => break,
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::sync::mpsc::Sender;
|
||||||
use super::WatchEvent;
|
use super::WatchEvent;
|
||||||
|
|
||||||
pub struct NotifyEventHandler {
|
pub struct NotifyEventHandler {
|
||||||
pub tx: Sender<WatchEvent>,
|
pub sender: Sender<WatchEvent>,
|
||||||
/// Used to report which exercise was modified.
|
/// Used to report which exercise was modified.
|
||||||
pub exercise_names: &'static [&'static [u8]],
|
pub exercise_names: &'static [&'static [u8]],
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,6 @@ impl notify_debouncer_mini::DebounceEventHandler for NotifyEventHandler {
|
||||||
|
|
||||||
// An error occurs when the receiver is dropped.
|
// An error occurs when the receiver is dropped.
|
||||||
// After dropping the receiver, the debouncer guard should also be dropped.
|
// After dropping the receiver, the debouncer guard should also be dropped.
|
||||||
let _ = self.tx.send(output_event);
|
let _ = self.sender.send(output_event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,11 @@ use crossterm::{
|
||||||
},
|
},
|
||||||
terminal, QueueableCommand,
|
terminal, QueueableCommand,
|
||||||
};
|
};
|
||||||
use std::io::{self, StdoutLock, Write};
|
use std::{
|
||||||
|
io::{self, StdoutLock, Write},
|
||||||
|
sync::mpsc::Sender,
|
||||||
|
thread,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app_state::{AppState, ExercisesProgress},
|
app_state::{AppState, ExercisesProgress},
|
||||||
|
@ -14,6 +18,11 @@ use crate::{
|
||||||
term::progress_bar,
|
term::progress_bar,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
terminal_event::{terminal_event_handler, InputPauseGuard},
|
||||||
|
WatchEvent,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
enum DoneStatus {
|
enum DoneStatus {
|
||||||
DoneWithSolution(String),
|
DoneWithSolution(String),
|
||||||
|
@ -31,11 +40,19 @@ pub struct WatchState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> WatchState<'a> {
|
impl<'a> WatchState<'a> {
|
||||||
pub fn build(app_state: &'a mut AppState, manual_run: bool) -> Result<Self> {
|
pub fn build(
|
||||||
|
app_state: &'a mut AppState,
|
||||||
|
watch_event_sender: Sender<WatchEvent>,
|
||||||
|
manual_run: bool,
|
||||||
|
) -> Result<Self> {
|
||||||
let term_width = terminal::size()
|
let term_width = terminal::size()
|
||||||
.context("Failed to get the terminal size")?
|
.context("Failed to get the terminal size")?
|
||||||
.0;
|
.0;
|
||||||
|
|
||||||
|
thread::Builder::new()
|
||||||
|
.spawn(move || terminal_event_handler(watch_event_sender, manual_run))
|
||||||
|
.context("Failed to spawn a thread to handle terminal events")?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
app_state,
|
app_state,
|
||||||
output: Vec::with_capacity(OUTPUT_CAPACITY),
|
output: Vec::with_capacity(OUTPUT_CAPACITY),
|
||||||
|
@ -47,6 +64,9 @@ impl<'a> WatchState<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_current_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
|
pub fn run_current_exercise(&mut self, stdout: &mut StdoutLock) -> Result<()> {
|
||||||
|
// Ignore any input until running the exercise is done.
|
||||||
|
let _input_pause_guard = InputPauseGuard::scoped_pause();
|
||||||
|
|
||||||
self.show_hint = false;
|
self.show_hint = false;
|
||||||
|
|
||||||
writeln!(
|
writeln!(
|
||||||
|
|
|
@ -1,8 +1,32 @@
|
||||||
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::{
|
||||||
|
atomic::{AtomicBool, Ordering::Relaxed},
|
||||||
|
mpsc::Sender,
|
||||||
|
};
|
||||||
|
|
||||||
use super::WatchEvent;
|
use super::WatchEvent;
|
||||||
|
|
||||||
|
static INPUT_PAUSED: 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 {
|
||||||
|
INPUT_PAUSED.store(true, Relaxed);
|
||||||
|
Self(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for InputPauseGuard {
|
||||||
|
#[inline]
|
||||||
|
fn drop(&mut self) {
|
||||||
|
INPUT_PAUSED.store(false, Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum InputEvent {
|
pub enum InputEvent {
|
||||||
Run,
|
Run,
|
||||||
Next,
|
Next,
|
||||||
|
@ -11,46 +35,41 @@ pub enum InputEvent {
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminal_event_handler(tx: Sender<WatchEvent>, manual_run: bool) {
|
pub fn terminal_event_handler(sender: Sender<WatchEvent>, manual_run: bool) {
|
||||||
let last_input_event = loop {
|
let last_watch_event = loop {
|
||||||
let terminal_event = match event::read() {
|
match event::read() {
|
||||||
Ok(v) => v,
|
Ok(Event::Key(key)) => {
|
||||||
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 {
|
match key.kind {
|
||||||
KeyEventKind::Release | KeyEventKind::Repeat => continue,
|
KeyEventKind::Release | KeyEventKind::Repeat => continue,
|
||||||
KeyEventKind::Press => (),
|
KeyEventKind::Press => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if INPUT_PAUSED.load(Relaxed) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let input_event = match key.code {
|
let input_event = match key.code {
|
||||||
KeyCode::Char('n') => InputEvent::Next,
|
KeyCode::Char('n') => InputEvent::Next,
|
||||||
KeyCode::Char('h') => InputEvent::Hint,
|
KeyCode::Char('h') => InputEvent::Hint,
|
||||||
KeyCode::Char('l') => break InputEvent::List,
|
KeyCode::Char('l') => break WatchEvent::Input(InputEvent::List),
|
||||||
KeyCode::Char('q') => break InputEvent::Quit,
|
KeyCode::Char('q') => break WatchEvent::Input(InputEvent::Quit),
|
||||||
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
KeyCode::Char('r') if manual_run => InputEvent::Run,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
if tx.send(WatchEvent::Input(input_event)).is_err() {
|
if sender.send(WatchEvent::Input(input_event)).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Resize(width, _) => {
|
Ok(Event::Resize(width, _)) => {
|
||||||
if tx.send(WatchEvent::TerminalResize { width }).is_err() {
|
if sender.send(WatchEvent::TerminalResize { width }).is_err() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::FocusGained | Event::FocusLost | Event::Mouse(_) => continue,
|
Ok(Event::FocusGained | Event::FocusLost | Event::Mouse(_)) => continue,
|
||||||
|
Err(e) => break WatchEvent::TerminalEventErr(e),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = tx.send(WatchEvent::Input(last_input_event));
|
let _ = sender.send(last_watch_event);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue