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 input = std::io::stdin();
    // let output = std::io::stdout();
    // ```
    //
    // Or the equivalent:
    //
    // ```
    // let _prompter = Trompt::stdout();
    // ```
    //
    // But for now we will use cursors instead of stdin, and stdout.

    use std::io::Cursor;

    // Normally this would come from the user after they see the
    // prompt.

    let input = Cursor::new(b"tupac");
    let output = Cursor::new(Vec::new());

    // Here the user is asked for a username.

    let username = Trompt::new(input, output)
        .required()
        .prompt("Username: ");

    assert_eq!(username, Ok("tupac".to_string()));

    // Same as prompt, but the console will not echo the user
    // input. Now we use a shorthand to write to stdout.

    let password = Trompt::stdout()
        .silent()
        .min_len(8)
        .prompt("Password: ");

    // We can use `confirm` to ask a question.

    let input = Cursor::new(b"YES");
    let output = Cursor::new(Vec::new());

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

    assert_eq!(confirmed, Ok(true));
}

Structs

Trompt

The state of the prompt that we send to the user. It is general on any input and output that implement Read and Write respectively.

Enums

Error
ValidationError

Type Definitions

Result