pub struct InputOption<Data> {
pub bulletin_string: Option<String>,
pub main_name: String,
pub alt_names: Vec<String>,
pub data: Data,
}Expand description
Allows you to add more data to an option
Example:
// example data
let mut colors = vec!("Red", "green", "Blue");
// prepare options, only capitalized colors can be removed
let mut option_number = 1;
let choosable_colors =
colors.iter().enumerate()
.filter_map(|(i, color_name)| {
let first_char = color_name.chars().next()?;
if first_char.is_lowercase() {return None;}
Some(OptionWithData::new(option_number, color_name, vec!(), i))
})
.collect::<Vec<_>>();
// prompt
let OptionWithData {data: index_to_remove, ..} = prompt!("Choose a color to remove: "; choosable_colors);
colors.remove(index_to_remove);Fields§
§bulletin_string: Option<String>This is what’s displayed before the colon
main_name: StringThis is what’s shown as the option name
alt_names: Vec<String>These are alternate valid strings that the user could enter to choose this option
data: DataExtra data for storing whatever you want
Implementations§
Source§impl<Data> InputOption<Data>
impl<Data> InputOption<Data>
Sourcepub fn new(
bulletin: impl Into<String>,
display: impl Into<String>,
choose: Vec<impl Into<String>>,
data: Data,
) -> Self
pub fn new( bulletin: impl Into<String>, display: impl Into<String>, choose: Vec<impl Into<String>>, data: Data, ) -> Self
Basic initializer
Examples found in repository?
examples/read_lines.rs (line 54)
3fn main() {
4
5 println!("\n==== custom fuzzy-search testing ====");
6 let input = read!(["Lanercoast", "Windrip", "Redwick Bush", "Brickelwhyte", "Sirencester", "Conriston", "Inverness", "Norwich", "Elinmylly", "Murlayfield"]).1;
7 println!("You entered: \"{input}\"");
8
9
10 println!("\n==== `read!()` ====");
11 let input = read!(); // read a line of text
12 println!("You entered: \"{input}\"");
13
14 println!("\n==== `prompt!(\"Enter a string: \")` ====");
15 let input = prompt!("Enter a string: "); // prompt a line of text
16 println!("You entered: \"{input}\"");
17
18 println!("\n==== `read!(UsizeInput)` ====");
19 let input = read!(UsizeInput); // read specific types
20 println!("You entered: \"{input}\"");
21 println!("\n==== `read!(BoolInput)` ====");
22 let input = read!(BoolInput);
23 println!("You entered: \"{input}\"");
24 println!("\n==== `read!(NonWhitespaceInput)` ====");
25 let input = read!(NonWhitespaceInput);
26 println!("You entered: \"{input}\"");
27 println!("\n==== `read!(I32Input)` ====");
28 let input = read!(I32Input);
29 println!("You entered: \"{input}\"");
30
31 println!("\n==== `read!(0. ..= 100.)` ====");
32 let input = read!(0. ..= 100.); // read a number within a range
33 println!("You entered: \"{input}\"");
34
35
36 println!("\n==== `prompt!(\"Confirm input: \"; YesNoInput)` ====");
37 let input = prompt!("Confirm input: "; YesNoInput); // read a bool
38 println!("You entered: \"{input}\"");
39
40 println!("\n==== `prompt!(\"Confirm input: \"; [true] YesNoInput)` ====");
41 let input = prompt!("Confirm input: "; [true] YesNoInput); // set a default value
42 println!("You entered: \"{input}\"");
43
44
45 println!("\n==== `read!([\"red\", \"green\", \"blue\"])` ====");
46 let (index, input) = read!(["red", "green", "blue"]); // choose from a list of options
47 println!("You entered: index {index}, \"{input}\"");
48 println!("\n==== `read!(= \"red\", \"green\", \"blue\")` ====");
49 let (index, input) = read!(= "red", "green", "blue"); // some inputs have special syntax
50 println!("You entered: index {index}, \"{input}\"");
51
52 println!("\n==== `read!([InputOption::new(...), ...])` ====");
53 let (index, input) = read!([
54 InputOption::new("1", "red", vec!("r", "the first color"), ()), // displayed as "1: red", and so on
55 InputOption::new("2", "green", vec!("g", "the second color"), ()),
56 InputOption::new("3", "blue", vec!("b", "the third color"), ()),
57 ]);
58 println!("You entered: index {index}, \"{}\"", input.main_name);
59
60 println!("\n==== `read!(= ...)` ====");
61 let (index, input) = read!(=
62 ["1", "red", "r", "the first color"], (),
63 ["2", "green", "g", "the second color"], (),
64 ["3", "blue", "b", "the third color"], (),
65 );
66 println!("You entered: index {index}, \"{}\"", input.main_name);
67
68
69 println!("\n==== `prompt!(\"Enter an even int: \"; TransformValidate(...));` ====");
70 // one-time custom logic
71 let input = prompt!("Enter an even int: "; TransformValidate (|x| {
72 // validate input as an integer
73 let Ok(x) = x.parse::<isize>() else {return Err(String::from("Could not parse input"));};
74 // validate input as even
75 if x % 2 != 0 {return Err(String::from("Input is not even."));}
76 Ok(x)
77 }));
78 println!("You entered: \"{input}\"");
79
80
81 println!("\n==== `prompt!(\"Enter an int: \"; [1usize] = 1, 2, 3, 4, 5)` ====");
82 let (index, input) = prompt!("Enter an int: "; [1usize] = 1, 2, 3, 4, 5); // combine any features
83 println!("You entered: index {index}, \"{input}\"");
84
85
86 println!();
87 prompt!("read_lines example finished, press enter to exit.");
88
89}Sourcepub fn new_without_bulletin(
display: impl Into<String>,
choose: Vec<impl Into<String>>,
data: Data,
) -> Self
pub fn new_without_bulletin( display: impl Into<String>, choose: Vec<impl Into<String>>, data: Data, ) -> Self
Initializer without bulletin string
Sourcepub fn get_display_string(&self, is_default: Option<bool>) -> String
pub fn get_display_string(&self, is_default: Option<bool>) -> String
Internal function
Auto Trait Implementations§
impl<Data> Freeze for InputOption<Data>where
Data: Freeze,
impl<Data> RefUnwindSafe for InputOption<Data>where
Data: RefUnwindSafe,
impl<Data> Send for InputOption<Data>where
Data: Send,
impl<Data> Sync for InputOption<Data>where
Data: Sync,
impl<Data> Unpin for InputOption<Data>where
Data: Unpin,
impl<Data> UnwindSafe for InputOption<Data>where
Data: UnwindSafe,
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