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:
- A builder —
text("…"),select("…"), etc. Chain.placeholder(),.default(),.validate(),.option()etc. Always finish with.run(). - A frame helper —
intro,outro,note,cancel,boxed::boxed. They print a single framed line/block and return immediately. Use them to surround your prompt flow. - A long-running task display —
spinner,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
| Prompt | Picks |
|---|---|
text | A line of text |
secret | A masked line (password, API key) |
multiline | Several lines |
confirm | Yes / No |
select | One option from a list |
multiselect | Many options from a list |
groupmultiselect | Many options from grouped lists |
autocomplete | One option, filtered by typing |
select_key | One option by single keypress |
date::date | A yyyy-mm-dd date |
path::path | A 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/promptsbox(). - confirm
- Boolean confirmation prompt — built on the
Prompttrait. - core
- Core abstractions every prompt implements.
- date
- Date picker — built on the
Prompttrait. - group
- Sequentially-chained prompts — ports
@clack/promptsgroup(). - groupmultiselect
- Grouped multi-select prompt — built on the
Prompttrait. - log
- Framed log messages — ports
@clack/promptslog.{info,warn,error,success,step,message}. - multiline
- Multi-line text input — built on the
Prompttrait. - multiselect
- Multi-choice select prompt — built on the
Prompttrait. - path
- Filesystem path picker — built on the
Prompttrait. - prelude
- Glob-importable shortcut for the most common entry points.
- progress_
bar - Progress bar — ports
@clack/promptsprogress(). - select
- Single-choice select — built on the
Prompttrait. - select_
key - Single-keypress option select — built on the
Prompttrait. - settings
- Global prompt settings — ports
@clack/core’ssettings.ts. - spinner
- Animated spinner — ports
@clack/promptsspinner(). - stream
- Streaming log output — ports
@clack/promptsstream.{info,warn,error,success,step,message}. - task_
log - Task with a collapsible running log — ports
@clack/promptstaskLog(). - tasks
- Sequential task runner — ports
@clack/promptstasks(). - text
- Free-text and masked password prompts — built on the
Prompttrait. - validate
- Composable input validators — ports
@clack/core’srunValidationwith a library-of-rules on top, and portsutils/string.ts-style helpers.
Enums§
- Prompt
Error - 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 truewhen 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.