From d622e596a14338771bdd43a8812ff7fc02f7ebe8 Mon Sep 17 00:00:00 2001 From: Matias <1847940+matias-casal@users.noreply.github.com> Date: Wed, 16 Jul 2025 00:35:58 -0300 Subject: [PATCH] feat(cli): clear input buffer on CTRL+C when not executing commands (#1729) Co-authored-by: Scott Densmore --- .../src/ui/components/InputPrompt.test.tsx | 26 +++++++++++++++++++ .../cli/src/ui/components/InputPrompt.tsx | 11 ++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/cli/src/ui/components/InputPrompt.test.tsx b/packages/cli/src/ui/components/InputPrompt.test.tsx index 53a2cb0e..e1d68125 100644 --- a/packages/cli/src/ui/components/InputPrompt.test.tsx +++ b/packages/cli/src/ui/components/InputPrompt.test.tsx @@ -543,4 +543,30 @@ describe('InputPrompt', () => { expect(props.buffer.newline).toHaveBeenCalled(); unmount(); }); + + it('should clear the buffer on Ctrl+C if it has text', async () => { + props.buffer.setText('some text to clear'); + const { stdin, unmount } = render(); + await wait(); + + stdin.write('\x03'); // Ctrl+C character + await wait(); + + expect(props.buffer.setText).toHaveBeenCalledWith(''); + expect(mockCompletion.resetCompletionState).toHaveBeenCalled(); + expect(props.onSubmit).not.toHaveBeenCalled(); + unmount(); + }); + + it('should NOT clear the buffer on Ctrl+C if it is empty', async () => { + props.buffer.text = ''; + const { stdin, unmount } = render(); + await wait(); + + stdin.write('\x03'); // Ctrl+C character + await wait(); + + expect(props.buffer.setText).not.toHaveBeenCalled(); + unmount(); + }); }); diff --git a/packages/cli/src/ui/components/InputPrompt.tsx b/packages/cli/src/ui/components/InputPrompt.tsx index 5edda273..41c88d6c 100644 --- a/packages/cli/src/ui/components/InputPrompt.tsx +++ b/packages/cli/src/ui/components/InputPrompt.tsx @@ -356,6 +356,16 @@ export const InputPrompt: React.FC = ({ } if (key.ctrl && key.name === 'e') { buffer.move('end'); + buffer.moveToOffset(cpLen(buffer.text)); + return; + } + // Ctrl+C (Clear input) + if (key.ctrl && key.name === 'c') { + if (buffer.text.length > 0) { + buffer.setText(''); + resetCompletionState(); + return; + } return; } @@ -397,6 +407,7 @@ export const InputPrompt: React.FC = ({ handleSubmitAndClear, shellHistory, handleClipboardImage, + resetCompletionState, ], );