demand

Struct Dialog

Source
pub struct Dialog<'a> {
    pub title: String,
    pub theme: &'a Theme,
    pub description: String,
    pub buttons: Vec<DialogButton>,
    /* private fields */
}
Expand description

A dialog to display to the user

§Example

use demand::Dialog;
use demand::DialogButton;

let dialog = Dialog::new("Are you sure?")
  .description("This will do a thing.")
  .buttons(vec![
     DialogButton::new("Ok"),
     DialogButton::new("Not sure"),
     DialogButton::new("Cancel"),
  ]);
let choice = match dialog.run() {
  Ok(value) => value,
  Err(e) => {
      if (e.kind() == std::io::ErrorKind::Interrupted) {
          println!("Dialog cancelled");
          return;
      } else {
          panic!("Error: {}", e);
      }
  }
};

Fields§

§title: String

The title of the selector

§theme: &'a Theme

The colors/style of the selector

§description: String

A description to display above the selector

§buttons: Vec<DialogButton>

The buttons to display to the user

Implementations§

Source§

impl<'a> Dialog<'a>

Source

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

Create a new dialog with the given title

By default, the dialog will have a single “Ok” button and a “Cancel” button.

Source

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

Set the description of the dialog

Source

pub fn buttons(self, buttons: Vec<DialogButton>) -> Self

Set the buttons of the dialog

Source

pub fn selected_button(self, idx: usize) -> Self

Set the index of the initially selected button.

The idx is the index of the button in the buttons vector and is 0-indexed.

§Errors

This will panic if there are no buttons to select or if the index is out of bounds.

Source

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

Set the theme of the dialog

Source

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

Displays the dialog to the user and returns their response.

The response will be the label of the selected button.

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 Dialog<'a>

§

impl<'a> RefUnwindSafe for Dialog<'a>

§

impl<'a> Send for Dialog<'a>

§

impl<'a> Sync for Dialog<'a>

§

impl<'a> Unpin for Dialog<'a>

§

impl<'a> UnwindSafe for Dialog<'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.