#prompt

may-clack

stylish, interactive command line prompts

20 releases

0.6.1 Sep 15, 2024
0.5.8 Apr 6, 2024
0.5.7 Jan 29, 2024
0.5.4 Nov 29, 2023

#422 in Command-line interface

32 downloads per month

MPL-2.0 license

86KB
2K SLoC

may clack

this is a rust port of the npm package @clack/prompts.

wip

this project is still a work in progress

the api will likely change still


lib.rs:

This is a rust port of https://www.npmjs.com/package/@clack/prompts

Setup

You can setup the start and end of a prompt session with the macros intro! and outro!, respectively

use may_clack::{intro, outro};

intro!("intro");
// do stuff
outro!("outro");

Cancel

When the user cancels a question, you can use the cancel! utility to provide a cancellation message.

When cancelled the will return a error::ClackError::Cancelled, or you can check if it was cancelled using the traits::IsCancel trait extension.

All input types that can return a Cancelled Err will also have the option to add a .cancel closure

use may_clack::{cancel, error::ClackError, input};

let text = input("todo").interact();
if let Err(ClackError::Cancelled) = text {
    cancel!("operation cancelled");
}
use may_clack::{cancel, input, traits::IsCancel};
let text = input("todo").interact();
if text.is_cancel() {
    cancel!("operation cancelled");
}

Info

If you want to write a message in a prompting session you can use the info!, warn! or err! utility.

use may_clack::{err, info, intro, outro, warn};

intro!("intro");
// do stuff
info!("info");
// do stuff
warn!("warn");
// do stuff
err!("err");
// do stuff
outro!("outro");

General

There are 6 components: input, confirm, select, multi_select, multi_input

Each of the input types returns a struct, that allows you to setup the prompt.
since every prompt needs a message the initial

To actually prompt the user after setting up you have to call .interact()

use may_clack::confirm;

let answer = confirm("Yes or No?").interact()?;

Components

Input

The input::Input component accepts a single line of text.

use may_clack::input;

let answer = input("what is the meaning of life?")
    .initial_value("42")
    .interact()?;
println!("{:?}", answer);

Confirm

The confirm::Confirm component accepts a yes or no answer.

use may_clack::confirm;

let answer = confirm("do you want to continue?").interact()?;
println!("answer {:?}", answer);

Select

The select::Select component allows the user to choose one value from a list of options.

use may_clack::select;

#[derive(Debug, Clone)]
enum Fruit {
    Mango,
    Peach,
    PassionFruit,
}

let fruit = select("pick a fruit")
    .option_hint(Fruit::Mango, "Mango", "The best one")
    .option(Fruit::Peach, "Peach")
    .option(Fruit::PassionFruit, "Passion fruit")
    .interact()?;
println!("fruit {:?}", fruit);

MultiSelect

The multi_select::MultiSelect component allows the user to choose multiple values from a list of options.

use may_clack::multi_select;

let toppings = multi_select("Choose your toppings")
    .option("fruits", "Dried fruits")
    .option("chocolate", "Chocolate chips")
    .option_hint("sauce", "Chocolate sauce", "it's warm")
    .interact()?;
println!("toppings {:?}", toppings);

MultiInput

The multi_input::MultiInput component accepts multiple lines of text.

use may_clack::multi_input;

let lines = multi_input("idk").interact()?;
println!("lines {:?}", lines);

Dependencies

~7–15MB
~226K SLoC