summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/components/shared
diff options
context:
space:
mode:
authorJacob Richman <[email protected]>2025-05-16 13:17:48 -0700
committerGitHub <[email protected]>2025-05-16 13:17:48 -0700
commit8b959c2060352182889e8e056de8a62a301778df (patch)
treea0191b5f16146ba7bc9102cda5337dec2fbd1bfc /packages/cli/src/ui/components/shared
parent1728bf3f44f7298c6f86366eee9a11913aca5aa0 (diff)
strip escape characters when pasting. (#386)
Diffstat (limited to 'packages/cli/src/ui/components/shared')
-rw-r--r--packages/cli/src/ui/components/shared/text-buffer.test.ts8
-rw-r--r--packages/cli/src/ui/components/shared/text-buffer.ts8
2 files changed, 15 insertions, 1 deletions
diff --git a/packages/cli/src/ui/components/shared/text-buffer.test.ts b/packages/cli/src/ui/components/shared/text-buffer.test.ts
index 8e35e3e9..acaa4179 100644
--- a/packages/cli/src/ui/components/shared/text-buffer.test.ts
+++ b/packages/cli/src/ui/components/shared/text-buffer.test.ts
@@ -495,6 +495,14 @@ describe('useTextBuffer', () => {
act(() => result.current.handleInput(undefined, { rightArrow: true })); // cursor [0,2]
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
+
+ it('should strip ANSI escape codes when pasting text', () => {
+ const { result } = renderHook(() => useTextBuffer({ viewport }));
+ const textWithAnsi = '\x1B[31mHello\x1B[0m \x1B[32mWorld\x1B[0m';
+ // Simulate pasting by calling handleInput with a string longer than 1 char
+ act(() => result.current.handleInput(textWithAnsi, {}));
+ expect(getBufferState(result).text).toBe('Hello World');
+ });
});
// More tests would be needed for:
diff --git a/packages/cli/src/ui/components/shared/text-buffer.ts b/packages/cli/src/ui/components/shared/text-buffer.ts
index 5f25f9e0..b13a57c6 100644
--- a/packages/cli/src/ui/components/shared/text-buffer.ts
+++ b/packages/cli/src/ui/components/shared/text-buffer.ts
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import stripAnsi from 'strip-ansi';
import { spawnSync } from 'child_process';
import fs from 'fs';
import os from 'os';
@@ -1127,7 +1128,12 @@ export function useTextBuffer({
)
backspace();
else if (key['delete']) del();
- else if (input && !key['ctrl'] && !key['meta']) insert(input);
+ else if (input && !key['ctrl'] && !key['meta']) {
+ // Heuristic for paste: if input is longer than 1 char (potential paste)
+ // strip ANSI escape codes.
+ const cleanedInput = input.length > 1 ? stripAnsi(input) : input;
+ insert(cleanedInput);
+ }
const textChanged = text !== beforeText;
// After operations, visualCursor might not be immediately updated if the change