Crate trompt [] [src]

A simple utility to prompt users of your CLI app.

Examples

extern crate trompt;

use trompt::Trompt;

fn main() {
    // Normally you would want to something like this:
    // let stdin = std::io::stdin();
    // let mut input = stdin.lock();
    // let mut output = std::io::stdout();

    // But for now we will use cursors instead of stdin, and stdout.
    use std::io::Cursor;

    // Normally this would come a user after they see the prompt.
    let mut input = Cursor::new(b"tupac\n");
    let mut output = Cursor::new(Vec::new());

    // Here the user is asked for a username.
    let username = Trompt::new(&mut input, &mut output)
        .required()
        .prompt("Username: ");

    assert!(username.is_ok());
    assert_eq!(username.unwrap(), "tupac");
    assert_eq!(output.into_inner(), b"Username: ");

    // Same as prompt, but the console will not echo the user input.
    let mut input = Cursor::new(b"is the greatest\n");
    let mut output = Cursor::new(Vec::new());

    let password = Trompt::new(&mut input, &mut output)
        .silent()
        .min_len(8)
        .prompt("Password: ");

    // We can use `confirm` to ask a question.
    let mut input = Cursor::new(b"YES\n");
    let mut output = Cursor::new(Vec::new());

    let confirmed = Trompt::new(&mut input, &mut output)
        .confirm("Are you sure [yn]? ");

    assert_eq!(confirmed.unwrap(), true);
    assert_eq!(output.into_inner(), b"Are you sure [yn]? ");
}

Structs

Trompt

Enums

Error
ValidationError

Type Definitions

Result