fix(tui): reset terminal modes on startup and exit

Reset sticky mouse/focus/paste terminal modes before the TUI starts and during graceful shutdown paths so stale tab state from prior crashes cannot poison the next session.
This commit is contained in:
Brooklyn Nicholson 2026-04-29 21:41:51 -05:00
parent 98a428fd61
commit d05497f812
3 changed files with 87 additions and 2 deletions

View file

@ -0,0 +1,41 @@
import { writeSync } from 'node:fs'
export const TERMINAL_MODE_RESET =
'\x1b[?1006l' + // SGR mouse
'\x1b[?1003l' + // any-motion mouse
'\x1b[?1002l' + // button-motion mouse
'\x1b[?1000l' + // click mouse
'\x1b[?1004l' + // focus events
'\x1b[?2004l' + // bracketed paste
'\x1b[?1049l' + // alternate screen
'\x1b[0m' + // attributes
'\x1b[?25h' // cursor visible
type ResettableStream = Pick<NodeJS.WriteStream, 'isTTY' | 'write'> & {
fd?: number
}
export function resetTerminalModes(stream: ResettableStream = process.stdout): boolean {
if (!stream.isTTY) {
return false
}
const fd = typeof stream.fd === 'number' ? stream.fd : stream === process.stdout ? 1 : undefined
if (fd !== undefined) {
try {
writeSync(fd, TERMINAL_MODE_RESET)
return true
} catch {
// Fall through to stream.write for mocked or unusual TTY streams.
}
}
try {
stream.write(TERMINAL_MODE_RESET)
return true
} catch {
return false
}
}