Commit 29e4434e authored by stratact's avatar stratact

Reorganize and optimize

parent c5b37717
......@@ -672,7 +672,7 @@ impl<'a, W: Write> Editor<'a, W> {
total
}
let terminal_width = terminal_width()?;
let terminal_width = util::terminal_width()?;
let prompt_width = util::last_prompt_line_width(&self.prompt);
let buf = cur_buf!(self);
......@@ -730,17 +730,12 @@ impl<'a, W: Write> Editor<'a, W> {
// Write the prompt
if ! self.no_newline {
let prompt: String = self.prompt
.chars()
.fold(String::with_capacity(self.prompt.len()) , |mut string, c| {
if c == '\n' {
string + "\r\n"
} else {
string.push(c);
string
}
});
self.output_buf.extend_from_slice(prompt.as_bytes());
for line in self.prompt.split('\n') {
self.output_buf.extend_from_slice(line.as_bytes());
self.output_buf.extend_from_slice(b"\r\n");
}
self.output_buf.pop(); // pop the '\n'
self.output_buf.pop(); // pop the '\r'
} else {
self.output_buf.extend_from_slice(util::handle_prompt(&self.prompt).as_bytes());
}
......@@ -843,18 +838,6 @@ impl<'a, W: Write> From<Editor<'a, W>> for String {
}
}
fn terminal_width() -> io::Result<usize> {
if cfg!(test) {
Ok(80 as usize)
} else {
let (mut size_col, _) = termion::terminal_size()?;
if size_col == 0 {
size_col = 80;
}
Ok(size_col as usize)
}
}
#[cfg(test)]
mod tests {
use super::*;
......
use std::borrow::Cow;
use std::{
borrow::Cow,
io
};
use unicode_width::*;
pub fn last_prompt_line_width<S: AsRef<str>>(s: S) -> usize {
......@@ -92,3 +95,15 @@ pub fn handle_prompt(full_prompt: &str) -> &str {
full_prompt
}
}
pub fn terminal_width() -> io::Result<usize> {
if cfg!(test) {
Ok(80 as usize)
} else {
let (mut size_col, _) = termion::terminal_size()?;
if size_col == 0 {
size_col = 80;
}
Ok(size_col as usize)
}
}
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