Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions packages/cli/src/ui/contexts/KeypressContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1256,13 +1256,13 @@ describe('KeypressContext - Kitty Protocol', () => {
});

expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer accumulating:',
'[DEBUG] CSI buffer accumulating:',
expect.stringContaining('\x1b[27u'),
);
const parsedCall = consoleLogSpy.mock.calls.find(
(args) =>
typeof args[0] === 'string' &&
args[0].includes('[DEBUG] Kitty sequence parsed successfully'),
args[0].includes('[DEBUG] CSI sequence parsed successfully'),
);
expect(parsedCall).toBeTruthy();
expect(parsedCall?.[1]).toEqual(expect.stringContaining('\x1b[27u'));
Expand Down Expand Up @@ -1293,7 +1293,7 @@ describe('KeypressContext - Kitty Protocol', () => {
});

expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer overflow, clearing:',
'[DEBUG] CSI buffer overflow, clearing:',
expect.any(String),
);
});
Expand Down Expand Up @@ -1384,13 +1384,13 @@ describe('KeypressContext - Kitty Protocol', () => {

// Verify debug logging for accumulation
expect(consoleLogSpy).toHaveBeenCalledWith(
'[DEBUG] Kitty buffer accumulating:',
'[DEBUG] CSI buffer accumulating:',
sequence,
);

// Verify warning for char codes
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Kitty sequence buffer has char codes:',
'CSI sequence buffer has char codes:',
[27, 91, 49, 50],
);
});
Expand Down Expand Up @@ -1468,6 +1468,29 @@ describe('KeypressContext - Kitty Protocol', () => {
);
},
);

it('should recognize Shift+Tab in non-Kitty protocol mode (Windows PowerShell)', () => {
const keyHandler = vi.fn();

// Create a wrapper with Kitty protocol disabled to simulate Windows PowerShell
const nonKittyWrapper = ({ children }: { children: React.ReactNode }) => (
<KeypressProvider kittyProtocolEnabled={false}>
{children}
</KeypressProvider>
);

const { result } = renderHook(() => useKeypressContext(), {
wrapper: nonKittyWrapper,
});
act(() => result.current.subscribe(keyHandler));

// Send legacy reverse Tab sequence (ESC [ Z)
act(() => stdin.sendKittySequence(`\x1b[Z`));

expect(keyHandler).toHaveBeenCalledWith(
expect.objectContaining({ name: 'tab', shift: true }),
);
});
});

