diff options
author | Silas Bartha <[email protected]> | 2024-08-01 14:54:42 -0400 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2024-08-01 14:54:42 -0400 |
commit | a1648a10613a7cb80878bf8b83cd220e56723c48 (patch) | |
tree | cb39ce36d4ed934576862bbae6394ace71460ebd /src | |
parent | 130ddd81170fed904218381d7182c36fd78e9d6d (diff) |
Added panic hooksv0.4.2
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -3,9 +3,7 @@ //! Bevy plugin which allows a camera to render to a terminal window. use std::{ - fs::OpenOptions, - path::PathBuf, - sync::{Arc, Mutex}, + fs::OpenOptions, io::stdout, path::PathBuf, sync::{Arc, Mutex} }; use bevy::{ @@ -17,7 +15,9 @@ use bevy::{ use bevy_dither_post_process::DitherPostProcessPlugin; use bevy_headless_render::HeadlessRenderPlugin; +use color_eyre::config::HookBuilder; pub use crossterm; +use crossterm::{event::{DisableMouseCapture, PopKeyboardEnhancementFlags}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand}; use once_cell::sync::Lazy; pub use ratatui; @@ -67,6 +67,21 @@ impl Plugin for TerminalDisplayPlugin { .with_filter(EnvFilter::builder().parse_lossy(format!("{},{}", Level::INFO, "wgpu=error,naga=warn"))); let subscriber = Registry::default().with(file_layer); subscriber::set_global_default(subscriber).unwrap(); + + let (panic, error) = HookBuilder::default().into_hooks(); + let panic = panic.into_panic_hook(); + let error = error.into_eyre_hook(); + + color_eyre::eyre::set_hook(Box::new(move |e| { + let _ = restore_terminal(); + error(e) + })).unwrap(); + + std::panic::set_hook(Box::new(move |info| { + let _ = restore_terminal(); + panic(info); + })); + app.add_plugins(( DitherPostProcessPlugin, HeadlessRenderPlugin, @@ -87,3 +102,11 @@ impl Plugin for TerminalDisplayPlugin { .add_event::<input::events::TerminalInputEvent>(); } } + +fn restore_terminal() { + let mut stdout = stdout(); + let _ = stdout.execute(PopKeyboardEnhancementFlags); + let _ = stdout.execute(DisableMouseCapture); + let _ = stdout.execute(LeaveAlternateScreen); + let _ = disable_raw_mode(); +} |