Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@clack/prompts

bombshell-dev4.7mMIT0.10.1TypeScript support: included

Effortlessly build beautiful command-line apps 🪄 Try the demo

ask, clack, cli, command-line, command, input, interact, interface, menu, prompt, prompts, stdin, ui

readme

@clack/prompts

Effortlessly build beautiful command-line apps 🪄 Try the demo

clack-prompt


@clack/prompts is an opinionated, pre-styled wrapper around @clack/core.

  • 🤏 80% smaller than other options
  • 💎 Beautiful, minimal UI
  • ✅ Simple API
  • 🧱 Comes with text, confirm, select, multiselect, and spinner components

Basics

Setup

The intro and outro functions will print a message to begin or end a prompt session, respectively.

import { intro, outro } from '@clack/prompts';

intro(`create-my-app`);
// Do stuff
outro(`You're all set!`);

Cancellation

The isCancel function is a guard that detects when a user cancels a question with CTRL + C. You should handle this situation for each prompt, optionally providing a nice cancellation message with the cancel utility.

import { isCancel, cancel, text } from '@clack/prompts';

const value = await text({
  message: 'What is the meaning of life?',
});

if (isCancel(value)) {
  cancel('Operation cancelled.');
  process.exit(0);
}

Components

Text

The text component accepts a single line of text.

import { text } from '@clack/prompts';

const meaning = await text({
  message: 'What is the meaning of life?',
  placeholder: 'Not sure',
  initialValue: '42',
  validate(value) {
    if (value.length === 0) return `Value is required!`;
  },
});

Confirm

The confirm component accepts a yes or no answer. The result is a boolean value of true or false.

import { confirm } from '@clack/prompts';

const shouldContinue = await confirm({
  message: 'Do you want to continue?',
});

Select

The select component allows a user to choose one value from a list of options. The result is the value prop of a given option.

import { select } from '@clack/prompts';

const projectType = await select({
  message: 'Pick a project type.',
  options: [
    { value: 'ts', label: 'TypeScript' },
    { value: 'js', label: 'JavaScript' },
    { value: 'coffee', label: 'CoffeeScript', hint: 'oh no' },
  ],
});

Multi-Select

The multiselect component allows a user to choose many values from a list of options. The result is an array with all selected value props.

import { multiselect } from '@clack/prompts';

const additionalTools = await multiselect({
  message: 'Select additional tools.',
  options: [
    { value: 'eslint', label: 'ESLint', hint: 'recommended' },
    { value: 'prettier', label: 'Prettier' },
    { value: 'gh-action', label: 'GitHub Action' },
  ],
  required: false,
});

Spinner

The spinner component surfaces a pending action, such as a long-running download or dependency installation.

import { spinner } from '@clack/prompts';

const s = spinner();
s.start('Installing via npm');
// Do installation here
s.stop('Installed via npm');

Utilities

Grouping

Grouping prompts together is a great way to keep your code organized. This accepts a JSON object with a name that can be used to reference the group later. The second argument is an optional but has a onCancel callback that will be called if the user cancels one of the prompts in the group.

import * as p from '@clack/prompts';

const group = await p.group(
  {
    name: () => p.text({ message: 'What is your name?' }),
    age: () => p.text({ message: 'What is your age?' }),
    color: ({ results }) =>
      p.multiselect({
        message: `What is your favorite color ${results.name}?`,
        options: [
          { value: 'red', label: 'Red' },
          { value: 'green', label: 'Green' },
          { value: 'blue', label: 'Blue' },
        ],
      }),
  },
  {
    // On Cancel callback that wraps the group
    // So if the user cancels one of the prompts in the group this function will be called
    onCancel: ({ results }) => {
      p.cancel('Operation cancelled.');
      process.exit(0);
    },
  }
);

console.log(group.name, group.age, group.color);

Tasks

Execute multiple tasks in spinners.

await p.tasks([
  {
    title: 'Installing via npm',
    task: async (message) => {
      // Do installation here
      return 'Installed via npm';
    },
  },
]);

Logs

import { log } from '@clack/prompts';

log.info('Info!');
log.success('Success!');
log.step('Step!');
log.warn('Warn!');
log.error('Error!');
log.message('Hello, World', { symbol: color.cyan('~') });

Stream

When interacting with dynamic LLMs or other streaming message providers, use the stream APIs to log messages from an iterable, even an async one.

