Crate asky

Source
Expand description

Good looking prompts for the terminal

§Available prompts

  • Confirm - Ask yes/no questions.
  • Toggle - Choose between two options.
  • Text - One-line user input.
  • Number - One-line user input of numbers.
  • Password - One-line user input as password.
  • Select - Select an item from a list.
  • MultiSelect - Select multiple items from a list.

§Simple Example

use asky::{Confirm, Text};

fn main() -> std::io::Result<()> {
    let name = Text::new("Hi. What's your name?").prompt()?;

    if Confirm::new("Do you like coffee?").prompt()? {
        println!("Great! Me too");
    } else {
        println!("Hmm... Interesting");
    }

    // ...

    Ok(())
}

§Customization

If you’d like to use this crate but don’t want the default styles or just want to customize as you like, all the prompts allow setting a custom formatter using format() method.

The formatter receives a prompt state reference and a DrawTime, and returns the string to display in the terminal.

Note: When using a custom formatter, you are responsible for the presentation of the prompt, so you must handle the colors, icons, etc. by yourself.

§Example
Confirm::new("Do you like Rust?")
    .format(|prompt, _draw_time| {
        let state = if prompt.active { "Y/n" } else { "y/N" };
        format!("{} {}\n", prompt.message, state)
    })
    .prompt();

This will prints

Do you like Rust? y/N

§Cursor Position

Almost all the prompts just need a custom string, but some prompts like Text also requires an array of [x, y] position for the cursor, due to these prompts also depends on the cursor position in the process.

§Example
Text::new("What is your name")
    .format(|prompt, _draw_time| {
        let cursor_col = prompt.input.col;
        let prefix = "> ";

        let x = (prefix.len() + cursor_col);
        let y = 1;

        (
            format!("{}\n{} {}", prompt.message, prefix, prompt.input.value),
            [x, y],
        )
    })
    .prompt();

This will prints

What is your name?
> |

Where | is the cursor position.

Structs§

Confirm
Prompt to ask yes/no questions.
LineInput
State of the user input for read-line text prompts (like Text).
MultiSelect
Prompt to select multiple items from a list.
Number
Prompt to get one-line user input of numbers.
Password
Prompt to get one-line user input as password.
Select
Prompt to select an item from a list.
SelectInput
State of the input for select-like prompts (like Select).
SelectOption
Utility struct to create items for select-like prompts (like Select).
Text
Prompt to get one-line user input.
Toggle
Prompt to choose between two options.

Enums§

DrawTime
Enum that indicates the current draw time to format closures.

Traits§

NumLike
A utility trait to allow only numbers in Number prompt. Also allows to custom handle they based on the type.