Added panic hooks
This commit is contained in:
parent
130ddd8117
commit
a1648a1061
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bevy_terminal_display"
|
name = "bevy_terminal_display"
|
||||||
version = "0.4.1"
|
version = "0.4.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "0BSD OR MIT OR Apache-2.0"
|
license = "0BSD OR MIT OR Apache-2.0"
|
||||||
description = "A plugin for the Bevy game engine which enables rendering to a terminal using unicode braille characters."
|
description = "A plugin for the Bevy game engine which enables rendering to a terminal using unicode braille characters."
|
||||||
@ -15,4 +15,5 @@ bevy = "0.14"
|
|||||||
bevy_headless_render = "0.1"
|
bevy_headless_render = "0.1"
|
||||||
bevy_dither_post_process = "0.2"
|
bevy_dither_post_process = "0.2"
|
||||||
ratatui = "0.26"
|
ratatui = "0.26"
|
||||||
|
color-eyre = "0.6"
|
||||||
|
|
||||||
|
29
src/lib.rs
29
src/lib.rs
@ -3,9 +3,7 @@
|
|||||||
//! Bevy plugin which allows a camera to render to a terminal window.
|
//! Bevy plugin which allows a camera to render to a terminal window.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::OpenOptions,
|
fs::OpenOptions, io::stdout, path::PathBuf, sync::{Arc, Mutex}
|
||||||
path::PathBuf,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
@ -17,7 +15,9 @@ use bevy::{
|
|||||||
use bevy_dither_post_process::DitherPostProcessPlugin;
|
use bevy_dither_post_process::DitherPostProcessPlugin;
|
||||||
|
|
||||||
use bevy_headless_render::HeadlessRenderPlugin;
|
use bevy_headless_render::HeadlessRenderPlugin;
|
||||||
|
use color_eyre::config::HookBuilder;
|
||||||
pub use crossterm;
|
pub use crossterm;
|
||||||
|
use crossterm::{event::{DisableMouseCapture, PopKeyboardEnhancementFlags}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
pub use ratatui;
|
pub use ratatui;
|
||||||
|
|
||||||
@ -67,6 +67,21 @@ impl Plugin for TerminalDisplayPlugin {
|
|||||||
.with_filter(EnvFilter::builder().parse_lossy(format!("{},{}", Level::INFO, "wgpu=error,naga=warn")));
|
.with_filter(EnvFilter::builder().parse_lossy(format!("{},{}", Level::INFO, "wgpu=error,naga=warn")));
|
||||||
let subscriber = Registry::default().with(file_layer);
|
let subscriber = Registry::default().with(file_layer);
|
||||||
subscriber::set_global_default(subscriber).unwrap();
|
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((
|
app.add_plugins((
|
||||||
DitherPostProcessPlugin,
|
DitherPostProcessPlugin,
|
||||||
HeadlessRenderPlugin,
|
HeadlessRenderPlugin,
|
||||||
@ -87,3 +102,11 @@ impl Plugin for TerminalDisplayPlugin {
|
|||||||
.add_event::<input::events::TerminalInputEvent>();
|
.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();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user