import { stream } from '@clack/prompts';

stream.info((function *() { yield 'Info!'; })());
stream.success((function *() { yield 'Success!'; })());
stream.step((function *() { yield 'Step!'; })());
stream.warn((function *() { yield 'Warn!'; })());
stream.error((function *() { yield 'Error!'; })());
stream.message((function *() { yield 'Hello'; yield ", World" })(), { symbol: color.cyan('~') });

clack-log-prompts

changelog

@clack/prompts

0.10.1

Patch Changes

  • 11a5dc1: Fixes multiselect only shows hints on the first item in the options list. Now correctly shows hints for all selected options with hint property.
  • 30aa7ed: Adds a new selectableGroups boolean to the group multi-select prompt. Using selectableGroups: false will disable the ability to select a top-level group, but still allow every child to be selected individually.
  • Updated dependencies [30aa7ed]
  • Updated dependencies [5dfce8a]
  • Updated dependencies [f574297]

0.10.0

Minor Changes

  • 613179d: Adds a new indicator option to spinner, which supports the original "dots" loading animation or a new "timer" loading animation.

    import * as p from "@clack/prompts";
    
    const spin = p.spinner({ indicator: "timer" });
    spin.start("Loading");
    await sleep(3000);
    spin.stop("Loaded");
  • a38b2bc: Adds stream API which provides the same methods as log, but for iterable (even async) message streams. This is particularly useful for AI responses which are dynamically generated by LLMs.

    import * as p from "@clack/prompts";
    
    await p.stream.step(
      (async function* () {
        yield* generateLLMResponse(question);
      })()
    );

0.9.1

Patch Changes

  • 8093f3c: Adds Error support to the validate function
  • 98925e3: Exports the Option type and improves JSDocannotations
  • 1904e57: Replace custom utility for stripping ANSI control sequences with Node's built-in stripVTControlCharacters utility.
  • Updated dependencies [8093f3c]
  • Updated dependencies [e5ba09a]
  • Updated dependencies [8cba8e3]

0.9.0

Minor Changes

  • a83d2f8: Adds a new updateSettings() function to support new global keybindings.

    updateSettings() accepts an aliases object that maps custom keys to an action (up | down | left | right | space | enter | cancel).

    import { updateSettings } from "@clack/prompts";
    
    // Support custom keybindings
    updateSettings({
      aliases: {
        w: "up",
        a: "left",
        s: "down",
        d: "right",
      },
    });

[!WARNING] In order to enforce consistent, user-friendly defaults across the ecosystem, updateSettings does not support disabling Clack's default keybindings.

  • 801246b: Adds a new signal option to support programmatic prompt cancellation with an abort controller.

    One example use case is automatically cancelling a prompt after a timeout.

    const shouldContinue = await confirm({
      message: "This message will self destruct in 5 seconds",
      signal: AbortSignal.timeout(5000),
    });

    Another use case is racing a long running task with a manual prompt.

    const abortController = new AbortController();
    
    const projectType = await Promise.race([
      detectProjectType({
        signal: abortController.signal,
      }),
      select({
        message: "Pick a project type.",
        options: [
          { value: "ts", label: "TypeScript" },
          { value: "js", label: "JavaScript" },
          { value: "coffee", label: "CoffeeScript", hint: "oh no" },
        ],
        signal: abortController.signal,
      }),
    ]);
    
    abortController.abort();
  • a83d2f8: Updates default keybindings to support Vim motion shortcuts and map the escape key to cancel (ctrl+c).

    | alias | action | | ----- | ------ | | k | up | | l | right | | j | down | | h | left | | esc | cancel |

Patch Changes

  • f9f139d: Adapts spinner output for static CI environments
  • Updated dependencies [a83d2f8]
  • Updated dependencies [801246b]
  • Updated dependencies [a83d2f8]
  • Updated dependencies [51e12bc]

0.8.2

Patch Changes

  • Updated dependencies [4845f4f]
  • Updated dependencies [d7b2fb9]

0.8.1

Patch Changes

  • 360afeb: feat: adaptative max items

0.8.0

Minor Changes

  • 9acccde: Add tasks function for executing tasks in spinners

Patch Changes

  • b5c6b9b: Feat multiselect maxItems option
  • 50ed94a: fix: clear spinner hooks on spinner.stop
  • Updated dependencies [a04e418]
  • Updated dependencies [4f6fcf5]

