From 2143731f6efdf1aafff38ec249caf01a8bcd163e Mon Sep 17 00:00:00 2001
From: Jacob Richman <jacob314@gmail.com>
Date: Tue, 19 Aug 2025 13:41:08 -0700
Subject: fix(paste) incorrect handling of \\\n in pastes (#6532)

---
 packages/cli/src/ui/contexts/KeypressContext.tsx | 48 +++++++++++++-----------
 1 file changed, 26 insertions(+), 22 deletions(-)

(limited to 'packages/cli/src/ui/contexts/KeypressContext.tsx')

diff --git a/packages/cli/src/ui/contexts/KeypressContext.tsx b/packages/cli/src/ui/contexts/KeypressContext.tsx
index b1e70aed..eaf1819b 100644
--- a/packages/cli/src/ui/contexts/KeypressContext.tsx
+++ b/packages/cli/src/ui/contexts/KeypressContext.tsx
@@ -172,6 +172,29 @@ export function KeypressProvider({
     };
 
     const handleKeypress = (_: unknown, key: Key) => {
+      if (key.name === 'paste-start') {
+        isPaste = true;
+        return;
+      }
+      if (key.name === 'paste-end') {
+        isPaste = false;
+        broadcast({
+          name: '',
+          ctrl: false,
+          meta: false,
+          shift: false,
+          paste: true,
+          sequence: pasteBuffer.toString(),
+        });
+        pasteBuffer = Buffer.alloc(0);
+        return;
+      }
+
+      if (isPaste) {
+        pasteBuffer = Buffer.concat([pasteBuffer, Buffer.from(key.sequence)]);
+        return;
+      }
+
       if (key.name === 'return' && waitingForEnterAfterBackslash) {
         if (backslashTimeout) {
           clearTimeout(backslashTimeout);
@@ -278,29 +301,10 @@ export function KeypressProvider({
         }
       }
 
-      if (key.name === 'paste-start') {
-        isPaste = true;
-      } else if (key.name === 'paste-end') {
-        isPaste = false;
-        broadcast({
-          name: '',
-          ctrl: false,
-          meta: false,
-          shift: false,
-          paste: true,
-          sequence: pasteBuffer.toString(),
-        });
-        pasteBuffer = Buffer.alloc(0);
-      } else {
-        if (isPaste) {
-          pasteBuffer = Buffer.concat([pasteBuffer, Buffer.from(key.sequence)]);
-        } else {
-          if (key.name === 'return' && key.sequence === `${ESC}\r`) {
-            key.meta = true;
-          }
-          broadcast({ ...key, paste: isPaste });
-        }
+      if (key.name === 'return' && key.sequence === `${ESC}\r`) {
+        key.meta = true;
       }
+      broadcast({ ...key, paste: isPaste });
     };
 
     const handleRawKeypress = (data: Buffer) => {
-- 
cgit v1.2.3

