Fix terminal not restoring properly on exit
Ensure disable_raw_mode and LeaveAlternateScreen run even when the app returns an error. Also add a panic hook to restore the terminal on unexpected panics. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0eda9045cd
commit
df9d67a7c9
1 changed files with 11 additions and 3 deletions
14
src/main.rs
14
src/main.rs
|
|
@ -13,6 +13,14 @@ use ratatui::{
|
|||
fn main() -> io::Result<()> {
|
||||
let config = Config::load().unwrap();
|
||||
|
||||
// Restore terminal on panic
|
||||
let default_hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |info| {
|
||||
let _ = disable_raw_mode();
|
||||
let _ = execute!(io::stdout(), LeaveAlternateScreen);
|
||||
default_hook(info);
|
||||
}));
|
||||
|
||||
// --- Setup terminal ---
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen)?;
|
||||
|
|
@ -21,12 +29,12 @@ fn main() -> io::Result<()> {
|
|||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut terminal = Terminal::new(backend)?;
|
||||
|
||||
skim::main(&config, &mut terminal)?;
|
||||
let result = skim::main(&config, &mut terminal);
|
||||
|
||||
// --- Restore terminal ---
|
||||
// --- Restore terminal (always, even on error) ---
|
||||
disable_raw_mode()?;
|
||||
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||
terminal.show_cursor()?;
|
||||
|
||||
Ok(())
|
||||
result
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue