Skip to content

Commit d622e59

Browse files
feat(cli): clear input buffer on CTRL+C when not executing commands (google-gemini#1729)
Co-authored-by: Scott Densmore <scottdensmore@mac.com>
1 parent 0903421 commit d622e59

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

packages/cli/src/ui/components/InputPrompt.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,30 @@ describe('InputPrompt', () => {
543543
expect(props.buffer.newline).toHaveBeenCalled();
544544
unmount();
545545
});
546+
547+
it('should clear the buffer on Ctrl+C if it has text', async () => {
548+
props.buffer.setText('some text to clear');
549+
const { stdin, unmount } = render(<InputPrompt {...props} />);
550+
await wait();
551+
552+
stdin.write('\x03'); // Ctrl+C character
553+
await wait();
554+
555+
expect(props.buffer.setText).toHaveBeenCalledWith('');
556+
expect(mockCompletion.resetCompletionState).toHaveBeenCalled();
557+
expect(props.onSubmit).not.toHaveBeenCalled();
558+
unmount();
559+
});
560+
561+
it('should NOT clear the buffer on Ctrl+C if it is empty', async () => {
562+
props.buffer.text = '';
563+
const { stdin, unmount } = render(<InputPrompt {...props} />);
564+
await wait();
565+
566+
stdin.write('\x03'); // Ctrl+C character
567+
await wait();
568+
569+
expect(props.buffer.setText).not.toHaveBeenCalled();
570+
unmount();
571+
});
546572
});

packages/cli/src/ui/components/InputPrompt.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
356356
}
357357
if (key.ctrl && key.name === 'e') {
358358
buffer.move('end');
359+
buffer.moveToOffset(cpLen(buffer.text));
360+
return;
361+
}
362+
// Ctrl+C (Clear input)
363+
if (key.ctrl && key.name === 'c') {
364+
if (buffer.text.length > 0) {
365+
buffer.setText('');
366+
resetCompletionState();
367+
return;
368+
}
359369
return;
360370
}
361371

@@ -397,6 +407,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
397407
handleSubmitAndClear,
398408
shellHistory,
399409
handleClipboardImage,
410+
resetCompletionState,
400411
],
401412
);
402413

0 commit comments

Comments
 (0)