Skip to content

Commit

Permalink
Fix screen-tearing from writing invisibles
Browse files Browse the repository at this point in the history
Formatting the color-change screen for every invisible character
is inefficient. Since it doesn't change, we can save it in a static
variable.

Signed-off-by: Adam M Ludvik <[email protected]>
  • Loading branch information
aludvik committed Feb 10, 2020
1 parent dca2a8d commit 7f81857
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
termion="1"
lazy_static="1"

[dev-dependencies]
tempfile="3"
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(test)]
extern crate tempfile;
extern crate termion;
#[macro_use]
extern crate lazy_static;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -145,12 +147,23 @@ fn replace_invisibles(c: char) -> char {
}
}

lazy_static! {
static ref SET_NORMAL_COLORS: Vec<u8> = format!(
"{}",
termion::color::Fg(termion::color::Reset),
).into_bytes();
static ref SET_INVISIBLE_COLORS: Vec<u8> = format!(
"{}",
termion::color::Fg(termion::color::LightBlack),
).into_bytes();
}

fn set_normal_colors(scr: &mut Screen) -> io::Result<()> {
write!(scr, "{}", termion::color::Fg(termion::color::Reset))
scr.write(&SET_NORMAL_COLORS).map(|_|())
}

fn set_invisible_colors(scr: &mut Screen) -> io::Result<()> {
write!(scr, "{}", termion::color::Fg(termion::color::LightBlack))
scr.write(&SET_INVISIBLE_COLORS).map(|_|())
}

fn write_invisible_to_screen(scr: &mut Screen, mut c: char) -> io::Result<()> {
Expand Down

0 comments on commit 7f81857

Please sign in to comment.