Skip to content
Open
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
4 changes: 3 additions & 1 deletion packages/cli/src/ui/components/KeyboardShortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Box, Text } from 'ink';
import { theme } from '../semantic-colors.js';
import { useTerminalSize } from '../hooks/useTerminalSize.js';
import { t } from '../../i18n/index.js';
import { formatShortcut } from '../utils/shortcutFormatter.js';

interface Shortcut {
key: string;
Expand Down Expand Up @@ -47,7 +48,8 @@ const getShortcuts = (): Shortcut[] => [

const ShortcutItem: React.FC<{ shortcut: Shortcut }> = ({ shortcut }) => (
<Text color={theme.text.secondary}>
<Text color={theme.text.accent}>{shortcut.key}</Text> {shortcut.description}
<Text color={theme.text.accent}>{formatShortcut(shortcut.key)}</Text>{' '}
{shortcut.description}
</Text>
);

Expand Down
14 changes: 10 additions & 4 deletions packages/cli/src/ui/hooks/useGeminiStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { useSessionStats } from '../contexts/SessionContext.js';
import type { LoadedSettings } from '../../config/settings.js';
import { t } from '../../i18n/index.js';
import { useDualOutput } from '../../dualOutput/DualOutputContext.js';
import { formatShortcut } from '../utils/shortcutFormatter.js';

const debugLogger = createDebugLogger('GEMINI_STREAM');

Expand Down Expand Up @@ -867,8 +868,11 @@ export const useGeminiStream = (
);

if (!isShowingAutoRetry) {
const retryHint = t('Press Ctrl+Y to retry');
// Store error with hint as a pending item (not in history).
const retryKey = formatShortcut('ctrl+y');
const retryHint = t('Press Ctrl+Y to retry').replace(
'Ctrl+Y',
retryKey,
);
// This allows the hint to be removed when the user retries with Ctrl+Y,
// since pending items are in the dynamic rendering area (not <Static>).
setPendingRetryErrorItem({
Expand Down Expand Up @@ -1475,8 +1479,10 @@ export const useGeminiStream = (
onAuthError('Session expired or is unauthorized.');
} else if (!isNodeError(error) || error.name !== 'AbortError') {
lastPromptErroredRef.current = true;
const retryHint = t('Press Ctrl+Y to retry');
// Store error with hint as a pending item (same as handleErrorEvent)
const retryHint = t('Press Ctrl+Y to retry').replace(
'Ctrl+Y',
formatShortcut('ctrl+y'),
);
setPendingRetryErrorItem({
type: 'error' as const,
text: parseAndFormatApiError(
Expand Down
38 changes: 38 additions & 0 deletions packages/cli/src/ui/utils/shortcutFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, it, expect, vi, afterEach } from 'vitest';

describe('formatShortcut', () => {
const originalPlatform = process.platform;

afterEach(() => {
Object.defineProperty(process, 'platform', { value: originalPlatform });
vi.resetModules();
});

it('converts modifier keys to Mac symbols on darwin', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
const { formatShortcut } = await import('./shortcutFormatter.js');
expect(formatShortcut('ctrl+y')).toBe('⌃Y');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nice to have] Consider adding test cases for combined modifiers (ctrl+shift+y⌃⇧Y), single-character keys (!, /, @), and mixed-case input (Ctrl+Y) to catch regressions if future shortcuts use these patterns.

— Qwen Code /review

expect(formatShortcut('cmd+v')).toBe('⌘V');
expect(formatShortcut('alt+v')).toBe('⌥V');
expect(formatShortcut('shift+tab')).toBe('⇧TAB');
});

it('handles multi-part shortcuts on darwin', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });
const { formatShortcut } = await import('./shortcutFormatter.js');
expect(formatShortcut('esc esc')).toBe('ESC ESC');
});

it('returns input unchanged on non-darwin', async () => {
Object.defineProperty(process, 'platform', { value: 'linux' });
const { formatShortcut } = await import('./shortcutFormatter.js');
expect(formatShortcut('ctrl+y')).toBe('ctrl+y');
expect(formatShortcut('ctrl+c')).toBe('ctrl+c');
});
});
51 changes: 51 additions & 0 deletions packages/cli/src/ui/utils/shortcutFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

const isMac = process.platform === 'darwin';

/**
* Maps modifier names to macOS symbol equivalents.
*/
const MAC_MODIFIERS: Record<string, string> = {
ctrl: '⌃',
cmd: '⌘',
alt: '⌥',
shift: '⇧',
};

/**
* Formats a keyboard shortcut string for display, using macOS symbols when
* running on Darwin.
*
* Examples (on macOS):
* "ctrl+y" → "⌃Y"
* "cmd+v" → "⌘V"
* "ctrl+c" → "⌃C"
*
* On other platforms the input is returned unchanged.
*/
export function formatShortcut(shortcut: string): string {
if (!isMac) {
return shortcut;
}

return shortcut
.split(/\s+/)
.map((combo) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nice to have] combo.split('+') cannot represent the + key itself (e.g., ctrl++ for zoom-in would silently mangle). No current shortcut uses + as a key, but consider documenting this limitation or switching to a regex-based parser that matches known modifiers and leaves the remainder as the key.

— Qwen Code /review

const parts = combo.split('+');
let result = '';
for (const part of parts) {
const lower = part.toLowerCase();
if (MAC_MODIFIERS[lower]) {
result += MAC_MODIFIERS[lower];
} else {
result += part.toUpperCase();
}
}
return result;
})
.join(' ');
}
Loading