Commit f8f4eca6 authored by Sehny's avatar Sehny
Browse files

Initial work on autosuggestions (methods for history and buffer)

parent a81a6386
......@@ -243,6 +243,16 @@ impl Buffer {
Ok(())
}
pub fn print_rest<W>(&self, out: &mut W, other: &Buffer) -> io::Result<()>
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()));
Ok(())
}
fn remove_raw(&mut self, start: usize, end: usize) -> Vec<char> {
self.data.drain(start..end).collect()
}
......@@ -252,6 +262,20 @@ impl Buffer {
self.data.insert(start + i, c)
}
}
pub fn is_match(&self, other: &Buffer) -> bool {
if other.data.len() != 0 {
let match_let = self.data
.iter()
.zip(&other.data)
.take_while(|&(s, o)| *s == *o)
.collect::<Vec<_>>()
.len();
match_let == other.data.len()
} else {
false
}
}
}
#[cfg(test)]
......
......@@ -549,6 +549,11 @@ 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, "->"));
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));
}
if new_prompt_and_buffer_width % w == 0 {
// at the end of the line, move the cursor down a line
try!(write!(self.out, "\r\n"));
......
......@@ -59,6 +59,18 @@ impl History {
ret
}
pub fn get_first_match(&self, curr_position: Option<usize>, new_buff: &Buffer) -> Option<&Buffer> {
let pos = curr_position.unwrap_or(self.buffers.len());
for iter in (0..pos).rev() {
if let Some(tested) = self.buffers.get(iter) {
if tested.is_match(new_buff) {
return self.buffers.get(iter)
}
}
}
None
}
/// Get the history file name.
pub fn file_name(&self) -> Option<&str> {
match self.file_name {
......
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