summaryrefslogtreecommitdiff
path: root/src/watch/state.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-09-12 17:45:42 +0200
committermo8it <mo8it@proton.me>2024-09-12 17:46:06 +0200
commit3947c4de284cb82945055a0fe802c2755e951bb9 (patch)
tree943146d047f4e3722878df5e2bb3fd5c834886d5 /src/watch/state.rs
parent664228ef8b910640b353acd7445fa14b9d16ad9f (diff)
Pause input while running an exercise
Diffstat (limited to 'src/watch/state.rs')
-rw-r--r--src/watch/state.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/watch/state.rs b/src/watch/state.rs
index e66cbee..6e76001 100644
--- a/src/watch/state.rs
+++ b/src/watch/state.rs
@@ -5,7 +5,11 @@ use crossterm::{
},
terminal, QueueableCommand,
};
-use std::io::{self, StdoutLock, Write};
+use std::{
+ io::{self, StdoutLock, Write},
+ sync::mpsc::Sender,
+ thread,
+};
use crate::{
app_state::{AppState, ExercisesProgress},
@@ -14,6 +18,11 @@ use crate::{
term::progress_bar,
};
+use super::{
+ terminal_event::{terminal_event_handler, InputPauseGuard},
+ WatchEvent,
+};
+
#[derive(PartialEq, Eq)]
enum DoneStatus {
DoneWithSolution(String),
@@ -31,11 +40,19 @@ pub struct 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()
.context("Failed to get the terminal size")?
.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 {
app_state,
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<()> {
+ // Ignore any input until running the exercise is done.
+ let _input_pause_guard = InputPauseGuard::scoped_pause();
+
self.show_hint = false;
writeln!(