From dd51d17b26e8e6cd21a65aaeabb02ca9085a617a Mon Sep 17 00:00:00 2001 From: eldaniz Date: Fri, 5 Apr 2024 14:20:07 +0200 Subject: [PATCH] refactor(tui): improve the termion panic hook (#169) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Orhun Parmaksız --- systeroid-tui/src/main.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/systeroid-tui/src/main.rs b/systeroid-tui/src/main.rs index 55a1dcd..1ddd6b6 100644 --- a/systeroid-tui/src/main.rs +++ b/systeroid-tui/src/main.rs @@ -11,11 +11,9 @@ use termion::screen::IntoAlternateScreen; fn main() -> Result<()> { if let Some(args) = Args::parse(env::args().collect()) { - let output = io::stderr(); - let output = output.into_raw_mode()?; - let output = MouseTerminal::from(output); - let output = output.into_alternate_screen()?; - let backend = TermionBackend::new(output); + let raw_output = io::stderr().into_raw_mode()?; + raw_output.suspend_raw_mode()?; + let panic_hook = panic::take_hook(); panic::set_hook(Box::new(move |panic| { let panic_cleanup = || -> Result<()> { @@ -27,13 +25,20 @@ fn main() -> Result<()> { termion::screen::ToMainScreen, termion::cursor::Show )?; - output.into_raw_mode()?.suspend_raw_mode()?; - io::stderr().flush()?; + raw_output.suspend_raw_mode()?; + output.flush()?; Ok(()) }; - panic_cleanup().expect("failed to clean up for panic"); + + panic_cleanup().expect("Failed to cleanup after panic"); panic_hook(panic); })); + let output = io::stderr(); + let output = output.into_raw_mode()?; + let output = MouseTerminal::from(output); + let output = output.into_alternate_screen()?; + let backend = TermionBackend::new(output); + match systeroid_tui::run(args, backend) { Ok(_) => process::exit(0), Err(e) => {