Skip to content
generated from un-rust/template

πŸ“‹ Interactive CLI prompts library, lightweight and easy to use.

License

Notifications You must be signed in to change notification settings

un-rust/promptt

πŸ“‹ 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

About

πŸ“‹ Interactive CLI prompts library, lightweight and easy to use.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Contributors