Skip to content

Commit

Permalink
Merge pull request #82 from kyoto7250/feature/issue-81/set_panic_hook
Browse files Browse the repository at this point in the history
set a panic hook for crossterm
  • Loading branch information
kyoto7250 authored Jun 27, 2024
2 parents 4480a87 + 8e39778 commit b44c24a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ use crate::app::App;
use crate::config::Config;
use crate::event::{Event, Key};
use anyhow::Result;
use crossterm::execute;
use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use std::io::{self, stdout};
use std::panic::{set_hook, take_hook};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -68,10 +70,26 @@ async fn main() -> anyhow::Result<()> {

fn setup_terminal() -> Result<()> {
enable_raw_mode()?;
init_panic_hook();
io::stdout().execute(EnterAlternateScreen)?;
Ok(())
}

pub fn init_panic_hook() {
let original_hook = take_hook();
set_hook(Box::new(move |panic_info| {
// intentionally ignore errors here since we're already in a panic
let _ = restore_tui();
original_hook(panic_info);
}));
}

pub fn restore_tui() -> io::Result<()> {
disable_raw_mode()?;
execute!(stdout(), LeaveAlternateScreen)?;
Ok(())
}

fn shutdown_terminal() {
let leave_screen = io::stdout().execute(LeaveAlternateScreen).map(|_f| ());

Expand Down

0 comments on commit b44c24a

Please sign in to comment.