From 9e73cdadff9e12bddcb6298090b61341cae5501b Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 11 Dec 2024 22:26:25 +0100 Subject: [PATCH 1/2] initial commit --- cli/unstable_prompt_select_test.ts | 43 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/cli/unstable_prompt_select_test.ts b/cli/unstable_prompt_select_test.ts index d03482875af6..87472fedd5bf 100644 --- a/cli/unstable_prompt_select_test.ts +++ b/cli/unstable_prompt_select_test.ts @@ -6,8 +6,7 @@ import { restore, stub } from "@std/testing/mock"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); - -Deno.test("promptSelect() handles enter", () => { +Deno.test("promptSelect() handles CR", () => { stub(Deno.stdin, "setRaw"); const expectedOutput = [ @@ -17,15 +16,14 @@ Deno.test("promptSelect() handles enter", () => { " firefox\r\n", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -54,6 +52,7 @@ Deno.test("promptSelect() handles enter", () => { ]); assertEquals(browser, "safari"); + assertEquals(actualOutput, expectedOutput); restore(); }); @@ -77,15 +76,14 @@ Deno.test("promptSelect() handles arrow down", () => { "❯ firefox\r\n", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -116,6 +114,7 @@ Deno.test("promptSelect() handles arrow down", () => { ]); assertEquals(browser, "firefox"); + assertEquals(actualOutput, expectedOutput); restore(); }); @@ -139,15 +138,14 @@ Deno.test("promptSelect() handles arrow up", () => { " firefox\r\n", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -178,10 +176,11 @@ Deno.test("promptSelect() handles arrow up", () => { ]); assertEquals(browser, "safari"); + assertEquals(actualOutput, expectedOutput); restore(); }); -Deno.test("promptSelect() handles up index overflow", () => { +Deno.test("promptSelect() handles index underflow", () => { stub(Deno.stdin, "setRaw"); const expectedOutput = [ @@ -196,15 +195,14 @@ Deno.test("promptSelect() handles up index overflow", () => { "❯ firefox\r\n", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -234,10 +232,11 @@ Deno.test("promptSelect() handles up index overflow", () => { ]); assertEquals(browser, "firefox"); + assertEquals(actualOutput, expectedOutput); restore(); }); -Deno.test("promptSelect() handles down index overflow", () => { +Deno.test("promptSelect() handles index overflow", () => { stub(Deno.stdin, "setRaw"); const expectedOutput = [ @@ -262,15 +261,14 @@ Deno.test("promptSelect() handles down index overflow", () => { " firefox\r\n", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -302,6 +300,7 @@ Deno.test("promptSelect() handles down index overflow", () => { ]); assertEquals(browser, "safari"); + assertEquals(actualOutput, expectedOutput); restore(); }); @@ -317,15 +316,14 @@ Deno.test("promptSelect() handles clear option", () => { "\x1b[J", ]; - let writeIndex = 0; + const actualOutput: string[] = []; stub( Deno.stdout, "writeSync", (data: Uint8Array) => { const output = decoder.decode(data); - assertEquals(output, expectedOutput[writeIndex]); - writeIndex++; + actualOutput.push(output); return data.length; }, ); @@ -354,5 +352,6 @@ Deno.test("promptSelect() handles clear option", () => { ], { clear: true }); assertEquals(browser, "safari"); + assertEquals(actualOutput, expectedOutput); restore(); }); From adbebc4f2a2b9412d28e312b570e4301493cffc9 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 12 Dec 2024 01:32:33 +0100 Subject: [PATCH 2/2] updaet --- cli/unstable_prompt_select.ts | 6 +++--- cli/unstable_prompt_select_test.ts | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/cli/unstable_prompt_select.ts b/cli/unstable_prompt_select.ts index b1efeb69f78a..1c247ecdae20 100644 --- a/cli/unstable_prompt_select.ts +++ b/cli/unstable_prompt_select.ts @@ -41,12 +41,12 @@ export function promptSelect( const length = values.length; let selectedIndex = 0; - Deno.stdout.writeSync(encoder.encode(`${message}\r\n`)); Deno.stdin.setRaw(true); const buffer = new Uint8Array(4); loop: while (true) { + Deno.stdout.writeSync(encoder.encode(`${message}\r\n`)); for (const [index, value] of values.entries()) { const start = index === selectedIndex ? INDICATOR : PADDING; Deno.stdout.writeSync(encoder.encode(`${start} ${value}\r\n`)); @@ -66,11 +66,11 @@ export function promptSelect( case CR: break loop; } - Deno.stdout.writeSync(encoder.encode(`\x1b[${length}A`)); // move cursor after message + Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`)); Deno.stdout.writeSync(encoder.encode(CLR_ALL)); } if (clear) { - Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`)); // move cursor before message + Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`)); Deno.stdout.writeSync(encoder.encode(CLR_ALL)); } Deno.stdin.setRaw(false); diff --git a/cli/unstable_prompt_select_test.ts b/cli/unstable_prompt_select_test.ts index 87472fedd5bf..a227c1f33021 100644 --- a/cli/unstable_prompt_select_test.ts +++ b/cli/unstable_prompt_select_test.ts @@ -64,13 +64,15 @@ Deno.test("promptSelect() handles arrow down", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", @@ -126,13 +128,15 @@ Deno.test("promptSelect() handles arrow up", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", "❯ safari\r\n", " chrome\r\n", " firefox\r\n", @@ -188,8 +192,9 @@ Deno.test("promptSelect() handles index underflow", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", @@ -244,18 +249,21 @@ Deno.test("promptSelect() handles index overflow", () => { "❯ safari\r\n", " chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", "❯ chrome\r\n", " firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", " safari\r\n", " chrome\r\n", "❯ firefox\r\n", - "\x1b[3A", + "\x1b[4A", "\x1b[J", + "Please select a browser:\r\n", "❯ safari\r\n", " chrome\r\n", " firefox\r\n",