pub struct NumberInput { /* private fields */ }Expand description
A numeric input prompt with optional calculator expressions.
§Examples
use sparcli::{NumberInput, Outcome};
if let Outcome::Submitted(count) =
NumberInput::new("How many?").range(0.0, 100.0).run()?
{
println!("count = {count}");
}Implementations§
Source§impl NumberInput
impl NumberInput
Sourcepub fn new(prompt: impl Into<String>) -> Self
pub fn new(prompt: impl Into<String>) -> Self
Creates a number prompt with the given label.
Examples found in repository?
examples/prompt-readme.rs (line 72)
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 50)
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 initial(self, value: f64) -> Self
pub fn initial(self, value: f64) -> Self
Sets the initial value.
Examples found in repository?
examples/prompt-readme.rs (line 73)
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}Sourcepub fn range(self, min: f64, max: f64) -> Self
pub fn range(self, min: f64, max: f64) -> Self
Sets the inclusive [min, max] bounds.
Examples found in repository?
examples/prompt-readme.rs (line 74)
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 51)
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 decimals(self, decimals: usize) -> Self
pub fn decimals(self, decimals: usize) -> Self
Sets the number of decimal places shown by step adjustments.
Sourcepub fn calculator(self) -> Self
pub fn calculator(self) -> Self
Enables calculator expressions (+ - * / ( )).
Examples found in repository?
examples/prompts.rs (line 52)
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<f64>>
pub fn run(self) -> Result<Outcome<f64>>
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 53)
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 75)
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 NumberInput
impl RefUnwindSafe for NumberInput
impl Send for NumberInput
impl Sync for NumberInput
impl Unpin for NumberInput
impl UnsafeUnpin for NumberInput
impl UnwindSafe for NumberInput
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