From dcad002057acfb1a41513fb421275116ea946ca3 Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 5 Sep 2024 17:32:59 +0200 Subject: [PATCH] Only render when needed --- src/app_state.rs | 4 ++-- src/run.rs | 2 +- src/watch.rs | 10 +++------- src/watch/state.rs | 8 ++++++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app_state.rs b/src/app_state.rs index 7123d11..ed723c2 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -24,10 +24,10 @@ const STATE_FILE_NAME: &str = ".rustlings-state.txt"; pub enum ExercisesProgress { // All exercises are done. AllDone, - // The current exercise failed and is still pending. - CurrentPending, // A new exercise is now pending. NewPending, + // The current exercise is still pending. + CurrentPending, } pub enum StateFileStatus { diff --git a/src/run.rs b/src/run.rs index f0faa69..a969164 100644 --- a/src/run.rs +++ b/src/run.rs @@ -45,7 +45,7 @@ pub fn run(app_state: &mut AppState) -> Result<()> { } match app_state.done_current_exercise(&mut stdout)? { - ExercisesProgress::CurrentPending | ExercisesProgress::NewPending => { + ExercisesProgress::NewPending | ExercisesProgress::CurrentPending => { stdout.write_all(b"Next exercise: ")?; app_state .current_exercise() diff --git a/src/watch.rs b/src/watch.rs index ee5dd74..bca3832 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -83,13 +83,11 @@ fn run_watch( match event { WatchEvent::Input(InputEvent::Next) => match watch_state.next_exercise(&mut stdout)? { ExercisesProgress::AllDone => break, - ExercisesProgress::CurrentPending => watch_state.render(&mut stdout)?, ExercisesProgress::NewPending => watch_state.run_current_exercise(&mut stdout)?, + ExercisesProgress::CurrentPending => (), }, WatchEvent::Input(InputEvent::Hint) => watch_state.show_hint(&mut stdout)?, - WatchEvent::Input(InputEvent::List) => { - return Ok(WatchExit::List); - } + WatchEvent::Input(InputEvent::List) => return Ok(WatchExit::List), WatchEvent::Input(InputEvent::Quit) => { stdout.write_all(QUIT_MSG)?; break; @@ -99,9 +97,7 @@ fn run_watch( watch_state.handle_file_change(exercise_ind, &mut stdout)?; } WatchEvent::TerminalResize => watch_state.render(&mut stdout)?, - WatchEvent::NotifyErr(e) => { - return Err(Error::from(e).context(NOTIFY_ERR)); - } + WatchEvent::NotifyErr(e) => return Err(Error::from(e).context(NOTIFY_ERR)), WatchEvent::TerminalEventErr(e) => { return Err(Error::from(e).context("Terminal event listener failed")); } diff --git a/src/watch/state.rs b/src/watch/state.rs index fe9e274..75a0c9e 100644 --- a/src/watch/state.rs +++ b/src/watch/state.rs @@ -195,7 +195,11 @@ impl<'a> WatchState<'a> { } pub fn show_hint(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { - self.show_hint = true; - self.render(stdout) + if !self.show_hint { + self.show_hint = true; + self.render(stdout)?; + } + + Ok(()) } }