xonecas.io
64875
Mc

mini-coder / commit 8db43be

feat(input): paste placeholder with expand-on-submit

Sean2026-02-23 8db43be04ec2c2de9bb423e9c855a769d678fcda parent 06ffe47

2 files changed

src/cli/input.test.ts+28−0
@@ -0,0 +1,28 @@
1+import { describe, expect, test } from "bun:test";
2+import { pasteLabel } from "./input.ts";
3+
4+describe("pasteLabel", () => {
5+ test("single short line", () => {
6+ expect(pasteLabel("hello world")).toBe('[pasted: "hello world"]');
7+ });
8+
9+ test("single line truncated at 40 chars", () => {
10+ const long = "a".repeat(50);
11+ const label = pasteLabel(long);
12+ expect(label).toBe(`[pasted: "${"a".repeat(40)}…"]`);
13+ });
14+
15+ test("multi-line shows extra count", () => {
16+ expect(pasteLabel("line one\nline two\nline three")).toBe(
17+ '[pasted: "line one" +2 more lines]',
18+ );
19+ });
20+
21+ test("two lines says singular", () => {
22+ expect(pasteLabel("first\nsecond")).toBe('[pasted: "first" +1 more line]');
23+ });
24+
25+ test("empty string", () => {
26+ expect(pasteLabel("")).toBe('[pasted: ""]');
27+ });
28+});
src/cli/input.ts+55−5
@@ -102,6 +102,20 @@ async function readKey(reader: StreamReader): Promise<string> {
102102 return new TextDecoder().decode(value);
103103 }
104104
105+// ─── Paste handling ───────────────────────────────────────────────────────────
106+
107+const PASTE_SENTINEL = "\x00PASTE\x00";
108+const PASTE_SENTINEL_LEN = PASTE_SENTINEL.length;
109+
110+export function pasteLabel(text: string): string {
111+ const lines = text.split("\n");
112+ const first = lines[0] ?? "";
113+ const preview = first.length > 40 ? `${first.slice(0, 40)}…` : first;
114+ const extra = lines.length - 1;
115+ const more = extra > 0 ? ` +${extra} more line${extra === 1 ? "" : "s"}` : "";
116+ return `[pasted: "${preview}"${more}]`;
117+}
118+
105119 // ─── Main readline function ───────────────────────────────────────────────────
106120
107121 const PROMPT = c.green("▶ ");
@@ -123,6 +137,7 @@ export async function readline(opts: {
123137 let savedInput = ""; // saved current line when navigating history
124138 let searchMode = false;
125139 let searchQuery = "";
140+ let pasteBuffer: string | null = null; // full text of a pending paste
126141
127142 process.stdin.setRawMode(true);
128143 process.stdin.resume();
@@ -131,14 +146,27 @@ export async function readline(opts: {
131146
132147 function renderPrompt(): void {
133148 const cols = process.stdout.columns ?? 80;
149+ // Replace the sentinel with a styled placeholder for display
150+ const visualBuf = pasteBuffer
151+ ? buf.replace(PASTE_SENTINEL, c.dim(pasteLabel(pasteBuffer)))
152+ : buf;
153+ // For cursor positioning we need the visual length without the sentinel substitution
154+ const visualCursor = pasteBuffer
155+ ? (() => {
156+ const sentinelPos = buf.indexOf(PASTE_SENTINEL);
157+ if (sentinelPos === -1 || cursor <= sentinelPos) return cursor;
158+ // cursor is after the sentinel: shift by (label len - sentinel len)
159+ return cursor - PASTE_SENTINEL_LEN + pasteLabel(pasteBuffer).length;
160+ })()
161+ : cursor;
134162 const display =
135- buf.length > cols - PROMPT_RAW_LEN - 2
136- ? `…${buf.slice(-(cols - PROMPT_RAW_LEN - 3))}`
137- : buf;
163+ visualBuf.length > cols - PROMPT_RAW_LEN - 2
164+ ? `…${visualBuf.slice(-(cols - PROMPT_RAW_LEN - 3))}`
165+ : visualBuf;
138166
139167 const prompt = opts.planMode ? PROMPT_PLAN : PROMPT;
140168 process.stdout.write(
141- `${CLEAR_LINE}${prompt}${display}${CSI}${PROMPT_RAW_LEN + cursor + 1}G`,
169+ `${CLEAR_LINE}${prompt}${display}${CSI}${PROMPT_RAW_LEN + visualCursor + 1}G`,
142170 );
143171 }
144172
@@ -282,6 +310,7 @@ export async function readline(opts: {
282310 while (cursor > 0 && buf[cursor - 1] === " ") cursor--;
283311 while (cursor > 0 && buf[cursor - 1] !== " ") cursor--;
284312 buf = buf.slice(0, cursor) + buf.slice(end);
313+ if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
285314 renderPrompt();
286315 continue;
287316 }
@@ -289,12 +318,14 @@ export async function readline(opts: {
289318 if (raw === CTRL_U) {
290319 buf = buf.slice(cursor);
291320 cursor = 0;
321+ if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
292322 renderPrompt();
293323 continue;
294324 }
295325
296326 if (raw === CTRL_K) {
297327 buf = buf.slice(0, cursor);
328+ if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
298329 renderPrompt();
299330 continue;
300331 }
@@ -316,6 +347,7 @@ export async function readline(opts: {
316347 if (cursor > 0) {
317348 buf = buf.slice(0, cursor - 1) + buf.slice(cursor);
318349 cursor--;
350+ if (pasteBuffer && !buf.includes(PASTE_SENTINEL)) pasteBuffer = null;
319351 renderPrompt();
320352 }
321353 continue;
@@ -346,7 +378,12 @@ export async function readline(opts: {
346378
347379 // ── Enter — submit ────────────────────────────────────────────────────
348380 if (raw === ENTER || raw === NEWLINE) {
349- const text = buf.trim();
381+ // Expand paste sentinel before trimming/submitting
382+ const expanded = pasteBuffer
383+ ? buf.replace(PASTE_SENTINEL, pasteBuffer)
384+ : buf;
385+ pasteBuffer = null;
386+ const text = expanded.trim();
350387 process.stdout.write("\n");
351388 buf = "";
352389 cursor = 0;
@@ -376,6 +413,19 @@ export async function readline(opts: {
376413 return { type: "submit", text };
377414 }
378415
416+ // ── Paste detection ───────────────────────────────────────────────────
417+ // A chunk with more than one character that isn't an escape sequence is
418+ // almost certainly a clipboard paste. Capture it and show a placeholder.
419+ if (raw.length > 1) {
420+ // Strip trailing newline that some terminals append on paste
421+ const pasted = raw.replace(/\r?\n$/, "");
422+ pasteBuffer = pasted;
423+ buf = buf.slice(0, cursor) + PASTE_SENTINEL + buf.slice(cursor);
424+ cursor += PASTE_SENTINEL_LEN;
425+ renderPrompt();
426+ continue;
427+ }
428+
379429 // ── Printable characters ──────────────────────────────────────────────
380430 if (raw >= " " || raw === "\t") {
381431 buf = buf.slice(0, cursor) + raw + buf.slice(cursor);