diff --git a/src/main.rs b/src/main.rs index 3d6f896..8c83488 100644 --- a/src/main.rs +++ b/src/main.rs @@ -280,17 +280,8 @@ fn main() -> Result<(), Box> { } })?; - if let Event::Input(key) = events.next()? { - app.error_msg = match handle_key(&mut app, key) { - // Exit the program - Ok(CalculatorResponse::Quit) => break 'outer, - Ok(CalculatorResponse::Continue) => None, - Err(e) => Some(format!("{}", e)), - }; - } - // Slurp events without a redraw - for e in events.try_iter() { + for e in events.blocking_iter()? { match e { Event::Input(key) => { app.error_msg = match handle_key(&mut app, key) { diff --git a/src/util/event.rs b/src/util/event.rs index bacb7bd..7fa70a4 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -1,6 +1,7 @@ use std::io; +use std::iter; use std::sync::mpsc; -use std::sync::mpsc::TryIter; +use std::sync::mpsc::{Iter, TryIter}; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -54,11 +55,9 @@ impl Events { } } - pub fn next(&self) -> Result, mpsc::RecvError> { - self.rx.recv() - } - - pub fn try_iter(&self) -> TryIter> { - self.rx.try_iter() + /// Returns a chain of the next element (blocking) and a TryIter of everything after it. + /// Useful for reading a single Iterator of all elements once after blocking, so there are not too many UI redraws. + pub fn blocking_iter(&self) -> Result>,TryIter>>, mpsc::RecvError> { + Ok(iter::once(self.rx.recv()?).chain(self.rx.try_iter()).into_iter()) } }