diff --git a/crates/tui/src/shell_dispatcher.rs b/crates/tui/src/shell_dispatcher.rs index bb716980..6fb6d6bf 100644 --- a/crates/tui/src/shell_dispatcher.rs +++ b/crates/tui/src/shell_dispatcher.rs @@ -239,15 +239,24 @@ impl ShellDispatcher { } } - // Disable raw mode; guard restores it even on `?` early return. - let _ = crossterm::terminal::disable_raw_mode(); - struct FgRawModeGuard; + // Disable raw mode; guard restores it only if it was already enabled. + let raw_mode_was_enabled = crossterm::terminal::is_raw_mode_enabled().unwrap_or(false); + if raw_mode_was_enabled { + let _ = crossterm::terminal::disable_raw_mode(); + } + struct FgRawModeGuard { + restore: bool, + } impl Drop for FgRawModeGuard { fn drop(&mut self) { - let _ = crossterm::terminal::enable_raw_mode(); + if self.restore { + let _ = crossterm::terminal::enable_raw_mode(); + } } } - let _guard = FgRawModeGuard; + let _guard = FgRawModeGuard { + restore: raw_mode_was_enabled, + }; let mut cmd = self.build_command(shell_command); cmd.current_dir(cwd);