From 340893a860e99ec4029d12613f2a6de3cb7b47e2 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Thu, 2 Mar 2023 05:18:06 +0000 Subject: [PATCH] fix: correctly set gui error --- src/input_handler/mod.rs | 23 ++++++++++++----------- src/ui/mod.rs | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/input_handler/mod.rs b/src/input_handler/mod.rs index aa9a866..1d05087 100644 --- a/src/input_handler/mod.rs +++ b/src/input_handler/mod.rs @@ -80,22 +80,23 @@ impl InputHandler { /// Toggle the mouse capture (via input of the 'm' key) fn m_key(&mut self) { if self.mouse_capture { - match execute!(std::io::stdout(), DisableMouseCapture) { - Ok(_) => self - .gui_state + if execute!(std::io::stdout(), DisableMouseCapture).is_ok() { + self.gui_state .lock() - .set_info_box("✖ mouse capture disabled".to_owned()), - Err(_) => { - self.app_data - .lock() - .set_error(AppError::MouseCapture(false)); - } + .set_info_box("✖ mouse capture disabled".to_owned()); + } else { + self.app_data + .lock() + .set_error(AppError::MouseCapture(false)); + self.gui_state.lock().status_push(Status::Error); } - } else { - Ui::enable_mouse_capture(); + } else if Ui::enable_mouse_capture().is_ok() { self.gui_state .lock() .set_info_box("✓ mouse capture enabled".to_owned()); + } else { + self.app_data.lock().set_error(AppError::MouseCapture(true)); + self.gui_state.lock().status_push(Status::Error); }; // If the info box sleep handle is currently being executed, as in 'm' is pressed twice within a 4000ms window diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 2ee8788..1e143ae 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -43,17 +43,16 @@ pub struct Ui { impl Ui { /// Enable mouse capture, but don't enable capture of all the mouse movements, doing so will improve performance, and is part of the fix for the weird mouse event output bug - pub fn enable_mouse_capture() { - io::stdout() - .write_all( - concat!( - crossterm::csi!("?1000h"), - crossterm::csi!("?1015h"), - crossterm::csi!("?1006h"), - ) - .as_bytes(), + pub fn enable_mouse_capture() -> Result<()> { + io::stdout().write_all( + concat!( + crossterm::csi!("?1000h"), + crossterm::csi!("?1015h"), + crossterm::csi!("?1006h"), ) - .unwrap_or(()); + .as_bytes(), + )?; + Ok(()) } /// Create a new Ui struct, and execute the drawing loop @@ -87,13 +86,13 @@ impl Ui { } /// Setup the terminal for full-screen drawing mode, with mouse capture - fn setup_terminal() -> io::Result>> { + fn setup_terminal() -> Result>> { enable_raw_mode()?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen)?; - Self::enable_mouse_capture(); + Self::enable_mouse_capture()?; let backend = CrosstermBackend::new(stdout); - Terminal::new(backend) + Ok(Terminal::new(backend)?) } /// This is a fix for mouse-events being printed to screen, read an event and do nothing with it