Commit 9e26f938 authored by Sehny's avatar Sehny
Browse files

Move cursor to appropriate position. Introduce switch to turn off autosuggestions.

parent be9fb423
......@@ -248,14 +248,17 @@ impl Buffer {
Ok(())
}
pub fn print_rest<W>(&self, out: &mut W, other: &Buffer) -> io::Result<()>
/// Takes other buffer, measures its length and prints this buffer from the point where
/// the other stopped.
/// Used to implement autosuggestions.
pub fn print_rest<W>(&self, out: &mut W, other: &Buffer) -> io::Result<usize>
where W: Write
{
let matching_part = other.data.len();
let string: String = self.data.iter().skip(matching_part).cloned().collect();
try!(out.write(string.as_bytes()));
out.write(string.as_bytes())?;
Ok(())
Ok(string.len())
}
fn remove_raw(&mut self, start: usize, end: usize) -> Vec<char> {
......@@ -268,15 +271,19 @@ impl Buffer {
}
}
/// Check if the other buffer starts with the same content as this one.
/// Used to implement autosuggestions.
pub fn is_match(&self, other: &Buffer) -> bool {
if other.data.len() != 0 {
let other_len = other.data.len();
let self_len = self.data.len();
if other.data.len() != 0 && self_len != other_len {
let match_let = self.data
.iter()
.zip(&other.data)
.take_while(|&(s, o)| *s == *o)
.collect::<Vec<_>>()
.len();
match_let == other.data.len()
match_let == other_len
} else {
false
}
......
......@@ -82,6 +82,9 @@ pub struct Editor<'a, W: Write> {
// If this is true, on the next tab we print the completion list.
show_completions_hint: bool,
// Show autosuggestions based on history
show_autosuggestions: bool,
// if set, the cursor will not be allow to move one past the end of the line, this is necessary
// for Vi's normal mode.
pub no_eol: bool,
......@@ -126,6 +129,7 @@ impl<'a, W: Write> Editor<'a, W> {
cur_history_loc: None,
context: context,
show_completions_hint: false,
show_autosuggestions: true,
prompt_width: prompt_width,
term_cursor_line: 1,
no_eol: false,
......@@ -538,7 +542,7 @@ impl<'a, W: Write> Editor<'a, W> {
buf.insert_from_buffer(&hm);
}
}
self.print_current_buffer(false)
self.print_current_buffer(true)
}
/// Deletes the displayed prompt and buffer, replacing them with the current prompt and buffer
......@@ -563,11 +567,16 @@ impl<'a, W: Write> Editor<'a, W> {
try!(write!(self.out, "\r{}{}", clear::AfterCursor, self.prompt));
try!(buf.print(&mut self.out));
try!(write!(self.out, "{}", color::Fg(color::Yellow)));
if let Some(hist_match) = self.context.history.get_first_match(self.cur_history_loc, buf) {
try!(hist_match.print_rest(&mut self.out, buf));
// Display autosuggestion
if self.show_autosuggestions {
if let Some(hist_match) = self.context.history.get_first_match(self.cur_history_loc, buf) {
write!(self.out, "{}", color::Fg(color::Yellow))?;
let len = hist_match.print_rest(&mut self.out, buf)?;
write!(self.out, "{}", color::Fg(color::Reset))?;
write!(self.out, "{}", cursor::Left(len as u16))?;
}
}
try!(write!(self.out, "{}", color::Fg(color::Reset)));
if new_prompt_and_buffer_width % w == 0 {
// at the end of the line, move the cursor down a line
......
......@@ -59,6 +59,8 @@ impl History {
ret
}
/// Go through the history and try to find a buffer which starts the same as the new buffer
/// given to this function as argument.
pub fn get_first_match<'a, 'b>(&'a self, curr_position: Option<usize>, new_buff: &'b Buffer) -> Option<&'a Buffer> {
let pos = curr_position.unwrap_or(self.buffers.len());
for iter in (0..pos).rev() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment