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 { draw {
self.editor.update_view_dim(); self.editor.update_view_dim();
self.editor.formatter.set_wrap_width(self.editor.view_dim.1); 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)); self.draw_editor(&self.editor, (0, 0), (self.height - 1, self.width - 1));
}, },
@ -216,7 +216,7 @@ impl<'a> TermUI<'a> {
draw { draw {
self.editor.update_view_dim(); self.editor.update_view_dim();
self.editor.formatter.set_wrap_width(self.editor.view_dim.1); 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)); self.draw_editor(&self.editor, (0, 0), (self.height - 1, self.width - 1));
for i in 0..self.width { for i in 0..self.width {
self.screen.draw(i, 0, " ", style); 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() { for cell in self.buf.borrow_mut().iter_mut() {
match *cell { match *cell {
Some((ref mut style, ref mut text)) => { Some((ref mut style, ref mut text)) => {
*style = Style(Color::Black, Color::Black); *style = Style(col, col);
text.clear(); text.clear();
text.push_str(" "); 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) { pub(crate) fn present(&self) {
let buf = self.buf.borrow(); 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 { for y in 0..self.h {
let mut x = 0; let mut x = 0;
let mut last_style = Style(Color::Black, Color::Black); let mut last_style = Style(Color::Black, Color::Black);
let mut left_x = 0;
tmp_string.clear();
tmp_string.push_str(&format!("{}", last_style)); tmp_string.push_str(&format!("{}", last_style));
while x < self.w { while x < self.w {
if let Some((style, ref text)) = buf[y * self.w + x] { if let Some((style, ref text)) = buf[y * self.w + x] {
@ -73,23 +75,18 @@ impl Screen {
tmp_string.push_str(text); tmp_string.push_str(text);
x += 1; x += 1;
} else { } else {
write!(
self.out.borrow_mut(),
"{}{}",
termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16),
tmp_string,
).unwrap();
x += 1; x += 1;
left_x = x;
} }
} }
}
// Then write the tmp_string to screen all at once.
write!( write!(
self.out.borrow_mut(), self.out.borrow_mut(),
"{}{}", "{}{}",
termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16), termion::cursor::Goto(0, 0),
tmp_string, tmp_string,
).unwrap(); ).unwrap();
}
self.out.borrow_mut().flush().unwrap(); self.out.borrow_mut().flush().unwrap();
} }
@ -122,11 +119,11 @@ impl Drop for Screen {
fn drop(&mut self) { fn drop(&mut self) {
write!( write!(
self.out.borrow_mut(), self.out.borrow_mut(),
"{}{}", "{}{}{}",
color::Fg(color::Reset), color::Fg(color::Reset),
color::Bg(color::Reset) color::Bg(color::Reset),
termion::clear::All,
).unwrap(); ).unwrap();
self.clear();
self.show_cursor(); self.show_cursor();
} }
} }