From b43a837c6a17046c182309cab61ff2193f71f2db Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 21 May 2025 12:12:37 -0600 Subject: [PATCH] fix: pasting in prompt no worky --- cli/prompts.ts | 66 ++++++++++++++++++++++++++++++++------------------ deno.json | 2 +- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/cli/prompts.ts b/cli/prompts.ts index 0d23d65..1d643d4 100644 --- a/cli/prompts.ts +++ b/cli/prompts.ts @@ -43,39 +43,57 @@ export async function cliPrompt( render(); - const buf = new Uint8Array(6); // 6 bytes is enough for all the keys + const buf = new Uint8Array(64); // large enough for most pastes while (true) { const n = await Deno.stdin.read(buf); if (n === null) break; - const [a, b, c] = buf; - if (a === 3) { // Ctrl+C - block?.clear(); - block?.["layout"]?.clearAll(); - Deno.stdin.setRaw(false); - Deno.exit(130); - } + for (let i = 0; i < n; i++) { + const byte = buf[i]; - if (a === 13) { // Enter - break; - } else if (a === 127 || a === 8) { // Backspace - if (cursorPos > 0) { - input.splice(cursorPos - 1, 1); - cursorPos--; + // Ctrl+C + if (byte === 3) { + block?.clear(); + block?.["layout"]?.clearAll(); + await Deno.stdin.setRaw(false); + Deno.exit(130); } - } else if (a === 46) { // Delete - if (cursorPos < input.length) { + + // Escape sequence? + if (byte === 27 && i + 1 < n && buf[i + 1] === 91) { + const third = buf[i + 2]; + if (third === 68 && cursorPos > 0) cursorPos--; // Left + else if (third === 67 && cursorPos < input.length) cursorPos++; // Right + else if (third === 51 && i + 3 < n && buf[i + 3] === 126) { // Delete + if (cursorPos < input.length) input.splice(cursorPos, 1); + i += 1; // consume tilde + } + i += 2; // consume ESC [ X + continue; + } + + // Backspace + if (byte === 127 || byte === 8) { + if (cursorPos > 0) { + input.splice(cursorPos - 1, 1); + cursorPos--; + } + continue; + } + + // Delete (ASCII 46) + if (byte === 46 && cursorPos < input.length) { input.splice(cursorPos, 1); + continue; } - } else if (a === 27 && b === 91) { // Arrow keys - if (c === 51 && cursorPos < input.length) { // delete - input.splice(cursorPos, 1); + + // Printable + if (byte >= 32 && byte <= 126) { + input.splice(cursorPos, 0, String.fromCharCode(byte)); + cursorPos++; } - if (c === 68 && cursorPos > 0) cursorPos--; // Left - if (c === 67 && cursorPos < input.length) cursorPos++; // Right - } else if (a >= 32 && a <= 126) { // Printable ASCII - input.splice(cursorPos, 0, String.fromCharCode(a)); - cursorPos++; + + // Other cases: ignore } render(); diff --git a/deno.json b/deno.json index cbd45d6..687c07f 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@bearmetal/pdf-tools", - "version": "1.0.4", + "version": "1.0.5", "license": "GPL 3.0", "tasks": { "dev": "deno run -A --env-file=.env main.ts", -- 2.47.2