2025-08-13 13:32:54 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
let detectionComplete = false;
|
|
|
|
|
let protocolSupported = false;
|
|
|
|
|
let protocolEnabled = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Detects Kitty keyboard protocol support.
|
|
|
|
|
* Definitive document about this protocol lives at https://sw.kovidgoyal.net/kitty/keyboard-protocol/
|
|
|
|
|
* This function should be called once at app startup.
|
|
|
|
|
*/
|
|
|
|
|
export async function detectAndEnableKittyProtocol(): Promise<boolean> {
|
|
|
|
|
if (detectionComplete) {
|
|
|
|
|
return protocolSupported;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
|
|
|
detectionComplete = true;
|
|
|
|
|
resolve(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const originalRawMode = process.stdin.isRaw;
|
|
|
|
|
if (!originalRawMode) {
|
|
|
|
|
process.stdin.setRawMode(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let responseBuffer = '';
|
|
|
|
|
let progressiveEnhancementReceived = false;
|
2025-09-05 17:18:51 -07:00
|
|
|
let timeoutId: NodeJS.Timeout | undefined;
|
|
|
|
|
|
|
|
|
|
const onTimeout = () => {
|
|
|
|
|
timeoutId = undefined;
|
|
|
|
|
process.stdin.removeListener('data', handleData);
|
|
|
|
|
if (!originalRawMode) {
|
|
|
|
|
process.stdin.setRawMode(false);
|
|
|
|
|
}
|
|
|
|
|
detectionComplete = true;
|
|
|
|
|
resolve(false);
|
|
|
|
|
};
|
2025-08-13 13:32:54 -04:00
|
|
|
|
|
|
|
|
const handleData = (data: Buffer) => {
|
2025-09-05 17:18:51 -07:00
|
|
|
if (timeoutId === undefined) {
|
|
|
|
|
// Race condition. We have already timed out.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-13 13:32:54 -04:00
|
|
|
responseBuffer += data.toString();
|
|
|
|
|
|
|
|
|
|
// Check for progressive enhancement response (CSI ? <flags> u)
|
|
|
|
|
if (responseBuffer.includes('\x1b[?') && responseBuffer.includes('u')) {
|
|
|
|
|
progressiveEnhancementReceived = true;
|
2025-09-05 17:18:51 -07:00
|
|
|
// Give more time to get the full set of kitty responses if we have an
|
|
|
|
|
// indication the terminal probably supports kitty and we just need to
|
|
|
|
|
// wait a bit longer for a response.
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = setTimeout(onTimeout, 1000);
|
2025-08-13 13:32:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for device attributes response (CSI ? <attrs> c)
|
|
|
|
|
if (responseBuffer.includes('\x1b[?') && responseBuffer.includes('c')) {
|
2025-09-05 17:18:51 -07:00
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = undefined;
|
|
|
|
|
process.stdin.removeListener('data', handleData);
|
|
|
|
|
|
|
|
|
|
if (!originalRawMode) {
|
|
|
|
|
process.stdin.setRawMode(false);
|
2025-08-13 13:32:54 -04:00
|
|
|
}
|
2025-09-05 17:18:51 -07:00
|
|
|
|
|
|
|
|
if (progressiveEnhancementReceived) {
|
|
|
|
|
// Enable the protocol
|
|
|
|
|
process.stdout.write('\x1b[>1u');
|
|
|
|
|
protocolSupported = true;
|
|
|
|
|
protocolEnabled = true;
|
|
|
|
|
|
|
|
|
|
// Set up cleanup on exit
|
|
|
|
|
process.on('exit', disableProtocol);
|
|
|
|
|
process.on('SIGTERM', disableProtocol);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detectionComplete = true;
|
|
|
|
|
resolve(protocolSupported);
|
2025-08-13 13:32:54 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.stdin.on('data', handleData);
|
|
|
|
|
|
|
|
|
|
// Send queries
|
|
|
|
|
process.stdout.write('\x1b[?u'); // Query progressive enhancement
|
|
|
|
|
process.stdout.write('\x1b[c'); // Query device attributes
|
|
|
|
|
|
2025-09-05 17:18:51 -07:00
|
|
|
// Timeout after 200ms
|
|
|
|
|
// When a iterm2 terminal does not have focus this can take over 90s on a
|
|
|
|
|
// fast macbook so we need a somewhat longer threshold than would be ideal.
|
|
|
|
|
timeoutId = setTimeout(onTimeout, 200);
|
2025-08-13 13:32:54 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function disableProtocol() {
|
|
|
|
|
if (protocolEnabled) {
|
|
|
|
|
process.stdout.write('\x1b[<u');
|
|
|
|
|
protocolEnabled = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isKittyProtocolEnabled(): boolean {
|
|
|
|
|
return protocolEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isKittyProtocolSupported(): boolean {
|
|
|
|
|
return protocolSupported;
|
|
|
|
|
}
|