diff --git a/cli/unstable_prompt_select.ts b/cli/unstable_prompt_select.ts index c46970a01dfa..b1efeb69f78a 100644 --- a/cli/unstable_prompt_select.ts +++ b/cli/unstable_prompt_select.ts @@ -13,7 +13,7 @@ const CR = "\r"; const INDICATOR = "❯"; const PADDING = " ".repeat(INDICATOR.length); -const CLR = "\r\u001b[K"; // Clear the current line +const CLR_ALL = "\x1b[J"; // Clear all lines after cursor const encoder = new TextEncoder(); const decoder = new TextDecoder(); @@ -66,10 +66,12 @@ export function promptSelect( case CR: break loop; } - Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length))); + Deno.stdout.writeSync(encoder.encode(`\x1b[${length}A`)); // move cursor after message + Deno.stdout.writeSync(encoder.encode(CLR_ALL)); } if (clear) { - Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length + 1))); // clear values and message + Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`)); // move cursor before message + Deno.stdout.writeSync(encoder.encode(CLR_ALL)); } Deno.stdin.setRaw(false); return values[selectedIndex] ?? null; diff --git a/cli/unstable_prompt_select_test.ts b/cli/unstable_prompt_select_test.ts index 2aa03c6429d4..d03482875af6 100644 --- a/cli/unstable_prompt_select_test.ts +++ b/cli/unstable_prompt_select_test.ts @@ -65,11 +65,13 @@ Deno.test("promptSelect() handles arrow down", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", @@ -125,11 +127,13 @@ Deno.test("promptSelect() handles arrow up", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", "❯ safari\r\n", " chrome\r\n", " firefox\r\n", @@ -185,7 +189,8 @@ Deno.test("promptSelect() handles up index overflow", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", @@ -240,15 +245,18 @@ Deno.test("promptSelect() handles down index overflow", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[3A", + "\x1b[J", "❯ safari\r\n", " chrome\r\n", " firefox\r\n", @@ -305,7 +313,8 @@ Deno.test("promptSelect() handles clear option", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K", + "\x1b[4A", + "\x1b[J", ]; let writeIndex = 0;