xonecas.io
64875
Mc

mini-coder / commit 46de3d2

feat: add bracketed paste support in readline

Sean2026-02-23 46de3d299f921a4f51be8d2c8749381c1fd4a2eb parent b545ff0

1 file changed

src/cli/input.ts+37−0
@@ -18,6 +18,10 @@ const CURSOR_UP = `${CSI}A`;
1818 const CURSOR_DOWN = `${CSI}B`;
1919 const SAVE_CURSOR = `${CSI}s`;
2020 const RESTORE_CURSOR = `${CSI}u`;
21+const BPASTE_ENABLE = `${ESC}[?2004h`;
22+const BPASTE_DISABLE = `${ESC}[?2004l`;
23+const BPASTE_START = `${ESC}[200~`;
24+const BPASTE_END = `${ESC}[201~`;
2125
2226 // ─── Keycodes ─────────────────────────────────────────────────────────────────
2327
@@ -200,6 +204,7 @@ export async function readline(opts: {
200204
201205 process.stdin.setRawMode(true);
202206 process.stdin.resume();
207+ process.stdout.write(BPASTE_ENABLE);
203208
204209 const reader = getStdinReader();
205210
@@ -291,6 +296,37 @@ export async function readline(opts: {
291296 continue;
292297 }
293298
299+ // ── Bracketed paste ────────────────────────────────────────────────
300+ if (raw.includes(BPASTE_START)) {
301+ // Collect the full paste, potentially across multiple readKey chunks
302+ let accumulated = raw.slice(
303+ raw.indexOf(BPASTE_START) + BPASTE_START.length,
304+ );
305+ while (!accumulated.includes(BPASTE_END)) {
306+ const next = await readKey(reader);
307+ if (!next) break;
308+ accumulated += next;
309+ }
310+ const endIdx = accumulated.indexOf(BPASTE_END);
311+ const pasted =
312+ endIdx !== -1 ? accumulated.slice(0, endIdx) : accumulated;
313+
314+ const imageResult = await tryExtractImageFromPaste(pasted, cwd);
315+ if (imageResult) {
316+ imageAttachments.push(imageResult.attachment);
317+ buf = buf.slice(0, cursor) + imageResult.label + buf.slice(cursor);
318+ cursor += imageResult.label.length;
319+ renderPrompt();
320+ continue;
321+ }
322+
323+ pasteBuffer = pasted;
324+ buf = buf.slice(0, cursor) + PASTE_SENTINEL + buf.slice(cursor);
325+ cursor += PASTE_SENTINEL_LEN;
326+ renderPrompt();
327+ continue;
328+ }
329+
294330 // ── Escape sequences ──────────────────────────────────────────────────
295331 if (raw.startsWith(ESC)) {
296332 // Arrow keys: ESC [ A/B/C/D
@@ -525,6 +561,7 @@ export async function readline(opts: {
525561 } finally {
526562 // Do NOT cancel the shared reader — it is reused across readline() calls.
527563 // Just reset raw mode and pause stdin until the next prompt.
564+ process.stdout.write(BPASTE_DISABLE);
528565 process.stdin.setRawMode(false);
529566 process.stdin.pause();
530567 }