mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-05 09:43:31 +02:00
Pause input while running an exercise
This commit is contained in:
+22
-2
@@ -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!(
|
||||
|
||||
Reference in New Issue
Block a user