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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::*;
use std::collections::{LinkedList, VecDeque};



fn read_list<Data>(input_options: &[InputOption<Data>], prompt: Option<String>, default: Option<usize>) -> BoxResult<usize> {
	if input_options.is_empty() {return Err(Box::new(ListConstraintError::EmptyList));}
	
	// get prompt data
	let prompt = prompt.unwrap_or(String::from("Enter one of the following:"));
	let display_strings =
		input_options.iter().enumerate()
		.map(|(i, option)| {
			option.get_display_string(default.map(|default| i == default))
		})
		.collect::<Vec<_>>();
	
	// lists for string to match against, which option that string goes with, and whether that string is an alt_name
	let (mut all_choose_strings, mut choose_name_mappings, mut choose_name_hidden_flags) = (vec!(), vec!(), vec!());
	for (i, option) in input_options.iter().enumerate() {
		if let Some(bulletin_string) = option.bulletin_string.as_deref() {
			all_choose_strings.push(bulletin_string);
			choose_name_mappings.push(i);
			choose_name_hidden_flags.push(false);
		}
		all_choose_strings.push(&*option.main_name);
		choose_name_mappings.push(i);
		choose_name_hidden_flags.push(false);
		for alt_name in &option.alt_names {
			all_choose_strings.push(alt_name);
			choose_name_mappings.push(i);
			choose_name_hidden_flags.push(true);
		}
	}
	
	// misc work
	let print_prompt = || {
		println!("{prompt}");
		for option in display_strings.iter() {
			println!("{option}");
		}
		println!();
	};
	
	if input_options.len() == 1 {
		print_prompt();
		println!();
		println!("Automatically choosing the first option because it is the only option");
		return Ok(0);
	}
	
	print_prompt();
	let mut input = read_stdin()?;
	
	// read input
	loop {
		if input.is_empty() && let Some(default) = default {
			return Ok(default);
		}
		
		// find exact match
		for (i, option) in all_choose_strings.iter().enumerate() {
			if option.eq_ignore_ascii_case(&input) {
				let chosen_index = choose_name_mappings[i];
				return Ok(chosen_index);
			}
		}
		
		println!();
		println!("Invalid option.");
		
		// try fuzzy match
		if let Some(possible_choose_string_index) = custom_fuzzy_search(&input, &all_choose_strings) {
			let possible_option_index = choose_name_mappings[possible_choose_string_index];
			let possible_option = &input_options[possible_option_index];
			if choose_name_hidden_flags[possible_choose_string_index] {
				print!("Did you mean to type \"{}\", for option \"{}\"? (enter nothing to confirm, or re-enter input) ", all_choose_strings[possible_choose_string_index], possible_option.main_name);
			} else {
				print!("Did you mean \"{}\"? (enter nothing to confirm, or re-enter input) ", all_choose_strings[possible_choose_string_index]);
			}
			let new_input = read_stdin()?;
			if new_input.is_empty() {
				let chosen_index = possible_option_index;
				return Ok(chosen_index);
			}
			input = new_input;
		} else {
			print!("Invalid option, please re-enter input: ");
			input = read_stdin()?;
		}
		
	}
}



impl<'a, Data> TryRead for &'a [InputOption<Data>] {
	type Output = (usize, &'a InputOption<Data>);
	type Default = usize;
	fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let chosen_index = read_list(self, prompt, default)?;
		Ok((chosen_index, &self[chosen_index]))
	}
}

impl<Data, const LEN: usize> TryRead for [InputOption<Data>; LEN] {
	type Output = (usize, InputOption<Data>);
	type Default = usize;
	fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let chosen_index = read_list(&self, prompt, default)?;
		Ok((chosen_index, self.into_iter().nth(chosen_index).expect("chosen index is out of bounds")))
	}
}



/// Error type
#[derive(Debug)]
pub enum ListConstraintError {
	/// This exists because an empty list would be a softlock
	EmptyList,
}

impl Error for ListConstraintError {}

impl Display for ListConstraintError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::EmptyList => write!(f, "List Constraint is empty"),
		}
	}
}



/// Custom implementation of fuzzy search, returns the index of the closest match
pub fn custom_fuzzy_search(pattern: &str, items: &[&str]) -> Option<usize> {
	let (mut best_score, mut best_index) = (custom_fuzzy_match(pattern, items[0]), 0);
	for (i, item) in items.iter().enumerate().skip(1) {
		let score = custom_fuzzy_match(pattern, item);
		if score > best_score {
			best_score = score;
			best_index = i;
		}
	}
	if best_score > 0.0 {
		Some(best_index)
	} else {
		None
	}
}

/// Custom implementation of fuzzy match. Not efficient at all, but gives good results
pub fn custom_fuzzy_match(pattern: &str, item: &str) -> f32 {
	let mut best_score = 0.0f32;
	let offset_start = pattern.len() as isize * -1 + 1;
	let offset_end = item.len() as isize - 1;
	for offset in offset_start..=offset_end {
		let item_slice = &item[offset.max(0) as usize .. (offset + pattern.len() as isize).min(item.len() as isize) as usize];
		let pattern_slice = &pattern[(offset * -1).max(0) as usize .. (item.len() as isize - offset).min(pattern.len() as isize) as usize];
		let mut slice_score = 0.0f32;
		for (item_char, pattern_char) in item_slice.chars().zip(pattern_slice.chars()) {
			if item_char.eq_ignore_ascii_case(&pattern_char) {
				slice_score += 3.;
			} else {
				slice_score -= 1.;
			}
		}
		slice_score *= 1. - offset as f32 / item.len() as f32 * 0.5; // give higher value to earlier matches, best weight is at offset = 0
		best_score = best_score.max(slice_score);
	}
	best_score
}