describe('Double-tap and batching', () => {
Expand Down
156 changes: 79 additions & 77 deletions packages/cli/src/ui/contexts/KeypressContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -508,95 +508,97 @@ export function KeypressProvider({
return;
}

if (kittyProtocolEnabled) {
if (
kittySequenceBuffer ||
(key.sequence.startsWith(`${ESC}[`) &&
!key.sequence.startsWith(PASTE_MODE_PREFIX) &&
!key.sequence.startsWith(PASTE_MODE_SUFFIX) &&
!key.sequence.startsWith(FOCUS_IN) &&
!key.sequence.startsWith(FOCUS_OUT))
) {
kittySequenceBuffer += key.sequence;
// Parse CSI sequences for both Kitty protocol and legacy terminals
// This ensures Shift+Tab and other special keys work correctly even when
// Kitty protocol is not available (e.g., Windows PowerShell)
if (
kittySequenceBuffer ||
(key.sequence &&
key.sequence.startsWith(`${ESC}[`) &&
!key.sequence.startsWith(PASTE_MODE_PREFIX) &&
!key.sequence.startsWith(PASTE_MODE_SUFFIX) &&
!key.sequence.startsWith(FOCUS_IN) &&
!key.sequence.startsWith(FOCUS_OUT))
) {
kittySequenceBuffer += key.sequence;

if (debugKeystrokeLogging) {
console.log(
'[DEBUG] Kitty buffer accumulating:',
kittySequenceBuffer,
);
}
if (debugKeystrokeLogging) {
console.log('[DEBUG] CSI buffer accumulating:', kittySequenceBuffer);
}

// Try to peel off as many complete sequences as are available at the
// start of the buffer. This handles batched inputs cleanly. If the
// prefix is incomplete or invalid, skip to the next CSI introducer
// (ESC[) so that a following valid sequence can still be parsed.
let parsedAny = false;
while (kittySequenceBuffer) {
const parsed = parseKittyPrefix(kittySequenceBuffer);
if (!parsed) {
// Look for the next potential CSI start beyond index 0
const nextStart = kittySequenceBuffer.indexOf(`${ESC}[`, 1);
if (nextStart > 0) {
if (debugKeystrokeLogging) {
console.log(
'[DEBUG] Skipping incomplete/invalid CSI prefix:',
kittySequenceBuffer.slice(0, nextStart),
);
}
kittySequenceBuffer = kittySequenceBuffer.slice(nextStart);
continue;
}
break;
}
if (debugKeystrokeLogging) {
const parsedSequence = kittySequenceBuffer.slice(
0,
parsed.length,
);
if (kittySequenceBuffer.length > parsed.length) {
console.log(
'[DEBUG] Kitty sequence parsed successfully (prefix):',
parsedSequence,
);
} else {
// Try to peel off as many complete sequences as are available at the
// start of the buffer. This handles batched inputs cleanly. If the
// prefix is incomplete or invalid, skip to the next CSI introducer
// (ESC[) so that a following valid sequence can still be parsed.
let parsedAny = false;
while (kittySequenceBuffer) {
const parsed = parseKittyPrefix(kittySequenceBuffer);
if (!parsed) {
// Look for the next potential CSI start beyond index 0
const nextStart = kittySequenceBuffer.indexOf(`${ESC}[`, 1);
if (nextStart > 0) {
if (debugKeystrokeLogging) {
console.log(
'[DEBUG] Kitty sequence parsed successfully:',
parsedSequence,
'[DEBUG] Skipping incomplete/invalid CSI prefix:',
kittySequenceBuffer.slice(0, nextStart),
);
}
kittySequenceBuffer = kittySequenceBuffer.slice(nextStart);
continue;
}
// Consume the parsed prefix and broadcast it.
kittySequenceBuffer = kittySequenceBuffer.slice(parsed.length);
broadcast(parsed.key);
parsedAny = true;
break;
}
if (parsedAny) return;

if (config?.getDebugMode() || debugKeystrokeLogging) {
const codes = Array.from(kittySequenceBuffer).map((ch) =>
ch.charCodeAt(0),
);
console.warn('Kitty sequence buffer has char codes:', codes);
}

if (kittySequenceBuffer.length > MAX_KITTY_SEQUENCE_LENGTH) {
if (debugKeystrokeLogging) {
if (debugKeystrokeLogging) {
const parsedSequence = kittySequenceBuffer.slice(0, parsed.length);
if (kittySequenceBuffer.length > parsed.length) {
console.log(
'[DEBUG] Kitty buffer overflow, clearing:',
kittySequenceBuffer,
'[DEBUG] CSI sequence parsed successfully (prefix):',
parsedSequence,
);
}
if (config) {
const event = new KittySequenceOverflowEvent(
kittySequenceBuffer.length,
kittySequenceBuffer,
} else {
console.log(
'[DEBUG] CSI sequence parsed successfully:',
parsedSequence,
);
logKittySequenceOverflow(config, event);
}
kittySequenceBuffer = '';
} else {
return;
}
// Consume the parsed prefix and broadcast it.
kittySequenceBuffer = kittySequenceBuffer.slice(parsed.length);
broadcast(parsed.key);
parsedAny = true;
}
if (parsedAny) return;

if (config?.getDebugMode() || debugKeystrokeLogging) {
const codes = Array.from(kittySequenceBuffer).map((ch) =>
ch.charCodeAt(0),
);
console.warn('CSI sequence buffer has char codes:', codes);
}

if (
kittyProtocolEnabled &&
kittySequenceBuffer.length > MAX_KITTY_SEQUENCE_LENGTH
) {
if (debugKeystrokeLogging) {
console.log(
'[DEBUG] CSI buffer overflow, clearing:',
kittySequenceBuffer,
);
}
if (config) {
const event = new KittySequenceOverflowEvent(
kittySequenceBuffer.length,
kittySequenceBuffer,
);
logKittySequenceOverflow(config, event);
}
kittySequenceBuffer = '';
} else if (!kittyProtocolEnabled) {
// For non-Kitty terminals, clear the buffer to avoid accumulation
kittySequenceBuffer = '';
} else {
return;
}
}

Expand Down