diff options
author | Michael Gattozzi <mgattozzi@gmail.com> | 2020-01-05 23:10:49 -0500 |
---|---|---|
committer | Michael Gattozzi <mgattozzi@gmail.com> | 2020-01-05 23:10:49 -0500 |
commit | 03238ddf40330db173b00439139e6e74958b7eb1 (patch) | |
tree | 232b66befa09878b3762949bc88e2ecb19fd6d43 | |
parent | c8e11a8f9817bc780cb5858a946e5845788badd5 (diff) |
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
-rw-r--r-- | .dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml (renamed from .dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml) | 2 | ||||
-rw-r--r-- | ticket/src/tui.rs | 28 |
2 files changed, 29 insertions, 1 deletions
diff --git a/.dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml b/.dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml index 7b61c45..9847148 100644 --- a/.dev-suite/ticket/open/create-a-panic-handler-for-ticket-tui.toml +++ b/.dev-suite/ticket/closed/create-a-panic-handler-for-ticket-tui.toml @@ -1,5 +1,5 @@ title = 'Create a panic handler for ticket tui' -status = 'Open' +status = 'Closed' id = '9b344c00-2e3d-11ea-bc2d-b4e1317c6ecc' assignees = [[ '64c00ccc-761e-4484-86ac-904e461bb300', 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"]), |