Right click to paste in Alternate Buffer mode (#13234)

This commit is contained in:
Tommaso Sciortino
2025-11-17 15:48:33 -08:00
committed by GitHub
parent 1d1bdc57ce
commit 8877c85278
10 changed files with 172 additions and 523 deletions
@@ -24,6 +24,7 @@ import type { UseInputHistoryReturn } from '../hooks/useInputHistory.js';
import { useInputHistory } from '../hooks/useInputHistory.js';
import type { UseReverseSearchCompletionReturn } from '../hooks/useReverseSearchCompletion.js';
import { useReverseSearchCompletion } from '../hooks/useReverseSearchCompletion.js';
import clipboardy from 'clipboardy';
import * as clipboardUtils from '../utils/clipboardUtils.js';
import { useKittyKeyboardProtocol } from '../hooks/useKittyKeyboardProtocol.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
@@ -35,6 +36,7 @@ vi.mock('../hooks/useShellHistory.js');
vi.mock('../hooks/useCommandCompletion.js');
vi.mock('../hooks/useInputHistory.js');
vi.mock('../hooks/useReverseSearchCompletion.js');
vi.mock('clipboardy');
vi.mock('../utils/clipboardUtils.js');
vi.mock('../hooks/useKittyKeyboardProtocol.js');
@@ -146,6 +148,7 @@ describe('InputPrompt', () => {
deleteWordLeft: vi.fn(),
deleteWordRight: vi.fn(),
visualToLogicalMap: [[0, 0]],
getOffset: vi.fn().mockReturnValue(0),
} as unknown as TextBuffer;
mockShellHistory = {
@@ -505,6 +508,7 @@ describe('InputPrompt', () => {
// Set initial text and cursor position
mockBuffer.text = 'Hello world';
mockBuffer.cursor = [0, 5]; // Cursor after "Hello"
vi.mocked(mockBuffer.getOffset).mockReturnValue(5);
mockBuffer.lines = ['Hello world'];
mockBuffer.replaceRangeByOffset = vi.fn();
@@ -559,6 +563,32 @@ describe('InputPrompt', () => {
});
});
describe('clipboard text paste', () => {
it('should insert text from clipboard on Ctrl+V', async () => {
vi.mocked(clipboardUtils.clipboardHasImage).mockResolvedValue(false);
vi.mocked(clipboardy.read).mockResolvedValue('pasted text');
vi.mocked(mockBuffer.replaceRangeByOffset).mockClear();
const { stdin, unmount } = renderWithProviders(
<InputPrompt {...props} />,
);
await act(async () => {
stdin.write('\x16'); // Ctrl+V
});
await waitFor(() => {
expect(clipboardy.read).toHaveBeenCalled();
expect(mockBuffer.replaceRangeByOffset).toHaveBeenCalledWith(
expect.any(Number),
expect.any(Number),
'pasted text',
);
});
unmount();
});
});
it.each([
{
name: 'should complete a partial parent command',
+15 -14
View File
@@ -5,6 +5,7 @@
*/
import type React from 'react';
import clipboardy from 'clipboardy';
import { useCallback, useEffect, useState, useRef } from 'react';
import { Box, Text, getBoundingBox, type DOMElement } from 'ink';
import { SuggestionsDisplay, MAX_WIDTH } from './SuggestionsDisplay.js';
@@ -315,7 +316,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
}, [buffer, popAllMessages, inputHistory]);
// Handle clipboard image pasting with Ctrl+V
const handleClipboardImage = useCallback(async () => {
const handleClipboardPaste = useCallback(async () => {
try {
if (await clipboardHasImage()) {
const imagePath = await saveClipboardImage(config.getTargetDir());
@@ -331,14 +332,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// Insert @path reference at cursor position
const insertText = `@${relativePath}`;
const currentText = buffer.text;
const [row, col] = buffer.cursor;
// Calculate offset from row/col
let offset = 0;
for (let i = 0; i < row; i++) {
offset += buffer.lines[i].length + 1; // +1 for newline
}
offset += col;
const offset = buffer.getOffset();
// Add spaces around the path if needed
let textToInsert = insertText;
@@ -355,8 +349,13 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// Insert at cursor position
buffer.replaceRangeByOffset(offset, offset, textToInsert);
return;
}
}
const textToInsert = await clipboardy.read();
const offset = buffer.getOffset();
buffer.replaceRangeByOffset(offset, offset, textToInsert);
} catch (error) {
console.error('Error handling clipboard image:', error);
}
@@ -381,10 +380,12 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
buffer.moveToVisualPosition(visualRow, relX);
return true;
}
} else if (event.name === 'right-release') {
handleClipboardPaste();
}
return false;
},
[buffer],
[buffer, handleClipboardPaste],
);
useMouse(handleMouse, { isActive: focus && !isEmbeddedShellFocused });
@@ -772,9 +773,9 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
return;
}
// Ctrl+V for clipboard image paste
if (keyMatchers[Command.PASTE_CLIPBOARD_IMAGE](key)) {
handleClipboardImage();
// Ctrl+V for clipboard paste
if (keyMatchers[Command.PASTE_CLIPBOARD](key)) {
handleClipboardPaste();
return;
}
@@ -805,7 +806,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
handleSubmit,
shellHistory,
reverseSearchCompletion,
handleClipboardImage,
handleClipboardPaste,
resetCompletionState,
showEscapePrompt,
resetEscapeState,
@@ -2040,6 +2040,11 @@ export function useTextBuffer({
[visualLayout],
);
const getOffset = useCallback(
(): number => logicalPosToOffset(lines, cursorRow, cursorCol),
[lines, cursorRow, cursorCol],
);
const returnValue: TextBuffer = useMemo(
() => ({
lines,
@@ -2065,6 +2070,7 @@ export function useTextBuffer({
replaceRange,
replaceRangeByOffset,
moveToOffset,
getOffset,
moveToVisualPosition,
deleteWordLeft,
deleteWordRight,
@@ -2129,6 +2135,7 @@ export function useTextBuffer({
replaceRange,
replaceRangeByOffset,
moveToOffset,
getOffset,
moveToVisualPosition,
deleteWordLeft,
deleteWordRight,
@@ -2283,6 +2290,7 @@ export interface TextBuffer {
endOffset: number,
replacementText: string,
) => void;
getOffset: () => number;
moveToOffset(offset: number): void;
moveToVisualPosition(visualRow: number, visualCol: number): void;