1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// started      24/03/05
// last updated 24/03/05



pub mod input_options;
pub mod ranges;



use std::{error::Error, io::Write};

pub type BoxResult<T> = Result<T, Box<dyn Error>>;



/// reads a line of text, a number, etc
#[macro_export]
macro_rules! read {
	($($args:tt)*) => {
		smart_read::try_read!($($args)*).unwrap()
	}
}

/// same as read!(), but returns a result
#[macro_export]
macro_rules! try_read {
	() => {
		smart_read::read_string()
	};
	($input:expr) => {{
		use smart_read::ReadLine;
		$input.try_read_line()
	}};
	(= $($input:expr),*) => {{
		use smart_read::ReadLine;
		let mut choices = vec!();
		$(choices.push($input);)*
		choices.try_read_line()
	}};
}



/// prompts a line of text, a number, etc
#[macro_export]
macro_rules! prompt {
	($($args:tt)*) => {
		smart_read::try_prompt!($($args)*).unwrap()
	}
}

/// same as prompt!(), but returns a result
#[macro_export]
macro_rules! try_prompt {
	($prompt:expr) => {{
		print!("{}", $prompt);
		smart_read::read_string()
	}};
	($prompt:expr; $input:expr) => {{
		use smart_read::ReadLine;
		println!("{}", $prompt);
		$input.try_read_line()
	}};
	($prompt:expr; = $($input:expr),*) => {{
		use smart_read::ReadLine;
		let mut choices = vec!();
		$(choices.push($input);)*
		println!("{}", $prompt);
		choices.try_read_line()
	}};
}



/// This is what powers the whole crate. Anything that implements this can be passed to `read!()` and `try_read!()`
pub trait ReadLine {
	type Output;
	fn try_read_line(&self) -> BoxResult<Self::Output>;
	fn read_line(&self) -> Self::Output {
		self.try_read_line().unwrap()
	}
}





/// small utility function, mostly for internal use
pub fn read_string() -> BoxResult<String> {
	
	let mut output = String::new();
	std::io::stdout().flush()?;
	std::io::stdin().read_line(&mut output)?;
	
	if output.as_bytes().last() == Some(&10) {output.pop();} // pop \n
	if output.as_bytes().last() == Some(&13) {output.pop();} // pop \r
	
	Ok(output)
}