/// 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);
/// ```
pub struct InputOption<Data> {
	/// This is what's displayed before the colon
	pub bulletin_string: Option<String>,
	/// This is what's shown as the option name
	pub main_name: String,
	/// These are alternate valid strings that the user could enter to choose this option
	pub alt_names: Vec<String>,
	/// Extra data for storing whatever you want
	pub data: Data,
}

impl<Data> InputOption<Data> {
	/// Basic initializer
	pub fn new(bulletin: impl Into<String>, display: impl Into<String>, choose: Vec<impl Into<String>>, data: Data) -> Self {
		Self {
			bulletin_string: Some(bulletin.into()),
			main_name: display.into(),
			alt_names: choose.into_iter().map(|v| v.into()).collect(),
			data,
		}
	}
	/// Initializer without bulletin string
	pub fn new_without_bulletin(display: impl Into<String>, choose: Vec<impl Into<String>>, data: Data) -> Self {
		Self {
			bulletin_string: None,
			main_name: display.into(),
			alt_names: choose.into_iter().map(|v| v.into()).collect(),
			data,
		}
	}
	/// Internal function
	pub fn get_display_string(&self, is_default: Option<bool>) -> String {
		match (self.bulletin_string.as_deref(), is_default) {
			(Some(bulletin_string), Some(true )) => format!("[{bulletin_string}]: {}", self.main_name),
			(Some(bulletin_string), Some(false)) => format!(" {bulletin_string}:  {}", self.main_name),
			(None                       , Some(true )) => format!("[{}]", self.main_name),
			(None                       , Some(false)) => format!(" {} ", self.main_name),
			(Some(bulletin_string), None       ) => format!("{bulletin_string}: {}", self.main_name),
			(None                       , None       ) => format!("{}", self.main_name),
		}
	}
}





impl<'a, T: Display> TryRead for &'a [T] {
	type Output = (usize, &'a T);
	type Default = usize;
	fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let options = self.iter().enumerate()
			.map(|(i, option)| {
				InputOption {
					bulletin_string: Some((i + 1).to_string()),
					main_name: option.to_string(),
					alt_names: vec!(),
					data: (),
				}
			})
			.collect::<Vec<_>>();
		let chosen_index = (&*options).try_read_line(prompt, default)?.0;
		Ok((chosen_index, &self[chosen_index]))
	}
}

impl<T: Display, const LEN: usize> TryRead for [T; LEN] {
	type Output = (usize, T);
	type Default = usize;
	fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let options = self.iter().enumerate()
			.map(|(i, option)| {
				InputOption {
					bulletin_string: Some((i + 1).to_string()),
					main_name: option.to_string(),
					alt_names: vec!(),
					data: (),
				}
			})
			.collect::<Vec<_>>();
		let chosen_index = (&*options).try_read_line(prompt, default)?.0;
		Ok((chosen_index, self.into_iter().nth(chosen_index).expect("chosen index is out of bounds")))
	}
}

impl<T: Display> TryRead for Vec<T> {
	type Output = (usize, T);
	type Default = usize;
	fn try_read_line(mut self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let options = self.iter().enumerate()
			.map(|(i, option)| {
				InputOption {
					bulletin_string: Some((i + 1).to_string()),
					main_name: option.to_string(),
					alt_names: vec!(),
					data: (),
				}
			})
			.collect::<Vec<_>>();
		let chosen_index = (&*options).try_read_line(prompt, default)?.0;
		Ok((chosen_index, self.swap_remove(chosen_index)))
	}
}

impl<T: Display> TryRead for VecDeque<T> {
	type Output = (usize, T);
	type Default = usize;
	fn try_read_line(mut self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let options = self.iter().enumerate()
			.map(|(i, option)| {
				InputOption {
					bulletin_string: Some((i + 1).to_string()),
					main_name: option.to_string(),
					alt_names: vec!(),
					data: (),
				}
			})
			.collect::<Vec<_>>();
		let chosen_index = (&*options).try_read_line(prompt, default)?.0;
		Ok((chosen_index, self.swap_remove_back(chosen_index).expect("chosen index is out of bounds")))
	}
}

impl<T: Display> TryRead for LinkedList<T> {
	type Output = (usize, T);
	type Default = usize;
	fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> BoxResult<Self::Output> {
		let options = self.iter().enumerate()
			.map(|(i, option)| {
				InputOption {
					bulletin_string: Some((i + 1).to_string()),
					main_name: option.to_string(),
					alt_names: vec!(),
					data: (),
				}
			})
			.collect::<Vec<_>>();
		let chosen_index = (&*options).try_read_line(prompt, default)?.0;
		Ok((chosen_index, self.into_iter().nth(chosen_index).expect("chosen index is out of bounds")))
	}
}