Skip to content

Commit

Permalink
refactor(tui): improve the termion panic hook (#169)
Browse files Browse the repository at this point in the history
Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
eld4niz and orhun authored Apr 5, 2024
1 parent b47e91a commit dd51d17
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions systeroid-tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ use termion::screen::IntoAlternateScreen;

fn main() -> Result<()> {
if let Some(args) = Args::parse(env::args().collect()) {
let output = io::stderr();
let output = output.into_raw_mode()?;
let output = MouseTerminal::from(output);
let output = output.into_alternate_screen()?;
let backend = TermionBackend::new(output);
let raw_output = io::stderr().into_raw_mode()?;
raw_output.suspend_raw_mode()?;

let panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic| {
let panic_cleanup = || -> Result<()> {
Expand All @@ -27,13 +25,20 @@ fn main() -> Result<()> {
termion::screen::ToMainScreen,
termion::cursor::Show
)?;
output.into_raw_mode()?.suspend_raw_mode()?;
io::stderr().flush()?;
raw_output.suspend_raw_mode()?;
output.flush()?;
Ok(())
};
panic_cleanup().expect("failed to clean up for panic");

panic_cleanup().expect("Failed to cleanup after panic");
panic_hook(panic);
}));
let output = io::stderr();
let output = output.into_raw_mode()?;
let output = MouseTerminal::from(output);
let output = output.into_alternate_screen()?;
let backend = TermionBackend::new(output);

match systeroid_tui::run(args, backend) {
Ok(_) => process::exit(0),
Err(e) => {
Expand Down

0 comments on commit dd51d17

Please sign in to comment.