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 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);

Modules§

confirm
Confirm
error
Error
input
Text input
multi_input
Multiple text inputs
multi_select
Select multiple options
select
Select option
style
Style utility
traits
Traits

Macros§

cancel
Cancel message.
err
Error message.
info
Info message.
intro
Intro message.
outro
Setup outro
warn
Warn message.

Functions§

confirm
Shorthand for Confirm::new()
input
Shorthand for Input::new()
multi_input
Shorthand for MultiInput::new()
multi_select
Shorthand for MultiSelect::new()
select
Shorthand for Select::new()