Skip to main content

Input

Struct Input 

Source
pub struct Input<'a> {
    pub title: String,
    pub description: String,
    pub prompt: String,
    pub placeholder: String,
    pub suggestions: Option<&'a [&'a str]>,
    pub inline: bool,
    pub password: bool,
    pub input: String,
    pub theme: &'a Theme,
    pub validation: Box<dyn InputValidator>,
    /* private fields */
}
Expand description

Single line text input

§Example

use demand::Input;

let input = Input::new("What's your name?")
  .description("We'll use this to personalize your experience.")
  .placeholder("Enter your name");
let name = match input.run() {
  Ok(value) => value,
  Err(e) => {
      if e.kind() == std::io::ErrorKind::Interrupted {
          println!("Input cancelled");
          return;
      } else {
          panic!("Error: {}", e);
      }
  }
};

Fields§

§title: String

The title of the input

§description: String

A description to display after the title

§prompt: String

A prompt to display after the description

§placeholder: String

A placeholder to display in the input

§suggestions: Option<&'a [&'a str]>

A list of suggestions to autocomplete from (legacy, prefer using autocomplete())

§inline: bool

Show the input inline

§password: bool

Whether to mask the input

§input: String

Input entered by the user

§theme: &'a Theme

Colors/style of the input

§validation: Box<dyn InputValidator>

Validation function

Implementations§

Source§

impl<'a> Input<'a>

Source

pub fn new<S: Into<String>>(title: S) -> Self

Creates a new input with the given title.

Source

pub fn description(self, description: &str) -> Self

Sets the description of the input.

If the input is inline, it is displayed to the right of the title. Otherwise, it is displayed below the title.

Source

pub fn inline(self, inline: bool) -> Self

Sets the inline flag of the input.

If true, the input is displayed inline with the title

Source

pub fn password(self, password: bool) -> Self

Sets the password flag of the input.

If true, the input is masked with asterisks

Source

pub fn placeholder(self, placeholder: &str) -> Self

Sets the placeholder of the input.

The placeholder is displayed in the input before the user enters any text

Source

pub fn suggestions(self, suggestions: &'a [&'a str]) -> Self

Sets the suggestions of the input (legacy method)

Source

pub fn autocomplete<A: Autocomplete + 'static>(self, autocompleter: A) -> Self

Sets a custom autocompleter for the input.

The autocompleter will be used to generate suggestions and completions based on user input.

Source

pub fn autocomplete_fn<F>(self, suggester: F) -> Self
where F: Fn(&str) -> Result<Vec<String>, Box<dyn Error>> + Clone + 'static,

Sets a function-based autocompleter for the input.

The function receives the current input and should return a list of suggestions.

Source

pub fn max_suggestions_display(self, max: usize) -> Self

Sets the maximum number of suggestions to display

Source

pub fn prompt(self, prompt: &str) -> Self

Sets the prompt of the input.

The prompt is displayed after the title and description. If empty, the default prompt > is displayed.

Source

pub fn default_value(self, default_value: impl Into<String>) -> Self

Sets the default value of the input.

Source

pub fn theme(self, theme: &'a Theme) -> Self

Sets the theme of the input

Source

pub fn validation(self, validation: fn(&str) -> Result<(), &str>) -> Self

Sets the validation for the input.

If the input is valid, the Result is Ok(()). Otherwise, the Result is Err(&str).

Source

pub fn validator(self, validation: impl InputValidator + 'static) -> Self

Sets the validator for the input.

This is similar to the Input::validation method, but it’s more flexible. See InputValidator for examples

Source

pub fn run(self) -> Result<String>

Displays the input to the user and returns the response

This function will block until the user submits the input. If the user cancels the input, an error of type io::ErrorKind::Interrupted is returned.

Auto Trait Implementations§

§

impl<'a> Freeze for Input<'a>

§

impl<'a> !RefUnwindSafe for Input<'a>

§

impl<'a> !Send for Input<'a>

§

impl<'a> !Sync for Input<'a>

§

impl<'a> Unpin for Input<'a>

§

impl<'a> !UnwindSafe for Input<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.