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
use core::prelude::*;
use alloc::boxed::Box;

use collections::Vec;
use collections::String;
use collections::string::ToString;
use collections::string::FromUtf8Error;
use core::mem;
use core::array::FixedSizeArray;

use cli::*;
use utils::*;

enum AutocompleteRequest {	
	None,
	HaveMultipleOptions { matches: AutocompleteResult }
}

/// Holds the current line buffer for a terminal and its possible autocomplete state.
pub struct CliPromptAutocompleteBuffer {
	line_buffer: Vec<u8>,
	prompt: String,
	newline: String,
	autocomplete: AutocompleteRequest,
	max_buffer_length: usize
}

pub trait CliPromptTerminal {
	fn print_bytes(&self, bytes: &[u8]);
	fn print(&self, str: &str) {
		self.print_bytes(str.bytes().collect::<Vec<u8>>().as_slice());
	}
}

impl CliPromptAutocompleteBuffer {
	pub fn new(prompt: String) -> CliPromptAutocompleteBuffer {
		CliPromptAutocompleteBuffer {
			line_buffer: Vec::new(),
			prompt: prompt, 
			newline: "\r\n".to_string(),
			autocomplete: AutocompleteRequest::None,
			max_buffer_length: 512
		}
	}

	pub fn print_prompt<T>(&self, output: &T) where T: CliPromptTerminal {
		if !self.prompt.len() == 0 { return; }

		output.print(self.prompt.as_str());
	}

	pub fn handle_received_byte<T>(&mut self, byte: u8, output: &T, cmds: &mut [Box<CliCommand + 'static>], cli_terminal: &mut CliTerminal)
		where T: CliPromptTerminal
	{
		let mut handled_autocomplete = false;

		match byte {
			// tab \t

			0x09 => {
				match self.autocomplete {					
					AutocompleteRequest::None => {
						// try to resolve the options

						let str = String::from_utf8(self.line_buffer.clone());
						if !str.is_err() {
							let autocomplete = cli_try_autocomplete(str.unwrap().as_str(), cmds);

							match autocomplete {
								AutocompleteResult::None => {},
								AutocompleteResult::SingleMatch { line: ref line } => {
									// immediately send the new stuff

									let ref additional_part = line.additional_part;
									output.print_bytes(additional_part.bytes().collect::<Vec<u8>>().as_slice());

									// replace our line buffer with the stuff from autocomplete, to be consistent with future invokations

									self.line_buffer.clear();
									for c in line.full_new_line.bytes() {
										self.line_buffer.push(c);
									}

									// we're done with this one

									self.autocomplete = AutocompleteRequest::None;
								},
								AutocompleteResult::MultipleMatches { lines: ref lines } => {
									// this was the first time tab was pressed, and there are multiple options. store them,

									// when the user presses tab again, print them out

									// we could also bleep at this point...


									self.autocomplete = AutocompleteRequest::HaveMultipleOptions {
										matches: autocomplete.clone()
									};
								}
							}
						}

						handled_autocomplete = true;
					},
					AutocompleteRequest::HaveMultipleOptions { matches: ref matches } => {
						if let &AutocompleteResult::MultipleMatches { lines: ref lines } = matches {
							// print the available autocomplete options

							let suggestions = lines.iter().map(|l| { l.full_new_line.as_str() }).collect::<Vec<&str>>();							
							output.print(format_in_columns(suggestions.as_slice(), 80, 4, "\r\n").as_str());

							self.print_prompt(output);

							// restore the current buffer

							output.print_bytes(self.line_buffer.as_slice());

							handled_autocomplete = false;
						};
					}
				}
			}
			
			// carriage return, \r

			0x0d  => {
				output.print(self.newline.as_str());

				// new line

				let str = String::from_utf8(self.line_buffer.clone());
				if str.is_err() {
					output.print("String parse error.");
				} else {
					cli_execute(str.unwrap().as_str(), cmds, cli_terminal);
				}

				self.line_buffer.clear();
				self.print_prompt(output);
			}

			// backspace

			0x7f => {
				// reply to it only if our line buffer contains something

				// otherwise it would overwrite the prompt


				if let Some(..) = self.line_buffer.pop() {					
					output.print_bytes(&[0x7f].as_slice());
				}
			}

			// anything else

			_ => {
				self.line_buffer.push(byte);
				if self.line_buffer.len() >= self.max_buffer_length {
					// system message?

					output.print(self.newline.as_str());
					output.print("Line buffer overflow.");
					output.print(self.newline.as_str());

					self.line_buffer.clear();
					self.print_prompt(output);
				}
				output.print_bytes(&[byte].as_slice());
			}
		}

		if (handled_autocomplete == false) {
			// reset autocomplete state

			self.autocomplete = AutocompleteRequest::None;
		}
	}
}