-
Notifications
You must be signed in to change notification settings - Fork 666
feat(cli/unstable): add promptConfirm #6985
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
Open
crowlKats
wants to merge
2
commits into
main
Choose a base branch
from
promptconfirm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+770
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,174 @@ | ||
| // Copyright 2018-2026 the Deno authors. MIT license. | ||
|
|
||
| const encoder = new TextEncoder(); | ||
| const decoder = new TextDecoder(); | ||
|
|
||
| const LF = "\n".charCodeAt(0); | ||
| const CR = "\r".charCodeAt(0); | ||
| const BS = "\b".charCodeAt(0); | ||
| const DEL = 0x7f; | ||
|
|
||
| /** | ||
| * Represents a possible value for the confirm prompt. | ||
| * | ||
| * @typeParam T The type of the value returned when this option is selected. | ||
| */ | ||
| export interface PromptConfirmValue<T> { | ||
| /** The key the user types to select this option (e.g., "y", "n", "m"). */ | ||
| key: string; | ||
| /** The display label shown in the prompt (e.g., "yes", "no", "maybe"). */ | ||
| label?: string; | ||
| /** The value returned when this option is selected. */ | ||
| value: T; | ||
| } | ||
|
|
||
| /** Options for {@linkcode promptConfirm}. */ | ||
| export interface PromptConfirmOptions { | ||
| /** | ||
| * The key of the default value when the user presses Enter without typing. | ||
| * | ||
| * @default {"n"} | ||
| */ | ||
| default?: string; | ||
|
|
||
| /** Clear the current line after the user's input. */ | ||
| clear?: boolean; | ||
| } | ||
|
|
||
| /** Default values for the confirm prompt. */ | ||
| export const YES_NO_VALUES: PromptConfirmValue<boolean>[] = [ | ||
| { key: "y", value: true }, | ||
| { key: "n", value: false }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Shows the given message and waits for the user's input. Returns the value | ||
| * associated with the user's selection. | ||
| * | ||
| * The prompt shows the available options with the default option's key capitalized. | ||
| * For example: `Continue? [y/N]` where `N` is capitalized to indicate the default. | ||
| * If a label is provided, it's shown in parentheses: `[y (yes)/N (no)]`. | ||
| * | ||
| * The user can type either the key or the full label to select an option. | ||
| * | ||
| * @typeParam T The type of values that can be returned. | ||
| * @param message The prompt message to show to the user. | ||
| * @param values The possible values for the prompt. | ||
| * @param options The options for the prompt. | ||
| * @returns The value of the selected option, or `null` if stdin is not a TTY. | ||
| * | ||
| * @example Basic usage with YES_NO_VALUES | ||
| * ```ts ignore | ||
| * import { promptConfirm, YES_NO_VALUES } from "@std/cli/unstable-confirm"; | ||
| * | ||
| * const shouldProceed = promptConfirm("Continue?", YES_NO_VALUES); | ||
| * if (shouldProceed) { | ||
| * console.log("Continuing..."); | ||
| * } | ||
| * // Displays: Continue? [y/N] | ||
| * ``` | ||
| * | ||
| * @example With default set to yes | ||
| * ```ts ignore | ||
| * import { promptConfirm, YES_NO_VALUES } from "@std/cli/unstable-confirm"; | ||
| * | ||
| * const shouldProceed = promptConfirm("Continue?", YES_NO_VALUES, { default: "y" }); | ||
| * // Displays: Continue? [Y/n] | ||
| * ``` | ||
| * | ||
| * @example Custom values with labels | ||
| * ```ts ignore | ||
| * import { promptConfirm } from "@std/cli/unstable-confirm"; | ||
| * | ||
| * const result = promptConfirm("Save changes?", [ | ||
| * { key: "y", label: "yes", value: "save" }, | ||
| * { key: "n", label: "no", value: "discard" }, | ||
| * { key: "c", label: "cancel", value: "cancel" }, | ||
| * ], { default: "c" }); | ||
| * // Displays: Save changes? [y (yes)/n (no)/C (cancel)] | ||
| * ``` | ||
| * | ||
| * @example With clear option | ||
| * ```ts ignore | ||
| * import { promptConfirm, YES_NO_VALUES } from "@std/cli/unstable-confirm"; | ||
| * | ||
| * const shouldProceed = promptConfirm("Delete file?", YES_NO_VALUES, { clear: true }); | ||
| * ``` | ||
| */ | ||
| export function promptConfirm<T>( | ||
| message: string, | ||
| values: PromptConfirmValue<T>[], | ||
| options: PromptConfirmOptions = {}, | ||
| ): T | null { | ||
| const input = Deno.stdin; | ||
| const output = Deno.stdout; | ||
|
|
||
| if (!input.isTerminal()) { | ||
| return null; | ||
| } | ||
|
|
||
| const defaultKey = options.default ?? "n"; | ||
| const { clear } = options; | ||
|
|
||
| const defaultOption = values.find((v) => | ||
| v.key.toLowerCase() === defaultKey.toLowerCase() | ||
| ); | ||
|
|
||
| const optionsDisplay = values.map((v) => { | ||
| const isDefault = v.key.toLowerCase() === defaultKey.toLowerCase(); | ||
| const key = isDefault ? v.key.toUpperCase() : v.key.toLowerCase(); | ||
| return `${key}${v.label ? ` (${v.label})` : ""}`; | ||
| }).join("/"); | ||
|
|
||
| const prompt = `${message} [${optionsDisplay}] `; | ||
| output.writeSync(encoder.encode(prompt)); | ||
|
|
||
| input.setRaw(true); | ||
| try { | ||
| const answer = readLineFromStdinSync(); | ||
| const trimmedAnswer = answer.trim().toLowerCase(); | ||
|
|
||
| if (trimmedAnswer === "") { | ||
| return defaultOption?.value ?? values[0]!.value; | ||
| } | ||
|
|
||
| const selectedOption = values.find((v) => | ||
| v.key.toLowerCase() === trimmedAnswer || | ||
| v.label?.toLowerCase() === trimmedAnswer | ||
| ); | ||
|
|
||
| if (selectedOption) { | ||
| return selectedOption.value; | ||
| } | ||
|
|
||
| return defaultOption?.value ?? values[0]!.value; | ||
| } finally { | ||
| if (clear) { | ||
| output.writeSync(encoder.encode("\r\x1b[K")); | ||
| } else { | ||
| output.writeSync(encoder.encode("\n")); | ||
| } | ||
| input.setRaw(false); | ||
| } | ||
| } | ||
|
|
||
| function readLineFromStdinSync(): string { | ||
| const c = new Uint8Array(1); | ||
| const buf: number[] = []; | ||
|
|
||
| while (true) { | ||
| const n = Deno.stdin.readSync(c); | ||
| if (n === null || n === 0) { | ||
| break; | ||
| } | ||
| if (c[0] === CR || c[0] === LF) { | ||
| break; | ||
| } | ||
| if (c[0] === BS || c[0] === DEL) { | ||
| buf.pop(); | ||
| } else { | ||
| buf.push(c[0]!); | ||
| } | ||
| } | ||
| return decoder.decode(new Uint8Array(buf)); | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is the default for
values, then maybevaluesshould be a part of the option bag? (and it also can be omitted?)