Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions packages/cli/src/ui/utils/terminalCapabilityManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
enableKittyKeyboardProtocol,
enableModifyOtherKeys,
} from '@google/gemini-cli-core';
import * as fs from 'node:fs';

// Mock fs
vi.mock('node:fs', () => ({
Expand Down Expand Up @@ -289,5 +290,16 @@ describe('TerminalCapabilityManager', () => {
expect(manager.isKittyProtocolEnabled()).toBe(false);
expect(enableModifyOtherKeys).not.toHaveBeenCalled();
});

it('should wrap queries in hidden/clear sequence', async () => {
const manager = TerminalCapabilityManager.getInstance();
void manager.detectCapabilities();

expect(fs.writeSync).toHaveBeenCalledWith(
expect.anything(),
// eslint-disable-next-line no-control-regex
expect.stringMatching(/^\x1b\[8m.*\x1b\[2K\r\x1b\[0m$/s),
);
Comment thread
srithreepo marked this conversation as resolved.
});
});
});
15 changes: 13 additions & 2 deletions packages/cli/src/ui/utils/terminalCapabilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class TerminalCapabilityManager {
private static readonly TERMINAL_NAME_QUERY = '\x1b[>q';
private static readonly DEVICE_ATTRIBUTES_QUERY = '\x1b[c';
private static readonly MODIFY_OTHER_KEYS_QUERY = '\x1b[>4;?m';
private static readonly HIDDEN_MODE = '\x1b[8m';
private static readonly CLEAR_LINE_AND_RETURN = '\x1b[2K\r';
private static readonly RESET_ATTRIBUTES = '\x1b[0m';

/**
* Triggers a terminal background color query.
Expand Down Expand Up @@ -219,11 +222,19 @@ export class TerminalCapabilityManager {
try {
fs.writeSync(
process.stdout.fd,
TerminalCapabilityManager.KITTY_QUERY +
// Use hidden mode to prevent potential "m" character from being printed
// to the terminal during startup when querying for modifyOtherKeys.
// This can happen on some terminals that might echo the query or
// malform the response. We hide the output, send queries, then
// immediately clear the line and reset attributes.
TerminalCapabilityManager.HIDDEN_MODE +
TerminalCapabilityManager.KITTY_QUERY +
TerminalCapabilityManager.OSC_11_QUERY +
TerminalCapabilityManager.TERMINAL_NAME_QUERY +
TerminalCapabilityManager.MODIFY_OTHER_KEYS_QUERY +
TerminalCapabilityManager.DEVICE_ATTRIBUTES_QUERY,
TerminalCapabilityManager.DEVICE_ATTRIBUTES_QUERY +
TerminalCapabilityManager.CLEAR_LINE_AND_RETURN +
TerminalCapabilityManager.RESET_ATTRIBUTES,
);
} catch (e) {
debugLogger.warn('Failed to write terminal capability queries:', e);
Expand Down
Loading