Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compilation issues #95

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'a> From<&'a str> for Buffer {
impl fmt::Display for Buffer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for &c in &self.data {
try!(f.write_char(c));
f.write_char(c)?;
}
Ok(())
}
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Buffer {
where W: Write
{
let string: String = self.data.iter().cloned().collect();
try!(out.write(string.as_bytes()));
out.write(string.as_bytes())?;

Ok(())
}
Expand Down
17 changes: 9 additions & 8 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub enum KeyBindings {

pub struct Context {
pub history: History,
pub completer: Option<Box<Completer>>,
pub word_divider_fn: Box<Fn(&Buffer) -> Vec<(usize, usize)>>,
pub completer: Option<Box<dyn Completer>>,
pub word_divider_fn: Box<dyn Fn(&Buffer) -> Vec<(usize, usize)>>,
pub key_bindings: KeyBindings,
}

Expand All @@ -70,7 +70,7 @@ impl Context {
pub fn read_line<P: Into<String>>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
handler: &mut EventHandler<RawTerminal<Stdout>>,
) -> io::Result<String> {
self.read_line_with_init_buffer(prompt, handler, Buffer::new())
}
Expand All @@ -88,13 +88,14 @@ impl Context {
pub fn read_line_with_init_buffer<P: Into<String>, B: Into<Buffer>>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
handler: &mut EventHandler<RawTerminal<Stdout>>,
buffer: B,
) -> io::Result<String> {
let res = {
let bindings = self.key_bindings;
let stdout = stdout().into_raw_mode().unwrap();
let ed = try!(Editor::new_with_init_buffer(stdout, prompt, self, buffer));
match self.key_bindings {
let ed = Editor::new_with_init_buffer(stdout, prompt, self, buffer)?;
match bindings {
KeyBindings::Emacs => Self::handle_keys(keymap::Emacs::new(ed), handler),
KeyBindings::Vi => Self::handle_keys(keymap::Vi::new(ed), handler),
}
Expand All @@ -106,14 +107,14 @@ impl Context {

fn handle_keys<'a, T, W: Write, M: KeyMap<'a, W, T>>(
mut keymap: M,
mut handler: &mut EventHandler<W>,
handler: &mut EventHandler<W>,
) -> io::Result<String>
where
String: From<M>,
{
let stdin = stdin();
for c in stdin.keys() {
if try!(keymap.handle_key(c.unwrap(), handler)) {
if keymap.handle_key(c.unwrap(), handler)? {
break;
}
}
Expand Down
48 changes: 24 additions & 24 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'a, W: Write> Editor<'a, W> {
ed.move_cursor_to_end_of_line()?;
}

try!(ed.display());
ed.display()?;
Ok(ed)
}

Expand Down Expand Up @@ -171,7 +171,7 @@ impl<'a, W: Write> Editor<'a, W> {
} else {
self.cursor = cur_buf!(self).num_chars();
self._display(false)?;
try!(self.out.write(b"\r\n"));
self.out.write(b"\r\n")?;
self.show_completions_hint = false;
Ok(true)
}
Expand Down Expand Up @@ -218,24 +218,24 @@ impl<'a, W: Write> Editor<'a, W> {
fn print_completion_list(&mut self, completions: &[String]) -> io::Result<()> {
use std::cmp::max;

let (w, _) = try!(termion::terminal_size());
let (w, _) = termion::terminal_size()?;

// XXX wide character support
let max_word_size = completions.iter().fold(1, |m, x| max(m, x.chars().count()));
let cols = max(1, (w as usize / (max_word_size)));
let cols = max(1, w as usize / (max_word_size));
let col_width = 2 + w as usize / cols;
let cols = max(1, w as usize / col_width);

let mut i = 0;
for com in completions {
if i == cols {
try!(write!(self.out, "\r\n"));
write!(self.out, "\r\n")?;
i = 0;
} else if i > cols {
unreachable!()
}

try!(write!(self.out, "{:<1$}", com, col_width));
write!(self.out, "{:<1$}", com, col_width)?;

i += 1;
}
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'a, W: Write> Editor<'a, W> {
Ok(())
} else if completions.len() == 1 {
self.show_completions_hint = false;
try!(self.delete_word_before_cursor(false));
self.delete_word_before_cursor(false)?;
self.insert_str_after_cursor(completions[0].as_ref())
} else {
let common_prefix = util::find_longest_common_prefix(
Expand All @@ -291,16 +291,16 @@ impl<'a, W: Write> Editor<'a, W> {
let s = p.iter().cloned().collect::<String>();

if s.len() > word.len() && s.starts_with(&word[..]) {
try!(self.delete_word_before_cursor(false));
self.delete_word_before_cursor(false)?;
return self.insert_str_after_cursor(s.as_ref());
}
}

if self.show_completions_hint {
try!(write!(self.out, "\r\n"));
try!(self.print_completion_list(&completions[..]));
try!(write!(self.out, "\r\n"));
try!(self.display());
write!(self.out, "\r\n")?;
self.print_completion_list(&completions[..])?;
write!(self.out, "\r\n")?;
self.display()?;

self.show_completions_hint = false;
} else {
Expand Down Expand Up @@ -347,7 +347,7 @@ impl<'a, W: Write> Editor<'a, W> {

/// Clears the screen then prints the prompt and current buffer.
pub fn clear(&mut self) -> io::Result<()> {
try!(write!(self.out, "{}{}", clear::All, cursor::Goto(1, 1)));
write!(self.out, "{}{}", clear::All, cursor::Goto(1, 1))?;
self.term_cursor_line = 1;
self.display()
}
Expand Down Expand Up @@ -572,7 +572,7 @@ impl<'a, W: Write> Editor<'a, W> {
if self.show_autosuggestions {
{
let autosuggestion = self.current_autosuggestion().cloned();
let mut buf = self.current_buffer_mut();
let buf = self.current_buffer_mut();
if let Some(x) = autosuggestion {
buf.insert_from_buffer(&x);
}
Expand Down Expand Up @@ -615,7 +615,7 @@ impl<'a, W: Write> Editor<'a, W> {
if cfg!(test) { (80, 24) }
// otherwise pull values from termion
else {
let (mut size_col, mut size_row) = try!(termion::terminal_size());
let (mut size_col, mut size_row) = termion::terminal_size()?;
if size_col == 0 {
size_col = 80;
size_row = 24;
Expand Down Expand Up @@ -660,14 +660,14 @@ impl<'a, W: Write> Editor<'a, W> {

// Move the term cursor to the same line as the prompt.
if self.term_cursor_line > 1 {
try!(write!(
write!(
self.out,
"{}",
cursor::Up(self.term_cursor_line as u16 - 1)
));
)?;
}
// Move the cursor to the start of the line then clear everything after. Write the prompt
try!(write!(self.out, "\r{}{}", clear::AfterCursor, self.prompt));
write!(self.out, "\r{}{}", clear::AfterCursor, self.prompt)?;

// If we have an autosuggestion, we make the autosuggestion the buffer we print out.
// We get the number of bytes in the buffer (but NOT the autosuggestion).
Expand All @@ -685,7 +685,7 @@ impl<'a, W: Write> Editor<'a, W> {

for (i, line) in lines.iter().enumerate() {
if i > 0 {
try!(write!(self.out, "{}", cursor::Right(prompt_width as u16)));
write!(self.out, "{}", cursor::Right(prompt_width as u16))?;
}

if buf_num_remaining_bytes == 0 {
Expand All @@ -711,7 +711,7 @@ impl<'a, W: Write> Editor<'a, W> {

// at the end of the line, move the cursor down a line
if new_total_width % w == 0 {
try!(write!(self.out, "\r\n"));
write!(self.out, "\r\n")?;
}

self.term_cursor_line = (new_total_width_to_cursor + w) / w;
Expand All @@ -720,7 +720,7 @@ impl<'a, W: Write> Editor<'a, W> {
// to the line where the true cursor is.
let cursor_line_diff = new_num_lines as isize - self.term_cursor_line as isize;
if cursor_line_diff > 0 {
try!(write!(self.out, "{}", cursor::Up(cursor_line_diff as u16)));
write!(self.out, "{}", cursor::Up(cursor_line_diff as u16))?;
} else if cursor_line_diff < 0 {
unreachable!();
}
Expand All @@ -730,13 +730,13 @@ impl<'a, W: Write> Editor<'a, W> {
let cursor_col_diff = new_total_width as isize - new_total_width_to_cursor as isize -
cursor_line_diff * w as isize;
if cursor_col_diff > 0 {
try!(write!(self.out, "{}", cursor::Left(cursor_col_diff as u16)));
write!(self.out, "{}", cursor::Left(cursor_col_diff as u16))?;
} else if cursor_col_diff < 0 {
try!(write!(
write!(
self.out,
"{}",
cursor::Right((-cursor_col_diff) as u16)
));
)?;
}

self.out.flush()
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::Write;
use termion::event::Key;
use Editor;

pub type EventHandler<'a, W> = FnMut(Event<W>) + 'a;
pub type EventHandler<'a, W> = dyn FnMut(Event<W>) + 'a;

pub struct Event<'a, 'out: 'a, W: Write + 'a> {
pub editor: &'a mut Editor<'out, W>,
Expand Down
18 changes: 9 additions & 9 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl History {
))
}
};
let file = try!(OpenOptions::new().read(true).open(file_name));
let file = OpenOptions::new().read(true).open(file_name)?;
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Expand Down Expand Up @@ -217,7 +217,7 @@ fn write_to_disk(max_file_size: usize, new_item: &Buffer, file_name: &str) -> io
.open(file_name)?;

// The metadata contains the length of the file
let file_length = file.metadata().ok().map_or(0u64, |m| m.len());
let _file_length = file.metadata().ok().map_or(0u64, |m| m.len());

{
// Count number of entries in file
Expand Down Expand Up @@ -277,24 +277,24 @@ fn write_to_disk(max_file_size: usize, new_item: &Buffer, file_name: &str) -> io


// Move it all back
move_file_contents_backward(&mut file, move_dist);
move_file_contents_backward(&mut file, move_dist).unwrap_or_else(|_| {});
}
};


// Seek to end for appending
try!(file.seek(SeekFrom::End(0)));
file.seek(SeekFrom::End(0))?;
// Write the command to the history file.
try!(file.write_all(String::from(new_item.clone()).as_bytes()));
try!(file.write_all(b"\n"));
file.write_all(String::from(new_item.clone()).as_bytes())?;
file.write_all(b"\n")?;
file.flush()?;

Ok(())
}

fn move_file_contents_backward(file: &mut File, distance: u64) -> io::Result<()> {
let mut total_read = 0;
let mut buffer = [0u8, 4096];
let mut buffer = [0u8; 4096];

file.seek(SeekFrom::Start(distance))?;

Expand All @@ -307,11 +307,11 @@ fn move_file_contents_backward(file: &mut File, distance: u64) -> io::Result<()>
break;
}

file.seek(SeekFrom::Current(-(read as i64 + distance as i64)));
file.seek(SeekFrom::Current(-(read as i64 + distance as i64))).unwrap_or_else(|_| {0});


file.write_all(&buffer[..read])?;
file.seek(SeekFrom::Current(distance as i64));
file.seek(SeekFrom::Current(distance as i64)).unwrap_or_else(|_| {0});
}

file.set_len(total_read)?;
Expand Down
6 changes: 3 additions & 3 deletions src/keymap/emacs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, W: Write> Emacs<'a, W> {
'k' => self.ed.delete_all_after_cursor(),
'w' => self.ed.delete_word_before_cursor(true),
'x' => {
try!(self.ed.undo());
self.ed.undo()?;
Ok(())
}
_ => Ok(()),
Expand All @@ -51,7 +51,7 @@ impl<'a, W: Write> Emacs<'a, W> {
'f' => emacs_move_word(&mut self.ed, EmacsMoveDir::Right),
'b' => emacs_move_word(&mut self.ed, EmacsMoveDir::Left),
'r' => {
try!(self.ed.revert());
self.ed.revert()?;
Ok(())
}
'.' => self.handle_last_arg_fetch(),
Expand Down Expand Up @@ -191,7 +191,7 @@ mod tests {

macro_rules! simulate_keys {
($keymap:ident, $keys:expr) => {{
simulate_keys(&mut $keymap, $keys.into_iter())
simulate_keys(&mut $keymap, $keys.iter())
}}
}

Expand Down
16 changes: 8 additions & 8 deletions src/keymap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@ pub trait KeyMap<'a, W: Write, T>: From<T> {

match key {
Key::Ctrl('c') => {
try!(self.editor_mut().handle_newline());
self.editor_mut().handle_newline()?;
return Err(io::Error::new(ErrorKind::Interrupted, "ctrl-c"));
}
// if the current buffer is empty, treat ctrl-d as eof
Key::Ctrl('d') if is_empty => {
try!(self.editor_mut().handle_newline());
self.editor_mut().handle_newline()?;
return Err(io::Error::new(ErrorKind::UnexpectedEof, "ctrl-d"));
}
Key::Char('\t') => try!(self.editor_mut().complete(handler)),
Key::Char('\t') => self.editor_mut().complete(handler)?,
Key::Char('\n') => {
done = try!(self.editor_mut().handle_newline());
done = self.editor_mut().handle_newline()?;
}
Key::Ctrl('f') if self.editor().is_currently_showing_autosuggestion() => {
try!(self.editor_mut().accept_autosuggestion());
self.editor_mut().accept_autosuggestion()?;
}
Key::Right if self.editor().is_currently_showing_autosuggestion() &&
self.editor().cursor_is_at_end_of_line() => {
try!(self.editor_mut().accept_autosuggestion());
self.editor_mut().accept_autosuggestion()?;
}
_ => {
try!(self.handle_key_core(key));
self.handle_key_core(key)?;
self.editor_mut().skip_completions_hint();
}
};

handler(Event::new(self.editor_mut(), EventKind::AfterKey(key)));

try!(self.editor_mut().flush());
self.editor_mut().flush()?;

Ok(done)
}
Expand Down
Loading