Use synchronous writes when detecting keyboard modes (#13478)

This commit is contained in:
Tommaso Sciortino
2025-11-19 21:50:05 -08:00
committed by GitHub
parent ff725dea41
commit 9ebf321717
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import * as fs from 'node:fs';
let detectionComplete = false; let detectionComplete = false;
let kittySupported = false; let kittySupported = false;
@@ -91,9 +93,8 @@ export async function detectAndEnableKittyProtocol(): Promise<void> {
process.stdin.on('data', handleData); process.stdin.on('data', handleData);
// Send queries // Query progressive enhancement and device attributes
process.stdout.write('\x1b[?u'); // Query progressive enhancement fs.writeSync(process.stdout.fd, '\x1b[?u\x1b[c');
process.stdout.write('\x1b[c'); // Query device attributes
// Timeout after 200ms // Timeout after 200ms
// When a iterm2 terminal does not have focus this can take over 90s on a // When a iterm2 terminal does not have focus this can take over 90s on a
@@ -107,13 +108,17 @@ export function isKittyProtocolEnabled(): boolean {
} }
function disableAllProtocols() { function disableAllProtocols() {
if (kittyEnabled) { try {
process.stdout.write('\x1b[<u'); if (kittyEnabled) {
kittyEnabled = false; fs.writeSync(process.stdout.fd, '\x1b[<u');
} kittyEnabled = false;
if (sgrMouseEnabled) { }
process.stdout.write('\x1b[?1006l'); // Disable SGR Mouse if (sgrMouseEnabled) {
sgrMouseEnabled = false; fs.writeSync(process.stdout.fd, '\x1b[?1006l');
sgrMouseEnabled = false;
}
} catch {
// Ignore
} }
} }
@@ -122,12 +127,16 @@ function disableAllProtocols() {
* change the mode. * change the mode.
*/ */
export function enableSupportedProtocol(): void { export function enableSupportedProtocol(): void {
if (kittySupported) { try {
process.stdout.write('\x1b[>1u'); if (kittySupported) {
kittyEnabled = true; fs.writeSync(process.stdout.fd, '\x1b[>1u');
} kittyEnabled = true;
if (sgrMouseSupported) { }
process.stdout.write('\x1b[?1006h'); if (sgrMouseSupported) {
sgrMouseEnabled = true; fs.writeSync(process.stdout.fd, '\x1b[?1006h');
sgrMouseEnabled = true;
}
} catch {
// Ignore
} }
} }