From 1db7b1364e2a4157a1453fc39dc076c499e45025 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 Jan 2026 02:50:46 +0400 Subject: [PATCH] fix: restore terminal state on panic Registers a global panic hook that checks if the terminal is in raw mode (TUI active) and restores it (leaves alternate screen, disables mouse capture, shows cursor) before printing the panic message. This prevents the terminal from being left in a broken state if the TUI panics. --- src/cli/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index fea4113..bc5e59e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -9,8 +9,29 @@ pub use interactive::run_interactive_config; use anyhow::Result; use clap::Parser; +use crossterm::{ + cursor, event, execute, + terminal::{disable_raw_mode, is_raw_mode_enabled, LeaveAlternateScreen}, +}; +use std::io; pub fn run() -> Result<()> { + // Set up global panic hook to restore terminal state on panic + let default_hook = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + // Only try to restore if we seem to be in raw mode (TUI active) + if let Ok(true) = is_raw_mode_enabled() { + let _ = disable_raw_mode(); + let _ = execute!( + io::stdout(), + LeaveAlternateScreen, + event::DisableMouseCapture, + cursor::Show + ); + } + default_hook(info); + })); + let cli = Cli::parse(); cli.run() }