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 Question
s, and then prompting them to the user. There are 11
in-built Question
s, but if none of them fit your need, you can create your own!
There are 2 ways of creating Question
s.
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
Question
s 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 thequestions
andprompt_module
macros. -
smallvec
(default): Enabling this feature will useSmallVec
instead ofVec
for auto completions. This allows inlining single completions. -
crossterm
(default): Enabling this feature will use thecrossterm
library for terminal interactions such as drawing and receiving events. -
termion
: Enabling this feature will use thetermion
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
Special characters used for prompts/widgets.
Macros
macros
A macro to easily write a PromptModule
.
Structs
A representation of a Choice
for a particular key.
A collection of questions and answers for previously answered questions.
Enums
The errors that can occur in requestty
.
What to do after receiving Esc
Functions
crossterm
or termion
Prompt all the questions in the given iterator, with the default Backend
and EventIterator
.
crossterm
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.