Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli/unstable): add promptMultipleSelect() #6223

Merged
merged 14 commits into from
Dec 12, 2024
1 change: 1 addition & 0 deletions cli/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"./parse-args": "./parse_args.ts",
"./prompt-secret": "./prompt_secret.ts",
"./unstable-prompt-select": "./unstable_prompt_select.ts",
"./unstable-prompt-multiple-select": "./unstable_prompt_multiple_select.ts",
"./unstable-spinner": "./unstable_spinner.ts",
"./unicode-width": "./unicode_width.ts"
}
Expand Down
96 changes: 96 additions & 0 deletions cli/unstable_prompt_multiple_select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/** Options for {@linkcode promptMultipleSelect}. */
export interface PromptMultipleSelectOptions {
/** Clear the lines after the user's input. */
clear?: boolean;
}

const ETX = "\x03";
const ARROW_UP = "\u001B[A";
const ARROW_DOWN = "\u001B[B";
const CR = "\r";
const INDICATOR = "❯";
const PADDING = " ".repeat(INDICATOR.length);

const CHECKED = "◉";
const UNCHECKED = "◯";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const CLEAR_ALL = encoder.encode("\x1b[J"); // Clear all lines after cursor
const HIDE_CURSOR = encoder.encode("\x1b[?25l");
const SHOW_CURSOR = encoder.encode("\x1b[?25h");

/**
* Shows the given message and waits for the user's input. Returns the user's selected value as string.
*
* @param message The prompt message to show to the user.
* @param values The values for the prompt.
* @param options The options for the prompt.
* @returns The selected values as an array of strings.
*
* @example Usage
* ```ts ignore
* import { promptMultipleSelect } from "@std/cli/prompt-select";
*
* const browsers = promptMultipleSelect("Please select browsers:", ["safari", "chrome", "firefox"], { clear: true });
* ```
*/
export function promptMultipleSelect(
message: string,
values: string[],
{ clear }: PromptMultipleSelectOptions = {},
): string[] {
const length = values.length;
let selectedIndex = 0;
const selectedIndexes = new Set<number>();

Deno.stdin.setRaw(true);
Deno.stdout.writeSync(HIDE_CURSOR);
const buffer = new Uint8Array(4);
loop:
while (true) {
Deno.stdout.writeSync(encoder.encode(`${message}\r\n`));
for (const [index, value] of values.entries()) {
const selected = index === selectedIndex;
const start = selected ? INDICATOR : PADDING;
const checked = selectedIndexes.has(index);
const state = checked ? CHECKED : UNCHECKED;
Deno.stdout.writeSync(encoder.encode(`${start} ${state} ${value}\r\n`));
}
const n = Deno.stdin.readSync(buffer);
if (n === null || n === 0) break;
const input = decoder.decode(buffer.slice(0, n));

switch (input) {
case ETX:
Deno.stdout.writeSync(SHOW_CURSOR);
return Deno.exit(0);

Check warning on line 70 in cli/unstable_prompt_multiple_select.ts

View check run for this annotation

Codecov / codecov/patch

cli/unstable_prompt_multiple_select.ts#L69-L70

Added lines #L69 - L70 were not covered by tests
case ARROW_UP:
selectedIndex = (selectedIndex - 1 + length) % length;
break;
case ARROW_DOWN:
selectedIndex = (selectedIndex + 1) % length;
break;
case CR:
break loop;
case " ":
if (selectedIndexes.has(selectedIndex)) {
selectedIndexes.delete(selectedIndex);
} else {

Check warning on line 82 in cli/unstable_prompt_multiple_select.ts

View check run for this annotation

Codecov / codecov/patch

cli/unstable_prompt_multiple_select.ts#L81-L82

Added lines #L81 - L82 were not covered by tests
selectedIndexes.add(selectedIndex);
}
break;
}
Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`));
}
if (clear) {
Deno.stdout.writeSync(encoder.encode(`\x1b[${length + 1}A`));
Deno.stdout.writeSync(CLEAR_ALL);
}
Deno.stdout.writeSync(SHOW_CURSOR);
Deno.stdin.setRaw(false);
return [...selectedIndexes].map((it) => values[it] as string);
}
Loading
Loading