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
PromptModuleuse 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 thequestionsandprompt_modulemacros. -
smallvec(default): Enabling this feature will useSmallVecinstead ofVecfor auto completions. This allows inlining single completions. -
crossterm(default): Enabling this feature will use thecrosstermlibrary for terminal interactions such as drawing and receiving events. -
termion: Enabling this feature will use thetermionlibrary 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§
- prompt
- A module that re-exports all the things required for writing custom
Prompts. - question
- A module that contains things related to
Questions. - symbols
- Special characters used for prompts/widgets.
Macros§
- prompt_
module macros - A macro to easily write a
PromptModule. - questions
macros - A macro to easily write an iterator of
Questions.
Structs§
- Answers
- A collections of answers of previously asked
Questions. - Expand
Item - A representation of a
Choicefor a particular key. - List
Item - A representation of a
Choiceat a particular index. - Prompt
Module - A collection of questions and answers for previously answered questions.
Enums§
- Answer
- The different answer types that can be returned by the
Questions - Error
Kind - The errors that can occur in
requestty. - OnEsc
- What to do after receiving
Esc
Functions§
- prompt
crosstermortermion - Prompt all the questions in the given iterator, with the default
BackendandEventIterator. - prompt_
one crosstermortermion - Prompt the given question, with the default
BackendandEventIterator. - prompt_
one_ with - Prompt the given question, with the given
BackendandEventIterator. - prompt_
with - Prompt all the questions in the given iterator, with the given
BackendandEventIterator.
Type Aliases§
- Result
- The
requesttyresult type.