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
use crate::parse::{split, ends_with_whitespace};
use std::io::{Error, ErrorKind, Write};
use termion::cursor;
use termion::event::Key::{self, Alt, Char, Ctrl};
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::raw::RawTerminal;
use std::io::{stdin, stdout};
use crate::completion::{Command, complete, CompletionResult};

/// Config struct for building command line interfaces.
/// An example:
/// ```
/// use shli::{Prompt,Command};
/// 
/// let mut p = Prompt::new("> ".to_string(), vec!(
/// 	Command::new("print"),
/// 	Command::new("echo"),
/// 	Command::new("cat").arg("--help"),
/// 	Command::new("exit")
/// ));
/// ```
/// Now, use the `read_commandline` method to let the user type a command.
/// 
/// It will tab complete `print`, `echo`, `cat`, `cat --help` and `exit`.
pub struct Prompt {
	pub prompt_text: String,
	pub history: Vec<String>,
	pub commands: Vec<Command>,
}

impl Prompt {
	/// Creates a new Prompt instance.
	/// 
	/// `prompt_text` is the text written before the user input.
	/// `commands` is the list of available commands used by tab completion.
	pub fn new(prompt_text: String, commands: Vec<Command>) -> Prompt {
		Prompt {
			prompt_text: prompt_text,
			history: vec!(),
			commands: commands,
		}
	}

	/// Reprint the command line in the current terminal line.
	/// `right_line` refers to the command part supposed to be right from the cursor.
	fn reprint(&self,
		stdout: &mut RawTerminal<std::io::StdoutLock>,
		line: &str,
		right_line: &str,
	) -> std::io::Result<()> {
		write!(stdout, "\r{}{}{}", &self.prompt_text, line, right_line)?;
		if right_line.len() != 0 {
			write!(stdout, "{}", cursor::Left(right_line.len() as u16))?;
		}
		stdout.flush()?;
		Ok(())
	}

	fn completion(&self, stdout: &mut RawTerminal<std::io::StdoutLock>, line: &mut String, right_line: &str) -> std::io::Result<()> {
		match complete(&line, &self.commands) {
			CompletionResult::None => {}
			CompletionResult::Description(description) => {
				write!(stdout, "\n\r Parameter help: {}\n\r> {}", description, line)?;
			}
			CompletionResult::PossibilityList(possible_words) => {
				if possible_words.len() == 1 {
					// First, replace the last word
					let mut words = split(&line);
					if ! ends_with_whitespace(&line) {
						words.pop();
					}
					words.push(possible_words[0].clone());
					// Now build up the cmdline again
					*line = String::new();
					for word in words {
						line.push_str(&word);
						line.push(' ');
					}
					// Now display the new cmdline
					self.reprint(stdout, &line, right_line)?;
				} else if possible_words.len() > 1 {
					// Display the possibilities
					write!(
						stdout,
						"\n\r Completions: {:?}\n\r> {}",
						possible_words, line
					)?;
					stdout.flush()?;
				}
			}
		};
		Ok(())
	}

	/// Prompt for a single command line.
	///
	/// This function reads and returns a command line.
	/// Line editing with backspace and ALT+backspace is supported.
	///
	/// If TAB is pressed by the user, the callback function `completion` is asked
	/// for possible argument completion. If it returns exactly 1 completion, it
	/// is used, if it returns more, they are displayed.
	///
	/// If Ctrl+C is pressed, this function returns `Err(Error::new(ErrorKind::Other, "Ctrl-C pressed.")`,
	/// while an EOF of `stdin` or Ctrl+D will return the error type `ErrorKind::UnexpectedEof`.
	///
	/// On success, `read_commandline` returns a Vector of command line components (command + arguments).
	///
	/// For example, the following input:
	/// ```text
	/// > print out "example command"
	/// ```
	/// will return `vec!["print", "out", "example command"]`.
	pub fn read_commandline(&mut self) -> std::io::Result<Vec<String>> {
		let stdout = stdout();
		let mut stdout = stdout.lock().into_raw_mode()?;
		let stdin = stdin();
		let stdin = stdin.lock();
		write!(stdout, "{}", &self.prompt_text)?;
		stdout.flush()?;
		let mut line = String::new();
		let mut right_line = String::new();
		let mut history_offset = 0;
	
		for key in stdin.keys() {
			match key {
				Ok(Char('\n')) => break,
				Ok(Char('\t')) => {
					// The tabulator was pressed.
					self.completion(&mut stdout, &mut line, &right_line)?
				}
				Ok(Char(ch)) => {
					line.push(ch);
					self.reprint(&mut stdout, &line, &right_line)?
				}
				Ok(Key::Left) => {
					if let Some(ch) = line.pop() {
						right_line = format!("{}{}", ch, right_line);
						write!(stdout, "{}", cursor::Left(1))?;
						stdout.flush()?
					}
				}
				Ok(Key::Right) => {
					if right_line.len() > 0 {
						line.push(right_line.remove(0));
						write!(stdout, "{}", cursor::Right(1))?;
						stdout.flush()?
					}
				}
				Ok(Key::Up) => {
					if history_offset < self.history.len() {
						history_offset += 1;
						if let Some(new_cmd_line) = self.history.get(self.history.len() - history_offset) {
							let chars_to_wipe = self.prompt_text.len() + line.len() + right_line.len();
							line = new_cmd_line.clone();
							write!(stdout, "\r")?;
							for _ in 0..chars_to_wipe {
								write!(stdout, " ")?;
							}
							right_line = String::new();
							self.reprint(&mut stdout, &line, &right_line)?
						}
					}
				}
				Ok(Ctrl('c')) => {
					return Err(Error::new(ErrorKind::Other, "Ctrl-C pressed."));
				}
				Ok(Ctrl('d')) => {
					return Err(Error::new(ErrorKind::UnexpectedEof, ""));
				}
				Ok(Key::Backspace) => {
					if line.pop().is_some() {
						write!(stdout, "{} {}", cursor::Left(1), cursor::Left(1))?;
						stdout.flush()?;
					}
				}
				Ok(Alt('\u{7f}')) => {
					// ALT+← was pressed.
					// Remove the last word.
					let mut words = split(&line);
					if let Some(_) = words.pop() {
						let old_len = line.len();
						// Build up the cmdline again
						line = String::new();
						for word in words {
							line.push_str(&word);
							line.push(' ');
						}
						// Wipe removed characters
						if line.len() < old_len {
							write!(
								stdout,
								"{}{}",
								cursor::Left((old_len - line.len()) as u16),
								" ".repeat(old_len)
							)?;
						}
						// Now display the new cmdline
						self.reprint(&mut stdout, &line, &right_line)?;
					}
				}
				Ok(_) => {}
				Err(e) => return Err(e),
			}
		}
		line.push_str(&right_line);
		self.history.push(line.clone());
		Ok(split(&line))
	}
	
}