promptt 1.0.8

A interactive CLI prompts library, lightweight and easy to use.
Documentation

promptt

Crates.io Version Crates.io Total Downloads docs.rs GitHub commit activity GitHub Repo stars

Lightweight interactive CLI prompts for Rust: text, confirm, number, select, toggle, list, password, invisible.

Full documentation →

Install

cargo add promptt

Usage

Build a list of Questions and run prompt(). Answers are HashMap<String, PromptValue>.

use promptt::{prompt, Question, PromptValue};
use std::io::{self, BufRead};

fn main() -> io::Result<()> {
    let questions = vec![
        Question {
            name: "username".into(),
            type_name: "text".into(),
            message: "What is your name?".into(),
            ..Default::default()
        },
        Question {
            name: "confirm".into(),
            type_name: "confirm".into(),
            message: "Continue?".into(),
            initial_bool: Some(true),
            ..Default::default()
        },
    ];

    let mut stdin = io::stdin().lock();
    let mut stdout = io::stdout();
    let answers = prompt(&questions, &mut stdin, &mut stdout)?;

    if let Some(PromptValue::String(name)) = answers.get("username") {
        println!("Hello, {}!", name);
    }
    if let Some(PromptValue::Bool(yes)) = answers.get("confirm") {
        println!("You chose: {}", if *yes { "yes" } else { "no" });
    }
    Ok(())
}

Prompt types

Type Result Options / notes
text PromptValue::String initial_text
password PromptValue::String Masked input
invisible PromptValue::String No echo
number PromptValue::Float min/max, float, round
confirm PromptValue::Bool initial_bool
toggle PromptValue::Bool active/inactive labels
select PromptValue::String choices (title/value)
list PromptValue::List separator (default ,)

More in the documentation and docs.rs.

License

Published under the MIT license. Made by @YONGQI 💛


🛠️ auto updated with automd-rs