Skip to main content

Module prompt

Module prompt 

Source
Expand description

Interactive prompts — clack/prompts-style.

§Hello, prompt

use cli_ui::prompt::{intro, outro, text, confirm, OnCancel};

fn main() {
    intro("Set up a new project");

    let name = text("What's your name?")
        .placeholder("Anya")
        .run()
        .or_cancel("Cancelled.");

    let public = confirm("Make it public?")
        .default(true)
        .run()
        .or_cancel("Cancelled.");

    outro(format!("Hello {name}! profile is {}.",
        if public { "public" } else { "private" }));
}

§The mental model

Every prompt is one of three things:

  1. A buildertext("…"), select("…"), etc. Chain .placeholder(), .default(), .validate(), .option() etc. Always finish with .run().
  2. A frame helperintro, outro, note, cancel, boxed::boxed. They print a single framed line/block and return immediately. Use them to surround your prompt flow.
  3. A long-running task displayspinner, progress, tasks, task_log. These run in the foreground while non-prompt work happens.

Everything renders to stderr so your prompts don’t pollute pipes.

§The four categories at a glance

§Question prompts

PromptPicks
textA line of text
secretA masked line (password, API key)
multilineSeveral lines
confirmYes / No
selectOne option from a list
multiselectMany options from a list
groupmultiselectMany options from grouped lists
autocompleteOne option, filtered by typing
select_keyOne option by single keypress
date::dateA yyyy-mm-dd date
path::pathA filesystem path with completion

§Framing

intro / outro / note / cancel / boxed::boxed / log / stream.

§Live work

spinner / progress / tasks / task_log.

§Composition

group::group runs many prompts sequentially and collects their answers.

§Validation

Validators are promoted to the prompt root so they import like any other constructor:

use cli_ui::prompt::{text, min_chars, has_upper, has_digit};

let pw = text("Password")
    .rule(min_chars(12).and(has_upper()).and(has_digit()))
    .run();

§Customisation

Theme everything in one call via settings::update_colors:

cli_ui::prompt::settings::update_colors(|c| {
    c.accent = anstyle::Style::new()
        .fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Magenta)))
        .bold();
});

§Cancellation

When the user hits Ctrl-C or Esc, .run() returns Err(PromptError::Interrupted). The OnCancel extension trait turns that into a clean exit:

use cli_ui::prompt::{text, OnCancel};
let name = text("Your name").run().or_cancel("Aborted — see you next time.");

§Extending

Implement core::Prompt to ship your own prompt — see the existing prompts in this module for templates. The runner handles raw mode, cleanup, validation transitions, and the answered redraw for you.

§One-line import

For quick scripts and examples, glob-import the prelude:

use cli_ui::prompt::prelude::*;

Re-exports§

pub use autocomplete::autocomplete;
pub use autocomplete::AutocompleteSelected;
pub use boxed::boxed;
pub use boxed::Align as BoxAlign;
pub use boxed::BoxOptions;
pub use confirm::confirm;
pub use date::date;
pub use date::Date;
pub use date::DatePrompt;
pub use group::group;
pub use groupmultiselect::groupmultiselect;
pub use groupmultiselect::GroupItem;
pub use groupmultiselect::GroupSelected;
pub use multiline::multiline;
pub use multiselect::multiselect;
pub use multiselect::MultiSelected;
pub use path::path;
pub use progress_bar::progress;
pub use progress_bar::Progress;
pub use progress_bar::Style as ProgressStyle;
pub use select::select;
pub use select::Selected;
pub use select_key::select_key;
pub use select_key::KeySelected;
pub use spinner::spinner;
pub use spinner::Spinner;
pub use task_log::task_log;
pub use task_log::TaskLog;
pub use tasks::tasks;
pub use tasks::Task;
pub use text::secret;
pub use text::text;
pub use validate::alpha_only;
pub use validate::alphanumeric;
pub use validate::email;
pub use validate::ends_with;
pub use validate::exact_chars;
pub use validate::float_between;
pub use validate::forbid_chars;
pub use validate::has_digit;
pub use validate::has_lower;
pub use validate::has_special;
pub use validate::has_upper;
pub use validate::int_between;
pub use validate::max_chars;
pub use validate::min_chars;
pub use validate::one_of;
pub use validate::only_chars;
pub use validate::required;
pub use validate::starts_with;
pub use validate::word_count;
pub use validate::words_between;
pub use validate::Validate;
pub use validate::Validator;
pub use settings::colors;
pub use settings::set_colors;
pub use settings::update_colors;
pub use settings::Colors;

Modules§

autocomplete
Autocomplete prompt — type to filter, arrows to navigate.
boxed
Bordered text block — ports @clack/prompts box().
confirm
Boolean confirmation prompt — built on the Prompt trait.
core
Core abstractions every prompt implements.
date
Date picker — built on the Prompt trait.
group
Sequentially-chained prompts — ports @clack/prompts group().
groupmultiselect
Grouped multi-select prompt — built on the Prompt trait.
log
Framed log messages — ports @clack/prompts log.{info,warn,error,success,step,message}.
multiline
Multi-line text input — built on the Prompt trait.
multiselect
Multi-choice select prompt — built on the Prompt trait.
path
Filesystem path picker — built on the Prompt trait.
prelude
Glob-importable shortcut for the most common entry points.
progress_bar
Progress bar — ports @clack/prompts progress().
select
Single-choice select — built on the Prompt trait.
select_key
Single-keypress option select — built on the Prompt trait.
settings
Global prompt settings — ports @clack/core’s settings.ts.
spinner
Animated spinner — ports @clack/prompts spinner().
stream
Streaming log output — ports @clack/prompts stream.{info,warn,error,success,step,message}.
task_log
Task with a collapsible running log — ports @clack/prompts taskLog().
tasks
Sequential task runner — ports @clack/prompts tasks().
text
Free-text and masked password prompts — built on the Prompt trait.
validate
Composable input validators — ports @clack/core’s runValidation with a library-of-rules on top, and ports utils/string.ts-style helpers.

Enums§

PromptError
Error returned by prompt .run() methods.

Traits§

OnCancel
Extension trait: gracefully end the process when the user cancels a prompt.

Functions§

cancel
Print ■ message — used after a prompt was cancelled / interrupted.
intro
Print ┌ message — opens a connected prompt session.
is_cancel
true when the error came from Ctrl+C / Ctrl+D / Escape.
note
Print a framed informational note between prompts.
outro
Print └ message — closes a prompt session.

Type Aliases§

Result
Result alias for prompt operations.