pub struct Terminal;Expand description
Collection of interactive terminal prompt utilities.
All methods block on stdin until the user provides valid input. Invalid responses print an inline error and re-prompt rather than returning an error value, so callers always receive a valid result.
Implementations§
Source§impl Terminal
impl Terminal
Sourcepub fn prompt(prompt: &str) -> String
pub fn prompt(prompt: &str) -> String
Prints prompt and reads a line from stdin.
A single trailing space is appended to prompt before printing so the
cursor lands one character after the prompt text.
Sourcepub fn confirm(
prompt: &str,
default: Option<bool>,
show_default: Option<bool>,
) -> bool
pub fn confirm( prompt: &str, default: Option<bool>, show_default: Option<bool>, ) -> bool
Asks a yes/no question and returns the user’s answer as a bool.
Re-prompts on any input that is not y, yes, n, no, or an empty
string (which selects the default).
§Arguments
prompt- The question text, without any trailing suffix.default- The value returned when the user presses Enter with no input. Defaults tofalseifNone.show_default- Whentrue(orNone), appends[Y/n]or[y/N]to the prompt to indicate the default visually.
Sourcepub fn choice(
prompt: &str,
choices: &[&str],
default: Option<&str>,
show_default: Option<bool>,
show_choices: Option<bool>,
) -> String
pub fn choice( prompt: &str, choices: &[&str], default: Option<&str>, show_default: Option<bool>, show_choices: Option<bool>, ) -> String
Asks the user to pick one option from a fixed set of choices.
Matching is case-insensitive. Re-prompts until the user enters a recognized choice or presses Enter when a default is set.
§Arguments
prompt- The question text.choices- The allowed responses. Must not be empty.default- Optional default value returned on empty input. Must be present inchoicesif provided.show_default- Whentrue(orNone), marks the default choice with a*in the suffix list.show_choices- Whentrue(orNone), appends the full choice list to the prompt as[a/b*/c].
§Panics
Panics if choices is empty, or if default is set to a value not
present in choices.