read_lines/
read_lines.rs

1use smart_read::prelude::*;
2
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}