Crate may_clack

source ·
Expand description

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 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 will return a error::ClackError::Cancelled.

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

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

let text = input("todo").interact();
if let Err(ClackError::Cancelled) = text {
    cancel("operation cancelled")
}

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("idk", "idk", "idk")
    .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);

Modules

Functions