Fixed bug in screen buffer display code.

This both more correct and faster at the same time.  Yay!
This commit is contained in:
Nathan Vegdahl 2018-01-01 01:27:37 -08:00
parent 17beb9c06d
commit c91f1801ad
2 changed files with 21 additions and 24 deletions

View File

@ -114,7 +114,7 @@ impl<'a> TermUI<'a> {
draw {
self.editor.update_view_dim();
self.editor.formatter.set_wrap_width(self.editor.view_dim.1);
self.screen.clear();
self.screen.clear(Color::Black);
self.draw_editor(&self.editor, (0, 0), (self.height - 1, self.width - 1));
},
@ -216,7 +216,7 @@ impl<'a> TermUI<'a> {
draw {
self.editor.update_view_dim();
self.editor.formatter.set_wrap_width(self.editor.view_dim.1);
self.screen.clear();
self.screen.clear(Color::Black);
self.draw_editor(&self.editor, (0, 0), (self.height - 1, self.width - 1));
for i in 0..self.width {
self.screen.draw(i, 0, " ", style);

View File

@ -32,16 +32,16 @@ impl Screen {
}
}
pub(crate) fn clear(&self) {
pub(crate) fn clear(&self, col: Color) {
for cell in self.buf.borrow_mut().iter_mut() {
match *cell {
Some((ref mut style, ref mut text)) => {
*style = Style(Color::Black, Color::Black);
*style = Style(col, col);
text.clear();
text.push_str(" ");
}
_ => {
*cell = Some((Style(Color::Black, Color::Black), " ".into()));
*cell = Some((Style(col, col), " ".into()));
}
}
}
@ -57,12 +57,14 @@ impl Screen {
pub(crate) fn present(&self) {
let buf = self.buf.borrow();
let mut tmp_string = String::new();
// Double the minimum needed space, because of formatting characters and such.
let mut tmp_string = String::with_capacity(self.w * self.h * 2);
// Write everything to the tmp_string first.
for y in 0..self.h {
let mut x = 0;
let mut last_style = Style(Color::Black, Color::Black);
let mut left_x = 0;
tmp_string.clear();
tmp_string.push_str(&format!("{}", last_style));
while x < self.w {
if let Some((style, ref text)) = buf[y * self.w + x] {
@ -73,23 +75,18 @@ impl Screen {
tmp_string.push_str(text);
x += 1;
} else {
write!(
self.out.borrow_mut(),
"{}{}",
termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16),
tmp_string,
).unwrap();
x += 1;
left_x = x;
}
}
write!(
self.out.borrow_mut(),
"{}{}",
termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16),
tmp_string,
).unwrap();
}
// Then write the tmp_string to screen all at once.
write!(
self.out.borrow_mut(),
"{}{}",
termion::cursor::Goto(0, 0),
tmp_string,
).unwrap();
self.out.borrow_mut().flush().unwrap();
}
@ -122,11 +119,11 @@ impl Drop for Screen {
fn drop(&mut self) {
write!(
self.out.borrow_mut(),
"{}{}",
"{}{}{}",
color::Fg(color::Reset),
color::Bg(color::Reset)
color::Bg(color::Reset),
termion::clear::All,
).unwrap();
self.clear();
self.show_cursor();
}
}