Commit be9fb423 authored by Sehny's avatar Sehny
Browse files

Use Ctrl+f to accept autosuggestions.

parent f8f4eca6
......@@ -213,6 +213,11 @@ impl Buffer {
self.push_action(act);
}
pub fn insert_from_buffer(&mut self, other: &Buffer) {
let start = self.data.len();
self.insert(start, &other.data[start..])
}
pub fn range(&self, start: usize, end: usize) -> String {
self.data[start..end].iter().cloned().collect()
}
......
use std::cmp;
use std::io::{self, Write};
use termion::{self, clear, cursor};
use termion::{self, clear, cursor, color};
use unicode_width::*;
use Context;
......@@ -527,6 +527,20 @@ impl<'a, W: Write> Editor<'a, W> {
cur_buf_mut!(self)
}
/// Accept autosuggestion and copy its content into current buffer
pub fn accept_autosuggestion(&mut self) -> io::Result<()> {
{
let hist_match = self.context.history
.get_first_match(self.cur_history_loc, self.current_buffer())
.cloned();
let mut buf = self.current_buffer_mut();
if let Some(hm) = hist_match {
buf.insert_from_buffer(&hm);
}
}
self.print_current_buffer(false)
}
/// Deletes the displayed prompt and buffer, replacing them with the current prompt and buffer
pub fn print_current_buffer(&mut self, move_cursor_to_end_of_line: bool) -> io::Result<()> {
let buf = cur_buf!(self);
......@@ -549,10 +563,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, "->"));
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));
}
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,7 +59,7 @@ impl History {
ret
}
pub fn get_first_match(&self, curr_position: Option<usize>, new_buff: &Buffer) -> Option<&Buffer> {
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() {
if let Some(tested) = self.buffers.get(iter) {
......
......@@ -18,6 +18,9 @@ pub trait KeyMap<'a, W: Write, T>: From<T> {
try!(self.editor().handle_newline());
done = true;
}
Key::Ctrl('f') => {
try!(self.editor().accept_autosuggestion());
}
_ => {
try!(self.handle_key_core(key));
self.editor().skip_completions_hint();
......
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