Skip to main content

NumberInput

Struct NumberInput 

Source
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

Source

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
Hide additional 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}
Source

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}
Source

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
Hide additional 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}
Source

pub fn step(self, step: f64) -> Self

Sets the step used by Up/Down.

Source

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

Sets the number of decimal places shown by step adjustments.

Source

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}
Source

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}
Source

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§

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 = 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.