pub struct Confirm { /* private fields */ }Expand description
A yes/no confirmation prompt.
Implementations§
Source§impl Confirm
impl Confirm
Sourcepub fn new(question: impl Into<String>) -> Self
pub fn new(question: impl Into<String>) -> Self
Creates a confirmation prompt (defaults to “No”).
Examples found in repository?
examples/prompt-readme.rs (line 68)
65fn left_column() -> Rendered {
66 vstack(
67 &[
68 Confirm::new("Deploy to production?").default_yes().frame(),
69 blank(),
70 TextInput::new("Service").initial("api-gateway").frame(),
71 PasswordInput::new("Password").initial("hunter2").frame(),
72 NumberInput::new("Replicas")
73 .initial(3.0)
74 .range(1.0, 10.0)
75 .frame(),
76 TextInput::new("Email")
77 .placeholder("you@example.com")
78 .frame(),
79 TextInput::new("Region")
80 .initial("eu-")
81 .suggestions(["eu-central-1"])
82 .frame(),
83 blank(),
84 Textarea::new("Notes")
85 .initial("first line\nsecond line\nthird line")
86 .frame(),
87 ],
88 0,
89 )
90}More examples
examples/prompts.rs (line 76)
28fn main() -> sparcli::Result<()> {
29 footer_hint()?;
30
31 let name = get!(
32 TextInput::new("Your name?")
33 .placeholder("e.g. Alice")
34 .validate(non_empty())
35 .run()?
36 );
37
38 let _username = get!(
39 TextInput::new("Username?")
40 .char_filter(alnum())
41 .max_chars(16)
42 .suggestions(["alice", "albert", "bob", "carol"])
43 .history(["alice", "bob"])
44 .run()?
45 );
46
47 let _password = get!(PasswordInput::new("Password?").mask("•").run()?);
48
49 let age = get!(
50 NumberInput::new("Age? (try `= 20 + 2`)")
51 .range(0.0, 130.0)
52 .calculator()
53 .run()?
54 );
55
56 let _bio = get!(Textarea::new("Short bio (Ctrl-D to submit):").run()?);
57
58 let color = get!(
59 Select::new("Favorite color?")
60 .options(["red", "green", "blue"])
61 .run()?
62 );
63
64 let toppings = get!(
65 Select::new("Pick toppings (Space to toggle):")
66 .options(["cheese", "mushroom", "olive", "onion"])
67 .multi()
68 .run_multi()?
69 );
70
71 let _fruit = get!(pick_fruit()?);
72
73 let date = get!(DatePicker::new("Pick a date:").run()?);
74
75 let confirmed = matches!(
76 Confirm::new("Save everything?")
77 .default_yes()
78 .labels("Save", "Discard")
79 .run()?,
80 Outcome::Submitted(true)
81 );
82
83 if confirmed {
84 Alert::success(format!(
85 "Saved {name}, age {age:.0}, color #{color}, {} toppings, \
86 date {}-{:02}-{:02}.",
87 toppings.len(),
88 date.year,
89 date.month,
90 date.day,
91 ))
92 .print()?;
93 } else {
94 cancelled()?;
95 }
96 Ok(())
97}Sourcepub fn shortcuts<I>(self, shortcuts: I) -> Selfwhere
I: IntoIterator<Item = Shortcut>,
pub fn shortcuts<I>(self, shortcuts: I) -> Selfwhere
I: IntoIterator<Item = Shortcut>,
Registers shortcuts shown in a footer hint and the ? help overlay.
Pressing a bound key ends the prompt with Outcome::Shortcut.
Sourcepub fn default_yes(self) -> Self
pub fn default_yes(self) -> Self
Sets the initial selection to “Yes”.
Examples found in repository?
examples/prompt-readme.rs (line 68)
65fn left_column() -> Rendered {
66 vstack(
67 &[
68 Confirm::new("Deploy to production?").default_yes().frame(),
69 blank(),
70 TextInput::new("Service").initial("api-gateway").frame(),
71 PasswordInput::new("Password").initial("hunter2").frame(),
72 NumberInput::new("Replicas")
73 .initial(3.0)
74 .range(1.0, 10.0)
75 .frame(),
76 TextInput::new("Email")
77 .placeholder("you@example.com")
78 .frame(),
79 TextInput::new("Region")
80 .initial("eu-")
81 .suggestions(["eu-central-1"])
82 .frame(),
83 blank(),
84 Textarea::new("Notes")
85 .initial("first line\nsecond line\nthird line")
86 .frame(),
87 ],
88 0,
89 )
90}More examples
examples/prompts.rs (line 77)
28fn main() -> sparcli::Result<()> {
29 footer_hint()?;
30
31 let name = get!(
32 TextInput::new("Your name?")
33 .placeholder("e.g. Alice")
34 .validate(non_empty())
35 .run()?
36 );
37
38 let _username = get!(
39 TextInput::new("Username?")
40 .char_filter(alnum())
41 .max_chars(16)
42 .suggestions(["alice", "albert", "bob", "carol"])
43 .history(["alice", "bob"])
44 .run()?
45 );
46
47 let _password = get!(PasswordInput::new("Password?").mask("•").run()?);
48
49 let age = get!(
50 NumberInput::new("Age? (try `= 20 + 2`)")
51 .range(0.0, 130.0)
52 .calculator()
53 .run()?
54 );
55
56 let _bio = get!(Textarea::new("Short bio (Ctrl-D to submit):").run()?);
57
58 let color = get!(
59 Select::new("Favorite color?")
60 .options(["red", "green", "blue"])
61 .run()?
62 );
63
64 let toppings = get!(
65 Select::new("Pick toppings (Space to toggle):")
66 .options(["cheese", "mushroom", "olive", "onion"])
67 .multi()
68 .run_multi()?
69 );
70
71 let _fruit = get!(pick_fruit()?);
72
73 let date = get!(DatePicker::new("Pick a date:").run()?);
74
75 let confirmed = matches!(
76 Confirm::new("Save everything?")
77 .default_yes()
78 .labels("Save", "Discard")
79 .run()?,
80 Outcome::Submitted(true)
81 );
82
83 if confirmed {
84 Alert::success(format!(
85 "Saved {name}, age {age:.0}, color #{color}, {} toppings, \
86 date {}-{:02}-{:02}.",
87 toppings.len(),
88 date.year,
89 date.month,
90 date.day,
91 ))
92 .print()?;
93 } else {
94 cancelled()?;
95 }
96 Ok(())
97}Sourcepub fn labels(self, yes: impl Into<String>, no: impl Into<String>) -> Self
pub fn labels(self, yes: impl Into<String>, no: impl Into<String>) -> Self
Sets custom labels for the two options.
Examples found in repository?
examples/prompts.rs (line 78)
28fn main() -> sparcli::Result<()> {
29 footer_hint()?;
30
31 let name = get!(
32 TextInput::new("Your name?")
33 .placeholder("e.g. Alice")
34 .validate(non_empty())
35 .run()?
36 );
37
38 let _username = get!(
39 TextInput::new("Username?")
40 .char_filter(alnum())
41 .max_chars(16)
42 .suggestions(["alice", "albert", "bob", "carol"])
43 .history(["alice", "bob"])
44 .run()?
45 );
46
47 let _password = get!(PasswordInput::new("Password?").mask("•").run()?);
48
49 let age = get!(
50 NumberInput::new("Age? (try `= 20 + 2`)")
51 .range(0.0, 130.0)
52 .calculator()
53 .run()?
54 );
55
56 let _bio = get!(Textarea::new("Short bio (Ctrl-D to submit):").run()?);
57
58 let color = get!(
59 Select::new("Favorite color?")
60 .options(["red", "green", "blue"])
61 .run()?
62 );
63
64 let toppings = get!(
65 Select::new("Pick toppings (Space to toggle):")
66 .options(["cheese", "mushroom", "olive", "onion"])
67 .multi()
68 .run_multi()?
69 );
70
71 let _fruit = get!(pick_fruit()?);
72
73 let date = get!(DatePicker::new("Pick a date:").run()?);
74
75 let confirmed = matches!(
76 Confirm::new("Save everything?")
77 .default_yes()
78 .labels("Save", "Discard")
79 .run()?,
80 Outcome::Submitted(true)
81 );
82
83 if confirmed {
84 Alert::success(format!(
85 "Saved {name}, age {age:.0}, color #{color}, {} toppings, \
86 date {}-{:02}-{:02}.",
87 toppings.len(),
88 date.year,
89 date.month,
90 date.day,
91 ))
92 .print()?;
93 } else {
94 cancelled()?;
95 }
96 Ok(())
97}Sourcepub fn run(self) -> Result<Outcome<bool>>
pub fn run(self) -> Result<Outcome<bool>>
Runs the prompt on the real terminal.
§Errors
Returns SparcliError::NoTerminal without an interactive terminal,
or SparcliError::Io on a terminal failure.
Examples found in repository?
examples/prompts.rs (line 79)
28fn main() -> sparcli::Result<()> {
29 footer_hint()?;
30
31 let name = get!(
32 TextInput::new("Your name?")
33 .placeholder("e.g. Alice")
34 .validate(non_empty())
35 .run()?
36 );
37
38 let _username = get!(
39 TextInput::new("Username?")
40 .char_filter(alnum())
41 .max_chars(16)
42 .suggestions(["alice", "albert", "bob", "carol"])
43 .history(["alice", "bob"])
44 .run()?
45 );
46
47 let _password = get!(PasswordInput::new("Password?").mask("•").run()?);
48
49 let age = get!(
50 NumberInput::new("Age? (try `= 20 + 2`)")
51 .range(0.0, 130.0)
52 .calculator()
53 .run()?
54 );
55
56 let _bio = get!(Textarea::new("Short bio (Ctrl-D to submit):").run()?);
57
58 let color = get!(
59 Select::new("Favorite color?")
60 .options(["red", "green", "blue"])
61 .run()?
62 );
63
64 let toppings = get!(
65 Select::new("Pick toppings (Space to toggle):")
66 .options(["cheese", "mushroom", "olive", "onion"])
67 .multi()
68 .run_multi()?
69 );
70
71 let _fruit = get!(pick_fruit()?);
72
73 let date = get!(DatePicker::new("Pick a date:").run()?);
74
75 let confirmed = matches!(
76 Confirm::new("Save everything?")
77 .default_yes()
78 .labels("Save", "Discard")
79 .run()?,
80 Outcome::Submitted(true)
81 );
82
83 if confirmed {
84 Alert::success(format!(
85 "Saved {name}, age {age:.0}, color #{color}, {} toppings, \
86 date {}-{:02}-{:02}.",
87 toppings.len(),
88 date.year,
89 date.month,
90 date.day,
91 ))
92 .print()?;
93 } else {
94 cancelled()?;
95 }
96 Ok(())
97}Sourcepub fn frame(&self) -> Rendered
pub fn frame(&self) -> Rendered
Renders the prompt’s static frame without running it (for previews and README screenshots).
Examples found in repository?
examples/prompt-readme.rs (line 68)
65fn left_column() -> Rendered {
66 vstack(
67 &[
68 Confirm::new("Deploy to production?").default_yes().frame(),
69 blank(),
70 TextInput::new("Service").initial("api-gateway").frame(),
71 PasswordInput::new("Password").initial("hunter2").frame(),
72 NumberInput::new("Replicas")
73 .initial(3.0)
74 .range(1.0, 10.0)
75 .frame(),
76 TextInput::new("Email")
77 .placeholder("you@example.com")
78 .frame(),
79 TextInput::new("Region")
80 .initial("eu-")
81 .suggestions(["eu-central-1"])
82 .frame(),
83 blank(),
84 Textarea::new("Notes")
85 .initial("first line\nsecond line\nthird line")
86 .frame(),
87 ],
88 0,
89 )
90}Auto Trait Implementations§
impl Freeze for Confirm
impl RefUnwindSafe for Confirm
impl Send for Confirm
impl Sync for Confirm
impl Unpin for Confirm
impl UnsafeUnpin for Confirm
impl UnwindSafe for Confirm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more