From 03238ddf40330db173b00439139e6e74958b7eb1 Mon Sep 17 00:00:00 2001 From: Michael Gattozzi Date: Sun, 5 Jan 2020 23:10:49 -0500 Subject: Create a panic handler for ticket tui In ticket 9b344c00-2e3d-11ea-bc2d-b4e1317c6ecc 'Create a panic handler for ticket tui' it became clear that ticket needed it's own way to handle panics since crashing the tui without cleaning up after itself leaves the terminal in a completely garbled state. This commit now has the tui clean up properly after itself then run the normal hook which means it panics like any other Rust program and can get a backtrace with the panic as usual with RUST_BACKTRACE=1 --- .../create-a-panic-handler-for-ticket-tui.toml | 17 +++++++++++++ .../create-a-panic-handler-for-ticket-tui.toml | 17 ------------- ticket/src/tui.rs | 28 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 .dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml delete mode 100644 .dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml diff --git a/.dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml b/.dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml new file mode 100644 index 0000000..9847148 --- /dev/null +++ b/.dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml @@ -0,0 +1,17 @@ +title = 'Create a panic handler for ticket tui' +status = 'Closed' +id = '9b344c00-2e3d-11ea-bc2d-b4e1317c6ecc' +assignees = [[ + '64c00ccc-761e-4484-86ac-904e461bb300', + 'Michael Gattozzi', +]] +description = ''' +When the ticket tui has a panic it garbles up the terminal and leaves it in an +awful state. This is because it never sets the terminal back to it's previous +state like it can when it fails gracefully. When the tui is run it should have +a custom panic handler to reset the terminal to it's proper state, then continue +with it's normal unwinding behavior. +''' +version = 'V1' + +[comments] diff --git a/.dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml b/.dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml deleted file mode 100644 index 7b61c45..0000000 --- a/.dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml +++ /dev/null @@ -1,17 +0,0 @@ -title = 'Create a panic handler for ticket tui' -status = 'Open' -id = '9b344c00-2e3d-11ea-bc2d-b4e1317c6ecc' -assignees = [[ - '64c00ccc-761e-4484-86ac-904e461bb300', - 'Michael Gattozzi', -]] -description = ''' -When the ticket tui has a panic it garbles up the terminal and leaves it in an -awful state. This is because it never sets the terminal back to it's previous -state like it can when it fails gracefully. When the tui is run it should have -a custom panic handler to reset the terminal to it's proper state, then continue -with it's normal unwinding behavior. -''' -version = 'V1' - -[comments] diff --git a/ticket/src/tui.rs b/ticket/src/tui.rs index 9898e71..df62106 100644 --- a/ticket/src/tui.rs +++ b/ticket/src/tui.rs @@ -16,6 +16,7 @@ use configamajig::{ UserConfig, }; use crossterm::{ + cursor, event::{ self, DisableMouseCapture, @@ -34,6 +35,7 @@ use std::{ BufWriter, Write, }, + panic, sync::mpsc::{ self, Receiver, @@ -161,6 +163,32 @@ pub fn run() -> Result<()> { terminal.backend_mut().hide_cursor()?; terminal.clear()?; + // Setup panic handler so that screen gets reset properly + let old_hook = panic::take_hook(); + panic::set_hook(Box::new(move |panic_info| { + // Clean up terminal + queue!( + io::stdout().lock(), + LeaveAlternateScreen, + DisableMouseCapture, + cursor::Show + ) + .unwrap(); + disable_raw_mode().unwrap(); + // Load bearing println's without them the actual panic from the old hook + // Isn't flushed to the terminal + if let Some(location) = panic_info.location() { + println!( + "Panic occurred in file '{}' at line {}", + location.file(), + location.line() + ); + } else { + println!("Panic occurred but can't get location information..."); + } + old_hook(panic_info); + })); + // App let mut app = App { tabs: TabsState::new(vec!["Open", "Closed"]), -- cgit v1.2.3