Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cli/_prompt_select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export function handlePromptSelect<V>(
}
}

let visibleLines = visibleLinesInit ?? Math.min(
availableHeight,
values.length,
let visibleLines = visibleLinesInit ?? Math.max(
1,
Math.min(availableHeight, values.length),
);

while (true) {
Expand Down Expand Up @@ -186,7 +186,7 @@ export function handlePromptSelect<V>(
} else {
availableHeight = Deno.consoleSize().rows - SAFE_PADDING;
}
visibleLines = Math.min(availableHeight, visibleLines);
visibleLines = Math.max(1, Math.min(availableHeight, visibleLines));

clearLength = 1 + // message
(hasUpArrow ? 1 : 0) +
Expand Down
57 changes: 57 additions & 0 deletions cli/unstable_prompt_select_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,60 @@ Deno.test("promptSelect() handles fitToRemainingHeight option", () => {
assertEquals(expectedOutput, actualOutput);
restore();
});

Deno.test("promptSelect() always shows at least one item when height is too small", () => {
stub(Deno.stdin, "setRaw");
stub(Deno.stdin, "isTerminal", () => true);
// rows(4) - SAFE_PADDING(4) = 0 available height, which previously collapsed
// visibleLines to 0 and rendered only a scroll indicator with no content.
stub(Deno, "consoleSize", () => ({ columns: 80, rows: 4 }));

const expectedOutput = [
"\x1b[?25l",
"Please select a browser:\r\n",
"❯ safari\r\n",
" ...\r\n",
"\x1b[?25h",
];

const actualOutput: string[] = [];

stub(
Deno.stdout,
"writeSync",
(data: Uint8Array) => {
const output = decoder.decode(data);
actualOutput.push(output);
return data.length;
},
);

let readIndex = 0;

const inputs = [
"\r",
];

stub(
Deno.stdin,
"readSync",
(data: Uint8Array) => {
const input = inputs[readIndex++];
const bytes = encoder.encode(input);
data.set(bytes);
return bytes.length;
},
);

const browser = promptSelect("Please select a browser:", [
"safari",
"chrome",
"firefox",
"brave",
"edge",
]);

assertEquals(browser, "safari");
assertEquals(expectedOutput, actualOutput);
restore();
});
Loading