From 0a309fab1a134e36e51d96cb6b7ab4ea332f8698 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 31 Dec 2017 19:40:45 -0800 Subject: [PATCH] Made the screen buffer implemention way faster. It's in a pretty good place now, I think. Although its memory usage could be improved with a small string type. --- src/term_ui/mod.rs | 1 - src/term_ui/screen.rs | 25 ++++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index 20ea143..c453313 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -6,7 +6,6 @@ use std::io; use termion; use termion::event::{Event, Key}; use termion::input::TermRead; -use termion::color; use editor::Editor; use formatter::{block_index_and_offset, LineFormatter, LINE_BLOCK_LENGTH}; diff --git a/src/term_ui/screen.rs b/src/term_ui/screen.rs index b522bf6..14f1822 100644 --- a/src/term_ui/screen.rs +++ b/src/term_ui/screen.rs @@ -62,18 +62,33 @@ impl Screen { 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] { + if style != last_style { + tmp_string.push_str(&format!("{}", style)); + last_style = style; + } + tmp_string.push_str(text); + x += 1; + } else { write!( self.out.borrow_mut(), - "{}{}{}", - termion::cursor::Goto((x + 1) as u16, (y + 1) as u16), - style, - text, + "{}{}", + termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16), + tmp_string, ).unwrap(); + x += 1; + left_x = x; } - x += 1; } + write!( + self.out.borrow_mut(), + "{}{}", + termion::cursor::Goto((left_x + 1) as u16, (y + 1) as u16), + tmp_string, + ).unwrap(); } self.out.borrow_mut().flush().unwrap(); }