Skip to main content

ExpandBuilder

Struct ExpandBuilder 

Source
pub struct ExpandBuilder<'a> { /* private fields */ }
Expand description

The builder for a expand prompt.

The keys are ascii case-insensitive characters. The ‘h’ option is added by the prompt and shouldn’t be defined.

The choices are represented with the Choice enum. Choice::Choice can be multi-line, but Choice::Separators can only be single line.

See the various methods for more details on each available option.

§Examples

use reequestty::Question;

let expand = Question::expand("overwrite")
    .message("Conflict on `file.rs`")
    .choices(vec![
        ('y', "Overwrite"),
        ('a', "Overwrite this one and all next"),
        ('d', "Show diff"),
    ])
    .default_separator()
    .choice('x', "Abort")
    .build();

Implementations§

Source§

impl<'a> ExpandBuilder<'a>

Source

pub fn message<M>(self, message: M) -> Self
where M: Into<Getter<'a, String>>,

The message to display when the prompt is rendered in the terminal.

It can be either a String or a FnOnce that returns a String. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, the message defaults to “<name>: “.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .message("Conflict on `file.rs`")
    .build();
Source

pub fn when<W>(self, when: W) -> Self
where W: Into<Getter<'a, bool>>,

Whether to ask the question (true) or not (false).

It can be either a bool or a FnOnce that returns a bool. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, it defaults to true.

§Examples
use reequestty::{Question, Answers};

let expand = Question::expand("overwrite")
    .when(|previous_answers: &Answers| match previous_answers.get("ignore-conflicts") {
        Some(ans) => ans.as_bool().unwrap(),
        None => true,
    })
    .build();
Source

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

Prompt the question even if it is answered.

By default if an answer with the given name already exists, the question will be skipped. This can be overridden by setting ask_if_answered is set to true.

If this is not given, it defaults to false.

If you need to dynamically decide whether the question should be asked, use when.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .ask_if_answered(true)
    .build();
Source

pub fn on_esc<T>(self, on_esc: T) -> Self
where T: Into<Getter<'a, OnEsc>>,

Configure what to do when the user presses the Esc key.

It can be either a OnEsc or a FnOnce that returns a OnEsc. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, it defaults to OnEsc::Ignore.

§Examples
use reequestty::{Question, OnEsc};

let expand = Question::expand("overwrite")
    .on_esc(OnEsc::Terminate)
    .build();
Source

pub fn default(self, default: char) -> Self

Set a default key for the expand

If no key is entered by the user and they press Enter, the default key is used.

If default is unspecified, it defaults to the ‘h’ key.

§Panics

If the default given is not a key to a choice it will cause a panic on build

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .choice('d', "Show diff")
    .default('d')
    .build();
Source

pub fn page_size(self, page_size: usize) -> Self

The maximum height that can be taken by the expanded list

If the total height exceeds the page size, the list will be scrollable.

The page_size must be a minimum of 5. If page_size is not set, it will default to 15. It will only be used if the user expands the prompt.

§Panics

It will panic if the page_size is less than 5.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .page_size(10)
    .build();
Source

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

Whether to wrap around when user gets to the last element.

If should_loop is not set, it will default to true. It will only be used if the user expands the prompt.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .should_loop(false)
    .build();
Source

pub fn choice<I: Into<String>>(self, key: char, text: I) -> Self

Inserts a Choice with the given key and text

See expand for more information.

§Panics

It will panic if the key is ‘h’ or a duplicate.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .choice('x', "Abort")
    .build();
Source

pub fn separator<I: Into<String>>(self, text: I) -> Self

Inserts a Separator with the given text

See expand for more information.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .separator("-- custom separator text --")
    .build();
Source

pub fn default_separator(self) -> Self

Inserts a DefaultSeparator

See expand for more information.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .default_separator()
    .build();
Source

pub fn choices<I, T>(self, choices: I) -> Self
where T: Into<Choice<ExpandItem>>, I: IntoIterator<Item = T>,

Extends the given iterator of Choices

See expand for more information.

§Panics

It will panic if the key of any choice is ‘h’ or a duplicate.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .choices(vec![
        ('y', "Overwrite"),
        ('a', "Overwrite this one and all next"),
        ('d', "Show diff"),
    ])
    .build();
Source

pub fn transform<F>(self, transform: F) -> Self
where F: FnOnce(&ExpandItem, &Answers, &mut dyn Backend) -> Result<()> + 'a,

Change the way the answer looks when displayed to the user.

It is a FnOnce that is given the answer, previous Answers and the Backend to display the answer on. After the transform is called, a new line is also added.

It will only be called once the user finishes answering the question.

§Examples
use reequestty::Question;

let expand = Question::expand("overwrite")
    .transform(|choice, previous_answers, backend| {
        write!(backend, "({}) {}", choice.key, choice.text)
    })
    .build();
Source

pub fn build(self) -> Question<'a>

Consumes the builder returning a Question

Trait Implementations§

Source§

impl<'a> Debug for ExpandBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<ExpandBuilder<'a>> for Question<'a>

Source§

fn from(builder: ExpandBuilder<'a>) -> Self

Consumes the builder returning a Question

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a> Freeze for ExpandBuilder<'a>

§

impl<'a> Unpin for ExpandBuilder<'a>

§

impl<'a> UnsafeUnpin for ExpandBuilder<'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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.