diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-21 20:27:36 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-04-21 20:27:36 +0300 |
| commit | 4e45051da0f946948158ff7efc9414914640b230 (patch) | |
| tree | a8f5017acdbfb54bc9c23a374e8f46ea8b82fcd1 /src | |
| parent | b71184b1786cb15359ff2e99e3201096c3345116 (diff) | |
fix: restore terminal cursor state on client detach
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/mod.rs | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/src/client/mod.rs b/src/client/mod.rs index 2476a069..0e4f229c 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -157,21 +157,32 @@ struct TerminalGuard { in_tmux: bool, } +fn write_terminal_restore_postlude(writer: &mut impl io::Write) -> io::Result<()> { + // Restore a visible cursor and reset DECSCUSR back to the terminal default. + writer.write_all(b"\x1b[?25h\x1b[0 q")?; + writer.flush() +} + +fn restore_terminal_state(in_tmux: bool) { + // Reset modifyOtherKeys if we enabled it. + if in_tmux { + let _ = io::stdout().write_all(b"\x1b[>4;0m"); + let _ = io::stdout().flush(); + } + + let _ = execute!( + io::stdout(), + PopKeyboardEnhancementFlags, + DisableBracketedPaste, + DisableMouseCapture + ); + ratatui::restore(); + let _ = write_terminal_restore_postlude(&mut io::stdout()); +} + impl Drop for TerminalGuard { fn drop(&mut self) { - // Reset modifyOtherKeys if we enabled it. - if self.in_tmux { - let _ = io::stdout().write_all(b"\x1b[>4;0m"); - let _ = io::stdout().flush(); - } - - let _ = execute!( - io::stdout(), - PopKeyboardEnhancementFlags, - DisableBracketedPaste, - DisableMouseCapture - ); - ratatui::restore(); + restore_terminal_state(self.in_tmux); } } @@ -280,16 +291,7 @@ pub fn run_client() -> io::Result<()> { let in_tmux = std::env::var("TMUX").is_ok(); let original_hook = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { - if in_tmux { - let _ = io::stdout().write_all(b"\x1b[>4;0m"); - } - let _ = execute!( - io::stdout(), - PopKeyboardEnhancementFlags, - DisableBracketedPaste, - DisableMouseCapture - ); - ratatui::restore(); + restore_terminal_state(in_tmux); original_hook(info); })); @@ -633,6 +635,13 @@ mod tests { } #[test] + fn terminal_restore_postlude_restores_visible_default_cursor() { + let mut output = Vec::new(); + write_terminal_restore_postlude(&mut output).unwrap(); + assert_eq!(output, b"\x1b[?25h\x1b[0 q"); + } + + #[test] fn client_error_display_connection_failed() { let err = ClientError::ConnectionFailed(io::Error::new( io::ErrorKind::ConnectionRefused, |
