Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen committed Nov 18, 2024
1 parent d3b8bc5 commit 0f2af05
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
4 changes: 1 addition & 3 deletions cli/unstable_prompt_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ export function promptSelect(
const buffer = new Uint8Array(4);
loop:
while (true) {
let output = "";
for (const [index, value] of values.entries()) {
const start = index === selectedIndex ? INDICATOR : PADDING;
output += `${start} ${value}\r\n`;
Deno.stdout.writeSync(encoder.encode(`${start} ${value}\r\n`));
}
Deno.stdout.writeSync(encoder.encode(output));
const n = Deno.stdin.readSync(buffer);
if (n === null || n === 0) break;
const input = decoder.decode(buffer.slice(0, n));
Expand Down
59 changes: 44 additions & 15 deletions cli/unstable_prompt_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ function spyFn<T, K extends keyof T>(
fn: T[K],
) {
const originalFn = target[key];
target[key] = fn;
if (typeof fn !== "function") throw new Error(`fn is not a target method`);
target[key] = fn.bind(target);
return () => target[key] = originalFn;
}

Deno.test("promptSelect() handles enter", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
];

let writeIndex = 0;
Expand Down Expand Up @@ -67,11 +70,17 @@ Deno.test("promptSelect() handles enter", () => {
Deno.test("promptSelect() handles arrow down", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n❯ chrome\r\n firefox\r\n",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n chrome\r\n❯ firefox\r\n",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
];

let writeIndex = 0;
Expand Down Expand Up @@ -121,11 +130,17 @@ Deno.test("promptSelect() handles arrow down", () => {
Deno.test("promptSelect() handles arrow up", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n❯ chrome\r\n firefox\r\n",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
];

let writeIndex = 0;
Expand Down Expand Up @@ -175,9 +190,13 @@ Deno.test("promptSelect() handles arrow up", () => {
Deno.test("promptSelect() handles up index overflow", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n chrome\r\n❯ firefox\r\n",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
];

let writeIndex = 0;
Expand Down Expand Up @@ -226,13 +245,21 @@ Deno.test("promptSelect() handles up index overflow", () => {
Deno.test("promptSelect() handles down index overflow", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n❯ chrome\r\n firefox\r\n",
" safari\r\n",
"❯ chrome\r\n",
" firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
" safari\r\n chrome\r\n❯ firefox\r\n",
" safari\r\n",
" chrome\r\n",
"❯ firefox\r\n",
"\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K\x1b[1A\r\x1b[K",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ safari\r\n",
" chrome\r\n",
" firefox\r\n",
];

let writeIndex = 0;
Expand Down Expand Up @@ -283,7 +310,9 @@ Deno.test("promptSelect() handles down index overflow", () => {
Deno.test("promptSelect() handles clear option", () => {
const expectedOutput = [
"Please select a browser:\r\n",
"❯ safari\r\n chrome\r\n firefox\r\n",
"❯ 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",
];

Expand Down

0 comments on commit 0f2af05

Please sign in to comment.