Commit 3d9cf95e authored by AdminXVII's avatar AdminXVII

Move io errors up the error chain

parent 31634f14
Pipeline #4952 passed with stage
in 2 minutes and 1 second
use std::cmp;
use std::fmt::Write;
use std::fmt::{self, Write};
use std::io;
use termion::{self, clear, color, cursor};
......@@ -242,7 +242,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
} else {
self.cursor = cur_buf!(self).num_chars();
self.no_newline = true;
self._display(false)?;
let _ = self._display(false);
self.out.write_all(b"\r\n")?;
self.show_completions_hint = None;
Ok(true)
......@@ -363,14 +363,13 @@ impl<'a, W: io::Write> Editor<'a, W> {
}
fn print_completion_list(
w: usize,
completions: &[String],
highlighted: Option<usize>,
output_buf: &mut String,
) -> io::Result<usize> {
) -> usize {
use std::cmp::max;
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));
......@@ -379,37 +378,23 @@ impl<'a, W: io::Write> Editor<'a, W> {
let lines = completions.len() / cols;
let mut i = 0;
for (index, com) in completions.iter().enumerate() {
if i == cols {
if index % cols == 0 {
output_buf.push_str("\r\n");
i = 0;
} else if i > cols {
unreachable!()
}
if Some(index) == highlighted {
write!(
output_buf,
"{}{}",
color::Black.fg_str(),
color::White.bg_str()
);
output_buf.push_str(color::Black.fg_str());
output_buf.push_str(color::White.bg_str());
}
write!(output_buf, "{:<1$}", com, col_width);
if Some(index) == highlighted {
write!(
output_buf,
"{}{}",
color::Reset.bg_str(),
color::Reset.fg_str()
);
output_buf.push_str(color::Reset.fg_str());
output_buf.push_str(color::Reset.bg_str());
}
i += 1;
}
Ok(lines)
lines
}
pub fn skip_completions_hint(&mut self) {
......@@ -847,7 +832,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
}
}
fn _display(&mut self, show_autosuggest: bool) -> io::Result<()> {
fn _display(&mut self, show_autosuggest: bool) -> fmt::Result {
fn calc_width(prompt_width: usize, buf_widths: &[usize], terminal_width: usize) -> usize {
let mut total = 0;
......@@ -862,9 +847,10 @@ impl<'a, W: io::Write> Editor<'a, W> {
total
}
let terminal_width = util::terminal_width().unwrap_or(80);
let (prompt, rev_prompt_width) = self.search_prompt();
let terminal_width = util::terminal_width()?;
let prompt_width = util::last_prompt_line_width(&prompt);
let buf = cur_buf!(self);
......@@ -912,7 +898,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
&mut self.context.buf,
"{}",
cursor::Up(self.term_cursor_line as u16 - 1)
);
)?;
}
if !self.no_newline {
......@@ -921,21 +907,25 @@ impl<'a, W: io::Write> Editor<'a, W> {
self.context.buf.push(' '); // if the line is not empty, owerflow on next line
}
}
write!(&mut self.context.buf, "\r{}", clear::AfterCursor);
write!(&mut self.context.buf, "\r{}", clear::AfterCursor)?;
// If we're cycling through completions, show those
let mut completion_lines = 0;
if let Some((completions, i)) = self.show_completions_hint.as_ref() {
completion_lines =
1 + Self::print_completion_list(completions, *i, &mut self.context.buf)?;
completion_lines = 1 + Self::print_completion_list(
terminal_width,
completions,
*i,
&mut self.context.buf,
);
self.context.buf.push_str("\r\n");
}
// Write the prompt
if !self.no_newline {
write!(&mut self.context.buf, "{}", prompt.split('\n').join("\r\n"));
write!(&mut self.context.buf, "{}", prompt.split('\n').join("\r\n"))?;
} else {
write!(&mut self.context.buf, "{}", util::handle_prompt(&prompt));
write!(&mut self.context.buf, "{}", util::handle_prompt(&prompt))?;
}
// If we have an autosuggestion, we make the autosuggestion the buffer we print out.
......@@ -955,7 +945,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
&mut self.context.buf,
"{}",
cursor::Right(prompt_width as u16)
);
)?;
}
if buf_num_remaining_bytes == 0 {
......@@ -967,11 +957,11 @@ impl<'a, W: io::Write> Editor<'a, W> {
None => start.to_owned(),
};
if self.is_search() {
write!(&mut self.context.buf, "{}", color::Yellow.fg_str());
write!(&mut self.context.buf, "{}", color::Yellow.fg_str())?;
}
write!(&mut self.context.buf, "{}", start);
self.context.buf.push_str(&start);
if !self.is_search() {
write!(&mut self.context.buf, "{}", color::Yellow.fg_str());
write!(&mut self.context.buf, "{}", color::Yellow.fg_str())?;
}
self.context.buf.push_str(&line[buf_num_remaining_bytes..]);
buf_num_remaining_bytes = 0;
......@@ -982,7 +972,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
None => line,
};
if self.is_search() {
write!(&mut self.context.buf, "{}", color::Yellow.fg_str());
write!(&mut self.context.buf, "{}", color::Yellow.fg_str())?;
}
self.context.buf.push_str(&written_line);
}
......@@ -993,7 +983,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
}
if self.is_currently_showing_autosuggestion() || self.is_search() {
write!(&mut self.context.buf, "{}", color::Reset.fg_str());
write!(&mut self.context.buf, "{}", color::Reset.fg_str())?;
}
// at the end of the line, move the cursor down a line
......@@ -1011,7 +1001,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
&mut self.context.buf,
"{}",
cursor::Up(cursor_line_diff as u16)
);
)?;
} else if cursor_line_diff < 0 {
unreachable!();
}
......@@ -1026,23 +1016,18 @@ impl<'a, W: io::Write> Editor<'a, W> {
&mut self.context.buf,
"{}",
cursor::Left(cursor_col_diff as u16)
);
)?;
} else if cursor_col_diff < 0 {
write!(
&mut self.context.buf,
"{}",
cursor::Right((-cursor_col_diff) as u16)
);
)?;
}
self.term_cursor_line += completion_lines;
{
let out = &mut self.out;
out.write_all(self.context.buf.as_bytes());
self.context.buf.clear();
out.flush()
}
Ok(())
}
/// Deletes the displayed prompt and buffer, replacing them with the current prompt and buffer
......@@ -1054,7 +1039,10 @@ impl<'a, W: io::Write> Editor<'a, W> {
}
self.autosuggestion = self.current_autosuggestion();
self._display(true)
let _ = self._display(true);
self.out.write_all(self.context.buf.as_bytes())?;
self.context.buf.clear();
self.out.flush()
}
}
......
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