Struct InputOption

Source
pub struct InputOption<Data> {
    pub bulletin_string: Option<String>,
    pub names: Vec<String>,
    pub extra_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 = 0;
let choosable_colors =
	colors.iter().enumerate()
	.filter_map(|(i, color)| {
		let first_char = color.chars().next()?;
		if first_char.is_lowercase() {return None;}
		option_number += 1;
		Some(InputOption::new(option_number, &[*color], i))
	})
	.collect::<Vec<_>>();
 
// prompt
println!("List of colors: {colors:?}");
let (_option_index, InputOption {extra_data: index_to_remove, ..}) = prompt!("Choose a color to remove: "; choosable_colors);
colors.remove(*index_to_remove);
println!("New list of colors: {colors:?}");

Fields§

§bulletin_string: Option<String>

This is what’s displayed before the colon

§names: Vec<String>

The first value is shown as the option’s name, and all following values are alternative strings that can be used to select this option

§extra_data: Data

Extra data for storing whatever you want

Implementations§

Source§

impl<Data> InputOption<Data>

Source

pub fn new<T: ToString>( bulletin: impl ToString, names: &[T], 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 types 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"  , "r", "choose first" ], ()), // displayed as "1: red", can be chosen with "1", "red", "r", or "choose first"
55		InputOption::new("2", &["green", "g", "choose second"], ()),
56		InputOption::new("3", &["blue" , "b", "choose third" ], ()),
57	]);
58	println!("You entered: index {index}, \"{}\"", input.names[0]);
59	
60	println!("\n==== `read!(= ...)` ====");
61	let (index, input) = read!(=
62		"1"; "red"  ; ["r", "choose first" ]; (), // displayed as "1: red", can be chosen with "1", "red", "r", or "choose first"
63		"2"; "green"; ["g", "choose second"]; (),
64		"3"; "blue" ; ["b", "choose third" ]; (),
65	);
66	println!("You entered: index {index}, \"{}\"", input.names[0]);
67	
68	println!("\n==== `prompt!(\"Enter an even int: \"; TransformValidate(...));` ====");
69	// one-time custom logic
70	let input = prompt!("Enter an even int: "; TransformValidate (|x| {
71		// validate input as an integer
72		let Ok(x) = x.parse::<isize>() else {return Err(String::from("Could not parse input"));};
73		// validate input as even
74		if x % 2 != 0 {return Err(String::from("Input is not even."));}
75		Ok(x)
76	}));
77	println!("You entered: \"{input}\"");
78	
79	
80	println!("\n==== `prompt!(\"Enter an int: \"; [2usize] = \"a\", \"b\", \"c\")` ====");
81	let (index, input) = prompt!("Enter an int: "; [2usize] = "a", "b", "c"); // combine any features
82	println!("You entered: index {index}, \"{input}\"");
83	
84	
85	println!();
86	println!("read_lines example finished");
87	wait_for_enter();
88	
89}
Source

pub fn new_without_bulletin<T: ToString>(names: &[T], data: Data) -> Self

Initializer without bulletin string

Source

pub fn get_display_string(&self, is_default: Option<bool>) -> String

Internal function

Source

pub fn get_name(&self) -> &str

Gets the name of the option from the start of self.names

It is assumed that there is at least one value in names, but if not, it returns "[unnamed]"

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