promptt 1.1.0

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

Interactive CLI prompts library, lightweight and easy to use.

Full documentation →

Quick start

cargo add promptt

Usage

//! Demo: all prompt types supported by the promptt library.
//!
//! Run with: cargo run

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

fn main() -> io::Result<()> {
    let stdin = io::stdin();
    let mut stdin = stdin.lock();
    let mut stdout = io::stdout();

    let questions: Vec<Question> = vec![
        // text: plain input, optional default
        Question {
            name: "name".into(),
            type_name: "text".into(),
            message: "Your name?".into(),
            initial_text: Some("anonymous".into()),
            ..Default::default()
        },
        // confirm: yes/no (Y/n or y/N)
        Question {
            name: "proceed".into(),
            type_name: "confirm".into(),
            message: "Continue?".into(),
            initial_bool: Some(true),
            ..Default::default()
        },
        // select: pick from choices
        Question {
            name: "version".into(),
            type_name: "select".into(),
            message: "Bump version".into(),
            choices: Some(vec![
                Choice::new("Major", "major"),
                Choice::new("Minor", "minor"),
                Choice::new("Patch", "patch"),
            ]),
            ..Default::default()
        },
    ];

    let answers = prompt(&questions, &mut stdin, &mut stdout)?;

    writeln!(stdout, "\n--- Answers ---")?;
    for (name, value) in &answers {
        let s = match value {
            PromptValue::String(v) => v.clone(),
            PromptValue::Bool(v) => v.to_string(),
        };
        writeln!(stdout, "  {}: {}", name, s)?;
    }
    stdout.flush()?;

    Ok(())
}

License

Published under the Apache-2.0 license. Made by @UnRUST 💛


🛠️ auto updated with automd-rs