diff --git a/examples/user_input.rs b/examples/user_input.rs index 6fcb631..dddea7f 100644 --- a/examples/user_input.rs +++ b/examples/user_input.rs @@ -60,7 +60,7 @@ fn main() -> Result<(), Box> { let mut terminal = Terminal::new(backend)?; // Setup event handlers - let mut events = Events::new(); + let events = Events::new(); // Create default app state let mut app = App::default(); @@ -151,7 +151,6 @@ fn main() -> Result<(), Box> { InputMode::Normal => match input { Key::Char('e') => { app.input_mode = InputMode::Editing; - events.disable_exit_key(); } Key::Char('q') => { break; @@ -170,7 +169,6 @@ fn main() -> Result<(), Box> { } Key::Esc => { app.input_mode = InputMode::Normal; - events.enable_exit_key(); } _ => {} }, diff --git a/examples/util/event.rs b/examples/util/event.rs index 333096a..33ee9ec 100644 --- a/examples/util/event.rs +++ b/examples/util/event.rs @@ -1,9 +1,5 @@ use std::io; use std::sync::mpsc; -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, -}; use std::thread; use std::time::Duration; @@ -20,20 +16,17 @@ pub enum Event { pub struct Events { rx: mpsc::Receiver>, input_handle: thread::JoinHandle<()>, - ignore_exit_key: Arc, tick_handle: thread::JoinHandle<()>, } #[derive(Debug, Clone, Copy)] pub struct Config { - pub exit_key: Key, pub tick_rate: Duration, } impl Default for Config { fn default() -> Config { Config { - exit_key: Key::Char('q'), tick_rate: Duration::from_millis(250), } } @@ -46,10 +39,8 @@ impl Events { pub fn with_config(config: Config) -> Events { let (tx, rx) = mpsc::channel(); - let ignore_exit_key = Arc::new(AtomicBool::new(false)); let input_handle = { let tx = tx.clone(); - let ignore_exit_key = ignore_exit_key.clone(); thread::spawn(move || { let stdin = io::stdin(); for evt in stdin.keys() { @@ -58,16 +49,14 @@ impl Events { eprintln!("{}", err); return; } - if !ignore_exit_key.load(Ordering::Relaxed) && key == config.exit_key { - return; - } } } }) }; let tick_handle = { thread::spawn(move || loop { - if tx.send(Event::Tick).is_err() { + if let Err(err) = tx.send(Event::Tick) { + eprintln!("{}", err); break; } thread::sleep(config.tick_rate); @@ -75,7 +64,6 @@ impl Events { }; Events { rx, - ignore_exit_key, input_handle, tick_handle, } @@ -84,12 +72,4 @@ impl Events { pub fn next(&self) -> Result, mpsc::RecvError> { self.rx.recv() } - - pub fn disable_exit_key(&mut self) { - self.ignore_exit_key.store(true, Ordering::Relaxed); - } - - pub fn enable_exit_key(&mut self) { - self.ignore_exit_key.store(false, Ordering::Relaxed); - } }