From ff52420876fb4e9a2d205d5a819db0234fc36b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 4 May 2021 00:00:46 +0200 Subject: wip buffer for less flashing --- src/window.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/window.rs b/src/window.rs index baabc30..809ac4d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -18,7 +18,7 @@ pub enum Line { } pub fn truncate_dots(s: &str, to: usize) -> String { - //TODO We don't have time to do this every tick + //TODO Don't do this every tick let graphemes = s.graphemes(true).collect::>(); if graphemes.len() >= to { format!("{}...", graphemes.iter().take(to - 3).fold(String::new(), |acc, s| format!("{}{}", acc, s))) @@ -41,16 +41,17 @@ impl Window { } pub fn draw(&mut self, out: &mut W, area: Area) -> Result<(), std::io::Error> { + let mut buffer = Vec::new(); let mut y = area.y; let mut lines = self.lines.iter().skip(self.scroll); while y < area.h + 1 { match lines.next() { Some(line) => { - write!(out, "{}", cursor::Goto(area.x, y))?; + write!(buffer, "{}", cursor::Goto(area.x, y))?; match line { - Line::Normal(s) => write!(out, "{}", truncate_dots(s, area.w as usize))?, + Line::Normal(s) => write!(buffer, "{}", truncate_dots(s, area.w as usize))?, Line::Highlight(s) => write!( - out, + buffer, "{}{}{}", color::Fg(color::Red), truncate_dots(s, area.w as usize), @@ -62,6 +63,7 @@ impl Window { None => break, } } + out.write(&buffer)?; out.flush()?; self.lines.clear(); -- cgit v1.2.1