Pause input while running an exercise

This commit is contained in:
mo8it
2024-09-12 17:46:06 +02:00
parent 664228ef8b
commit 3947c4de28
4 changed files with 72 additions and 42 deletions
+22 -2
View File
@@ -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!(