Clack-style prompts for Rust: one question at a time on a rail, answers as typed values, and a form you can test without a terminal. It is a port of @clack/prompts with its look and keys.
┌ create-app
│
◇ Where should we create your project?
│ ./sparkling-solid
│
◆ Pick a project type
│ ● TypeScript
│ ○ JavaScript
└
clacky is inspired by clack 🍦: the prettiest terminal prompts I know, and I wanted them in Rust. cliclack ports the look but keeps the theme in a global, so you can't change colors per session or test your prompts. clacky keeps clack's look and makes everything a value: the prompts, the theme, the texts. It pulls in 4 crates where cliclack pulls in 41.
- Prompts:
text,password,confirm,number,date,path,select,multiselect,autocompleteand more, each a builder. - Derive forms:
#[derive(Choices, Form)]makes an enum aselectand a struct a form. - Live output: spinner, progress bar, tasks, notes and boxes.
- Theming by value: theme, symbols, messages and keys belong to the session, not a global.
- Prefill and
--yes:.given(arg)skips an answered question,Defaults::Taketakes the defaults. - Terminals without a keyboard:
TERM=dumband piped input have their own path. - Abort and timeout:
.timeout(duration)andAbortend a question and restore the terminal. - Scripted tests:
Prompt::simulateandfake::Scriptrun a form without a terminal. - Small: 4 dependencies, async included, about 410 KiB for a three question form.
cargo add clacky
cargo add clacky --features derive # #[derive(Choices, Form)]
cargo add clacky --dev --features fake # the scripted terminal, in testsThe async API (ask_until, wait, async_tasks) works on any executor. The minimum Rust version is 1.85.
use clacky::prelude::*;
fn main() -> Result<()> {
let ui = Session::detect()?;
ui.intro("my-app")?;
let name = ui.ask(text("Project name").placeholder("my-app").default("my-app"))?;
let kind = ui.ask(select("Kind").item("lib", "Library").item("bin", "Binary"))?;
let port: u16 = ui.ask(number("Port").min(1).max(65535).default(8080))?;
if ui.ask(confirm("Create it?"))? {
ui.outro(format!("{name}: {kind} on {port}"))
} else {
ui.cancel("Nothing created")
}
}The same, with a No that ends the form: ui.ask(confirm("Create it?").or_cancel("Nothing created"))?;. Text arguments of intro, outro, cancel, log and the spinner take anything that is Display, so format_args!(..) works too.
Session::detect reads the environment once. Esc and Ctrl-C come back as errors. The guide has a page per topic.
Run one with cargo run --example <name> (derive needs --features derive): create_app, select, search, files_and_dates, numbers, live, abort, defaults, i18n, accessible, derive, login and size.
See CONTRIBUTING.md.
MIT or Apache-2.0, at your option.