-
Notifications
You must be signed in to change notification settings - Fork 656
feat(cli/unstable): add promptSelect()
#6190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e3911c8
initial commit
timreichen 45129be
add copyright header
timreichen 6e307d5
add mod export
timreichen 41da460
add jsdoc
timreichen 414f1a3
update
timreichen a974e1e
update
timreichen 428e799
cleanup
timreichen c295684
remove obsolete class
timreichen 0589c81
move padding declaration
timreichen 9fd7c4a
cleanup
timreichen a738c45
fix
timreichen 5d97c32
add tests
timreichen d3b8bc5
cleanup
timreichen 0f2af05
update
timreichen 0bbb33f
Merge branch 'main' into cli-add-prompt-select
timreichen d3a32d7
update
timreichen 82a0ba2
Merge branch 'main' into cli-add-prompt-select
timreichen ef0a93d
Merge branch 'main' into cli-add-prompt-select
timreichen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** Options for {@linkcode promptSelect}. */ | ||
export interface PromptSelectOptions { | ||
/** 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 CLR = "\r\u001b[K"; // Clear the current line | ||
|
||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
/** | ||
* 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 string that was entered or `null` if stdin is not a TTY. | ||
* | ||
* @example Usage | ||
* ```ts ignore | ||
* import { promptSelect } from "@std/cli/prompt-select"; | ||
* | ||
* const browser = promptSelect("Please select a browser:", ["safari", "chrome", "firefox"], { clear: true }); | ||
* ``` | ||
*/ | ||
export function promptSelect( | ||
message: string, | ||
values: string[], | ||
{ clear }: PromptSelectOptions = {}, | ||
): string | null { | ||
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) { | ||
for (const [index, value] of values.entries()) { | ||
const start = index === selectedIndex ? INDICATOR : PADDING; | ||
Deno.stdout.writeSync(encoder.encode(`${start} ${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: | ||
return Deno.exit(0); | ||
case ARROW_UP: | ||
selectedIndex = (selectedIndex - 1 + length) % length; | ||
break; | ||
case ARROW_DOWN: | ||
selectedIndex = (selectedIndex + 1) % length; | ||
break; | ||
case CR: | ||
break loop; | ||
} | ||
Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length))); | ||
} | ||
if (clear) { | ||
Deno.stdout.writeSync(encoder.encode(`\x1b[1A${CLR}`.repeat(length + 1))); // clear values and message | ||
} | ||
kt3k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Deno.stdin.setRaw(false); | ||
return values[selectedIndex] ?? null; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.