Expand description

requestty (request-tty) is an easy-to-use collection of interactive cli prompts inspired by Inquirer.js.

Questions

This crate is based on creating Questions, and then prompting them to the user. There are 11 in-built Questions, but if none of them fit your need, you can create your own!

There are 2 ways of creating Questions.

Using builders

use requestty::{Question, Answers};

let question = Question::expand("toppings")
    .message("What toppings do you want?")
    .when(|answers: &Answers| !answers["custom_toppings"].as_bool().unwrap())
    .choice('p', "Pepperoni and cheese")
    .choice('a', "All dressed")
    .choice('w', "Hawaiian")
    .build();

See Question for more information on the builders.

Using macros (only with macros feature)

Unlike the builder api, the macros can only be used to create a list of questions.

use requestty::{questions, Answers};

let questions = questions! [
    Expand {
        name: "toppings",
        message: "What toppings do you want?",
        when: |answers: &Answers| !answers["custom_toppings"].as_bool().unwrap(),
        choices: [
            ('p', "Pepperoni and cheese"),
            ('a', "All dressed"),
            ('w', "Hawaiian"),
        ]
    }
];

See questions and prompt_module for more information on the macros.

Prompting

Questions can be asked in 2 main ways.

  • Using direct functions provided by the crate.

    let questions = vec![
        // Declare the questions you want to ask
    ];
    
    let answers = requestty::prompt(questions)?;
  • Using PromptModule

    use requestty::PromptModule;
    
    let questions = PromptModule::new(vec![
        // Declare the questions you want to ask
    ]);
    
    let answers = questions.prompt_all()?;

    This is mainly useful if you need more control over prompting the questions, and using previous Answers.

See the documentation of Question for more information on the different in-built questions.

Terminal Interaction

Terminal interaction is handled by 2 traits: Backend and EventIterator.

The traits are already implemented and can be enabled with features for the following terminal libraries:

The default is crossterm for the following reasons:

  • Wider terminal support
  • Better event processing (in my experience)

Custom Prompts

If the crate’s in-built prompts does not satisfy your needs, you can build your own custom prompts using the Prompt trait.

Optional features

  • macros: Enabling this feature will allow you to use the questions and prompt_module macros.

  • smallvec (default): Enabling this feature will use SmallVec instead of Vec for auto completions. This allows inlining single completions.

  • crossterm (default): Enabling this feature will use the crossterm library for terminal interactions such as drawing and receiving events.

  • termion: Enabling this feature will use the termion library for terminal interactions such as drawing and receiving events.

Examples

use requestty::Question;

let password = Question::password("password")
    .message("What is your password?")
    .mask('*')
    .build();

let answer = requestty::prompt_one(password)?;

println!("Your password was: {}", answer.as_string().expect("password returns a string"));

For more examples, see the documentation for the various in-built questions, and the examples directory.

Re-exports

pub use question::Choice::Choice;
pub use question::Choice::DefaultSeparator;
pub use question::Choice::Separator;
pub use question::Question;

Modules

A module that re-exports all the things required for writing custom Prompts.

A module that contains things related to Questions.

Special characters used for prompts/widgets.

Macros

A macro to easily write a PromptModule.

questionsmacros

A macro to easily write an iterator of Questions.

Structs

A collections of answers of previously asked Questions.

A representation of a Choice for a particular key.

A representation of a Choice at a particular index.

A collection of questions and answers for previously answered questions.

Enums

The different answer types that can be returned by the Questions

The errors that can occur in requestty.

What to do after receiving Esc

Functions

promptcrossterm or termion

Prompt all the questions in the given iterator, with the default Backend and EventIterator.

prompt_onecrossterm or termion

Prompt the given question, with the default Backend and EventIterator.

Prompt the given question, with the given Backend and EventIterator.

Prompt all the questions in the given iterator, with the given Backend and EventIterator.

Type Definitions

The requestty result type.