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 --- ticket/src/tui.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'ticket') 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