Keep hint displayed after resizing the terminal

This commit is contained in:
mo8it 2024-04-10 15:56:38 +02:00
parent 27e9520665
commit 256c4013b7
2 changed files with 26 additions and 19 deletions

View file

@ -29,7 +29,6 @@ pub enum WatchExit {
enum InputEvent { enum InputEvent {
Hint, Hint,
Clear,
List, List,
Quit, Quit,
Unrecognized(String), Unrecognized(String),
@ -106,7 +105,6 @@ fn terminal_event_handler(tx: Sender<WatchEvent>) {
KeyCode::Enter => { KeyCode::Enter => {
let input_event = match input.trim() { let input_event = match input.trim() {
"h" | "hint" => InputEvent::Hint, "h" | "hint" => InputEvent::Hint,
"c" | "clear" => InputEvent::Clear,
"l" | "list" => break InputEvent::List, "l" | "list" => break InputEvent::List,
"q" | "quit" => break InputEvent::Quit, "q" | "quit" => break InputEvent::Quit,
_ => InputEvent::Unrecognized(input.clone()), _ => InputEvent::Unrecognized(input.clone()),
@ -165,7 +163,7 @@ pub fn watch(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Resu
WatchEvent::Input(InputEvent::List) => { WatchEvent::Input(InputEvent::List) => {
return Ok(WatchExit::List); return Ok(WatchExit::List);
} }
WatchEvent::Input(InputEvent::Clear) | WatchEvent::TerminalResize => { WatchEvent::TerminalResize => {
watch_state.render()?; watch_state.render()?;
} }
WatchEvent::Input(InputEvent::Quit) => break, WatchEvent::Input(InputEvent::Quit) => break,

View file

@ -6,7 +6,7 @@ use crossterm::{
}; };
use std::{ use std::{
fmt::Write as _, fmt::Write as _,
io::{self, StdoutLock, Write as _}, io::{self, StdoutLock, Write},
}; };
use crate::{ use crate::{
@ -24,7 +24,7 @@ pub struct WatchState<'a> {
stdout: Option<Vec<u8>>, stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>, stderr: Option<Vec<u8>>,
message: Option<String>, message: Option<String>,
prompt: Vec<u8>, hint_displayed: bool,
} }
impl<'a> WatchState<'a> { impl<'a> WatchState<'a> {
@ -35,15 +35,6 @@ impl<'a> WatchState<'a> {
let writer = io::stdout().lock(); let writer = io::stdout().lock();
let prompt = format!(
"\n\n{}int/{}lear/{}ist/{}uit? ",
"h".bold(),
"c".bold(),
"l".bold(),
"q".bold(),
)
.into_bytes();
Self { Self {
writer, writer,
exercises, exercises,
@ -53,7 +44,7 @@ impl<'a> WatchState<'a> {
stdout: None, stdout: None,
stderr: None, stderr: None,
message: None, message: None,
prompt, hint_displayed: false,
} }
} }
@ -122,7 +113,15 @@ You can keep working on this exercise or jump into the next one by removing the
} }
pub fn show_prompt(&mut self) -> io::Result<()> { pub fn show_prompt(&mut self) -> io::Result<()> {
self.writer.write_all(&self.prompt)?; self.writer.write_all(b"\n\n")?;
if !self.hint_displayed {
self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?;
}
self.writer
.write_fmt(format_args!("{}ist/{}uit? ", 'l'.bold(), 'q'.bold()))?;
self.writer.flush() self.writer.flush()
} }
@ -134,10 +133,12 @@ You can keep working on this exercise or jump into the next one by removing the
if let Some(stdout) = &self.stdout { if let Some(stdout) = &self.stdout {
self.writer.write_all(stdout)?; self.writer.write_all(stdout)?;
self.writer.write_all(b"\n")?;
} }
if let Some(stderr) = &self.stderr { if let Some(stderr) = &self.stderr {
self.writer.write_all(stderr)?; self.writer.write_all(stderr)?;
self.writer.write_all(b"\n")?;
} }
if let Some(message) = &self.message { if let Some(message) = &self.message {
@ -145,6 +146,14 @@ You can keep working on this exercise or jump into the next one by removing the
} }
self.writer.write_all(b"\n")?; self.writer.write_all(b"\n")?;
if self.hint_displayed {
self.writer
.write_fmt(format_args!("\n{}\n", "Hint".bold().cyan().underlined()))?;
self.writer.write_all(self.exercise.hint.as_bytes())?;
self.writer.write_all(b"\n\n")?;
}
let line_width = size()?.0; let line_width = size()?.0;
let progress_bar = progress_bar(self.progress, self.exercises.len() as u16, line_width)?; let progress_bar = progress_bar(self.progress, self.exercises.len() as u16, line_width)?;
self.writer.write_all(progress_bar.as_bytes())?; self.writer.write_all(progress_bar.as_bytes())?;
@ -160,9 +169,9 @@ You can keep working on this exercise or jump into the next one by removing the
Ok(()) Ok(())
} }
pub fn show_hint(&mut self) -> io::Result<()> { pub fn show_hint(&mut self) -> Result<()> {
self.writer.write_all(self.exercise.hint.as_bytes())?; self.hint_displayed = true;
self.show_prompt() self.render()
} }
pub fn handle_invalid_cmd(&mut self, cmd: &str) -> io::Result<()> { pub fn handle_invalid_cmd(&mut self, cmd: &str) -> io::Result<()> {