0.7.0

Minor Changes

  • b27a701: add maxItems option to select prompt
  • 89371be: added a new method called spinner.message(msg: string)

Patch Changes

  • 52183c4: Fix spinner conflict with terminal on error between spinner.start() and spinner.stop()
  • ab51d29: Fixes cases where the note title length was miscalculated due to ansi characters
  • Updated dependencies [cd79076]

0.6.3

Patch Changes

  • c96eda5: Enable hard line-wrapping behavior for long words without spaces
  • Updated dependencies [c96eda5]

0.6.2

Patch Changes

  • 58a1df1: Fix line duplication bug by automatically wrapping prompts to process.stdout.columns
  • Updated dependencies [58a1df1]

0.6.1

Patch Changes

  • ca08fb6: Support complex value types for select, multiselect and groupMultiselect.

0.6.0

Minor Changes

  • 8a4a12f: add groupMultiselect prompt
  • 165a1b3: Add log APIs. Supports log.info, log.success, log.warn, and log.error. For low-level control, log.message is also exposed.

Patch Changes

  • Updated dependencies [8a4a12f]
  • Updated dependencies [8a4a12f]

0.5.1

Patch Changes

  • cc11917: Update default password mask
  • Updated dependencies [ec812b6]

0.5.0

Minor Changes

  • d74dd05: Adds a selectKey prompt type
  • 54c1bc3: Breaking Change multiselect has renamed initialValue to initialValues

Patch Changes

  • Updated dependencies [d74dd05]
  • Updated dependencies [54c1bc3]

0.4.5

Patch Changes

  • 1251132: Multiselect: return Value[] instead of Option[].
  • 8994382: Add a password prompt to @clack/prompts
  • Updated dependencies [1251132]
  • Updated dependencies [8994382]

0.4.4

Patch Changes

  • d96071c: Don't mutate initialValue in multiselect, fix parameter type for validate().

    Credits to @banjo for the bug report and initial PR!

  • Updated dependencies [d96071c]

0.4.3

Patch Changes

  • 83d890e: Fix text cancel display bug

0.4.2

Patch Changes

  • Update README

0.4.1

Patch Changes

  • 7fb5375: Adds a new defaultValue option to the text prompt, removes automatic usage of the placeholder value.
  • Updated dependencies [7fb5375]

0.4.0

Minor Changes

  • 61b88b6: Add group construct to group many prompts together

Patch Changes

  • de1314e: Support required option for multi-select
  • Updated dependencies [de1314e]

0.3.0

Minor Changes

  • 493c592: Improve types for select/multiselect prompts. Numbers and booleans are now supported as the value option.
  • 15558e3: Improved Windows/non-unicode support

Patch Changes

  • ca77da1: Fix multiselect initial value logic
  • Updated dependencies [ca77da1]
  • Updated dependencies [8aed606]

0.2.2

Patch Changes

  • 94b24d9: Fix CJS ansi-regex interop

0.2.1

Patch Changes

  • a99c458: Support initialValue option for text prompt
  • Updated dependencies [a99c458]

0.2.0

Minor Changes

  • Improved type safety
  • b1341d6: Updated styles, new note component

Patch Changes

  • Updated dependencies [7dcad8f]
  • Updated dependencies [2242f13]
  • Updated dependencies [b1341d6]

0.1.1

Patch Changes

  • fa09bf5: Use circle for radio, square for checkbox
  • Updated dependencies [4be7dbf]
  • Updated dependencies [b480679]

0.1.0

Minor Changes

  • 7015ec9: Create new prompt: multi-select

Patch Changes

0.0.10

Patch Changes

  • e0b49e5: Update spinner so it actually spins

0.0.9

Patch Changes

  • Update README

0.0.8

Patch Changes

0.0.7

Patch Changes

  • Update README

0.0.6

Patch Changes

  • d20ef2a: Update keywords, URLs
  • Updated dependencies [441d5b7]
  • Updated dependencies [d20ef2a]
  • Updated dependencies [fe13c2f]

0.0.5

Patch Changes

  • Update README

0.0.4

Patch Changes

  • 80404ab: Update README

0.0.3

Patch Changes

0.0.2

Patch Changes

0.0.1

Patch Changes