Interactive command line prompt to gather boolean input from users
npx @tessl/cli install tessl/npm-inquirer--confirm@4.0.0Inquirer Confirm provides a simple interactive command line prompt to gather boolean input from users. It's part of the Inquirer.js ecosystem and offers a clean, themeable confirmation prompt that accepts yes/no responses with customizable default values, message transformers, and styling options.
npm install @inquirer/prompts (recommended) or npm install @inquirer/confirmimport confirm from "@inquirer/confirm";Alternative import from prompts bundle:
import { confirm } from "@inquirer/prompts";For CommonJS:
const confirm = require("@inquirer/confirm");import confirm from "@inquirer/confirm";
// Simple confirmation
const shouldContinue = await confirm({ message: "Continue?" });
// With default value
const deleteFile = await confirm({
message: "Delete this file?",
default: false
});
// With custom transformer
const proceed = await confirm({
message: "Proceed with deployment?",
transformer: (answer) => answer ? "✅ Yes" : "❌ No"
});Creates an interactive confirmation prompt that captures boolean user input with customizable styling and behavior.
/**
* Creates a confirmation prompt that captures boolean user input
* @param config - Configuration object for the confirm prompt
* @param context - Optional context configuration for runtime behavior
* @returns Promise resolving to boolean user response
*/
function confirm(config: ConfirmConfig, context?: ContextOptions): Promise<boolean>;
interface ConfirmConfig {
/** The question to ask the user */
message: string;
/** Default answer (true or false). Any value other than false is treated as true */
default?: boolean;
/** Function to transform the boolean result into a custom display string */
transformer?: (value: boolean) => string;
/** Theme customization object for styling the prompt */
theme?: PartialDeep<Theme>;
}Usage Examples:
import confirm from "@inquirer/confirm";
// Basic confirmation with default true
const answer1 = await confirm({ message: "Do you want to proceed?" });
// Displays: ? Do you want to proceed? (Y/n)
// Confirmation with default false
const answer2 = await confirm({
message: "Delete all files?",
default: false
});
// Displays: ? Delete all files? (y/N)
// With custom response transformation
const answer3 = await confirm({
message: "Save changes?",
transformer: (value) => value ? "Saved!" : "Discarded"
});
// On completion shows: ✔ Save changes? Saved! (or Discarded)All inquirer prompts, including confirm, accept an optional second parameter for context configuration:
function confirm(config: ConfirmConfig, context?: ContextOptions): Promise<boolean>;
interface ContextOptions {
/** The stdin stream (defaults to process.stdin) */
input?: NodeJS.ReadableStream;
/** The stdout stream (defaults to process.stdout) */
output?: NodeJS.WritableStream;
/** If true, clear the screen after the prompt is answered */
clearPromptOnDone?: boolean;
/** An AbortSignal to cancel prompts asynchronously */
signal?: AbortSignal;
}Context Usage Example:
import confirm from "@inquirer/confirm";
const answer = await confirm(
{ message: "Deploy to production?" },
{
clearPromptOnDone: true,
signal: AbortSignal.timeout(10000) // Timeout after 10 seconds
}
);The prompt accepts various input formats:
y or yes (case insensitive, matches /^(y|yes)/i)n or no (case insensitive, matches /^(n|no)/i)default value (true if not specified)default valueThe prompt supports comprehensive theming through the theme configuration option.
interface Theme {
/** Prefix shown before the message, can be string or status-specific object */
prefix: string | Prettify<Omit<Record<Status, string>, 'loading'>>;
/** Spinner configuration for loading states */
spinner: {
/** The time interval between frames, in milliseconds */
interval: number;
/** A list of frames to show for the spinner */
frames: string[];
};
/** Styling functions for different text elements */
style: {
/** Style function for the final answer display */
answer: (text: string) => string;
/** Style function for the message text */
message: (text: string, status: Status) => string;
/** Style function for error messages */
error: (text: string) => string;
/** Style function for the default value indicator */
defaultAnswer: (text: string) => string;
/** Style function for help text */
help: (text: string) => string;
/** Style function for highlighted text */
highlight: (text: string) => string;
/** Style function for keyboard keys referred to in help texts */
key: (text: string) => string;
};
}
type Status = "idle" | "done" | "loading";
type PartialDeep<T> = T extends object
? {
[P in keyof T]?: PartialDeep<T[P]>;
}
: T;
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};Theme Usage Example:
const answer = await confirm({
message: "Deploy to production?",
theme: {
prefix: "🚀",
style: {
answer: (text) => `>>> ${text} <<<`,
message: (text, status) =>
status === "done" ? `✅ ${text}` : `❓ ${text}`,
defaultAnswer: (text) => `[${text}]`
}
}
});true when not specified (config.default !== false means undefined defaults to true)(Y/n) when default is not false, (y/N) when default is false"yes" for true, "no" for falseThe function returns a Promise<boolean>:
true for affirmative responses or when default is true and no specific input is providedfalse for negative responses or when default is false and no specific input is provided