Confirmation

Struct Confirmation 

Source
pub struct Confirmation { /* private fields */ }
Expand description

A prompt that expects a “yes or no” answer. You can press ‘y’ or ‘n’ for positive or negative result. Pressing ‘Enter’ without any previous input will trigger the default ansewer, which is configurable

use cli_prompts::{
    prompts::{Confirmation, AbortReason},
    style::{ConfirmationStyle, Formatting},
    DisplayPrompt,
};

fn main() {
    let prompt = Confirmation::new("Are you sure you want to delete this file?")
                    .default_positive(false)
                    .style(ConfirmationStyle::default());
    let answer : Result<bool, AbortReason> = prompt.display();
    match answer {
        Ok(is_positive) => println!("The answer is {}", is_positive),
        Err(abort_reason) => println!("The prompt was aborted because of {:?}", abort_reason),
    }
}

Implementations§

Source§

impl Confirmation

Source

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

Constructs a new prompt with a given label

Examples found in repository?
examples/styling.rs (line 38)
17fn main() -> Result<()> {
18    let label_style = LabelStyle::default()
19        .prefix("*")
20        .prefix_formatting(Formatting::default().foreground_color(Color::Cyan))
21        .prompt_formatting(
22            Formatting::default()
23                .bold()
24                .underline()
25                .foreground_color(Color::Magenta),
26        );
27    let input_formatting = Formatting::default().foreground_color(Color::Cyan);
28    let submitted_formatting = Formatting::default().foreground_color(Color::DarkCyan);
29
30    let name = Input::new("Enter your name", name_validation)
31        .style(
32            InputStyle::default()
33                .label_style(label_style.clone())
34                .input_formatting(input_formatting.clone())
35                .submitted_formatting(submitted_formatting.clone()),
36        )
37        .display();
38    let coffee = Confirmation::new("Do you want a cup of coffee")
39        .style(
40            ConfirmationStyle::default()
41                .label_style(label_style.clone())
42                .input_formatting(input_formatting.clone())
43                .submitted_formatting(submitted_formatting.clone()),
44        )
45        .display();
46
47    println!("Name: {:?}", name);
48    println!("Coffee: {:?}", coffee);
49
50    Ok(())
51}
More examples
Hide additional examples
examples/general.rs (line 45)
21fn main() {
22    let desserts = [
23        "Tiramisu",
24        "Cheesecake",
25        "Brownee",
26        "Cookie",
27        "Jelly",
28        "Chupa-Chups",
29        "Pudding",
30    ];
31    let subjects = [
32        "Physics",
33        "Math",
34        "Polish",
35        "English",
36        "Sport",
37        "Geography",
38        "History",
39    ];
40    let cars = [CarModel::Audi, CarModel::BMW, CarModel::Chevrolet];
41
42    let input_prompt = Input::new("Enter your name", |s| Ok(s.to_string()))
43        .default_value("John")
44        .help_message("Please provide your real name");
45    let confirmation = Confirmation::new("Do you want a cup of coffee?").default_positive(true);
46    let dessert_selection = Selection::new("Your favoite dessert", desserts.into_iter());
47    let car_selection =
48        Selection::new_with_transformation("Your car model", cars.into_iter(), car_to_string);
49    let subjects_selection =
50        Multiselect::new("What are your favourite subjects", subjects.into_iter());
51
52    let name = input_prompt.display();
53    let is_coffee = confirmation.display();
54    let dessert = dessert_selection.display();
55    let car = car_selection.display();
56    let subjects = subjects_selection.display();
57
58    println!("Name: {:?}", name);
59    println!("Is coffee: {:?}", is_coffee);
60    println!("Dessert: {:?}", dessert);
61    println!("Car: {:?}", car);
62    println!("Subjects: {:?}", subjects);
63}
Source

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

Sets whether the default value is positive or negative

Examples found in repository?
examples/general.rs (line 45)
21fn main() {
22    let desserts = [
23        "Tiramisu",
24        "Cheesecake",
25        "Brownee",
26        "Cookie",
27        "Jelly",
28        "Chupa-Chups",
29        "Pudding",
30    ];
31    let subjects = [
32        "Physics",
33        "Math",
34        "Polish",
35        "English",
36        "Sport",
37        "Geography",
38        "History",
39    ];
40    let cars = [CarModel::Audi, CarModel::BMW, CarModel::Chevrolet];
41
42    let input_prompt = Input::new("Enter your name", |s| Ok(s.to_string()))
43        .default_value("John")
44        .help_message("Please provide your real name");
45    let confirmation = Confirmation::new("Do you want a cup of coffee?").default_positive(true);
46    let dessert_selection = Selection::new("Your favoite dessert", desserts.into_iter());
47    let car_selection =
48        Selection::new_with_transformation("Your car model", cars.into_iter(), car_to_string);
49    let subjects_selection =
50        Multiselect::new("What are your favourite subjects", subjects.into_iter());
51
52    let name = input_prompt.display();
53    let is_coffee = confirmation.display();
54    let dessert = dessert_selection.display();
55    let car = car_selection.display();
56    let subjects = subjects_selection.display();
57
58    println!("Name: {:?}", name);
59    println!("Is coffee: {:?}", is_coffee);
60    println!("Dessert: {:?}", dessert);
61    println!("Car: {:?}", car);
62    println!("Subjects: {:?}", subjects);
63}
Source

pub fn style(self, s: ConfirmationStyle) -> Self

Sets the style of the prompt

Examples found in repository?
examples/styling.rs (lines 39-44)
17fn main() -> Result<()> {
18    let label_style = LabelStyle::default()
19        .prefix("*")
20        .prefix_formatting(Formatting::default().foreground_color(Color::Cyan))
21        .prompt_formatting(
22            Formatting::default()
23                .bold()
24                .underline()
25                .foreground_color(Color::Magenta),
26        );
27    let input_formatting = Formatting::default().foreground_color(Color::Cyan);
28    let submitted_formatting = Formatting::default().foreground_color(Color::DarkCyan);
29
30    let name = Input::new("Enter your name", name_validation)
31        .style(
32            InputStyle::default()
33                .label_style(label_style.clone())
34                .input_formatting(input_formatting.clone())
35                .submitted_formatting(submitted_formatting.clone()),
36        )
37        .display();
38    let coffee = Confirmation::new("Do you want a cup of coffee")
39        .style(
40            ConfirmationStyle::default()
41                .label_style(label_style.clone())
42                .input_formatting(input_formatting.clone())
43                .submitted_formatting(submitted_formatting.clone()),
44        )
45        .display();
46
47    println!("Name: {:?}", name);
48    println!("Coffee: {:?}", coffee);
49
50    Ok(())
51}

Trait Implementations§

Source§

impl Prompt<bool> for Confirmation

Source§

fn draw(&self, commands: &mut impl CommandBuffer)

Defines how to draw the prompt with a set of commands. The goal of this method is to populate the commands buffer with a set of commands that will draw your prompt to the screen.
Source§

fn on_key_pressed(&mut self, key: Key) -> EventOutcome<bool>

This should handle the keyboard key presses. Should return the outcome of the keypress: Read more

Auto Trait Implementations§

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, P> DisplayPrompt<T> for P
where P: Prompt<T>,

Source§

fn display(self) -> Result<T, AbortReason>

Draws the prompt on the screen and handles the input. 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 = 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.