From 557358a4c0f935112fd43b2a3aeccdde538212bf Mon Sep 17 00:00:00 2001 From: shy Date: Sun, 11 Apr 2021 19:04:09 +0200 Subject: [PATCH] Slightly refactored clock drawing code. --- src/clock.rs | 90 ++++++++++++++++++---------------------------------- src/lib.rs | 5 ++- 2 files changed, 35 insertions(+), 60 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 3489ca2..13e6cbb 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -82,6 +82,14 @@ impl Clock { mut stdout: &mut RawTerminal, layout: &Layout, ) { + // Setup style and color if appropriate. + if self.paused { + write!(stdout, "{}", style::Faint).unwrap(); + } + if let Some(c) = self.color_index { + write!(stdout, "{}", color::Fg(COLOR[c])).unwrap(); + } + // Draw hours if necessary. if layout.force_redraw || self.elapsed % 3600 == 0 { if self.elapsed >= 3600 { @@ -139,6 +147,15 @@ impl Clock { self.elapsed % 60, &layout.clock_sec, layout.plain); + + // Reset color and style. + if self.paused || self.color_index != None { + write!(stdout, + "{}{}", + style::NoFaint, + color::Fg(color::Reset)) + .unwrap(); + } } fn draw_digit_pair( @@ -148,41 +165,16 @@ impl Clock { pos: &Position, plain: bool, ) { - if self.paused { - write!(stdout, "{}", style::Faint).unwrap(); - } - - if let Some(c) = self.color_index { - write!(stdout, "{}", color::Fg(COLOR[c])).unwrap(); - } + let digits = if plain { DIGITS_PLAIN } else { DIGITS }; for l in 0..DIGIT_HEIGHT { - if plain { - write!(stdout, - "{}{} {}", - cursor::Goto(pos.col, pos.line + l), - // First digit. - DIGITS_PLAIN[(value / 10) as usize][l as usize], - // Second digit. - DIGITS_PLAIN[(value % 10) as usize][l as usize]) - .unwrap(); - } else { - write!(stdout, - "{}{} {}", - cursor::Goto(pos.col, pos.line + l), - // First digit. - DIGITS[(value / 10) as usize][l as usize], - // Second digit. - DIGITS[(value % 10) as usize][l as usize]) - .unwrap(); - } - } - - if self.paused || self.color_index != None { write!(stdout, - "{}{}", - style::NoFaint, - color::Fg(color::Reset)) + "{}{} {}", + cursor::Goto(pos.col, pos.line + l), + // First digit. + digits[(value / 10) as usize][l as usize], + // Second digit. + digits[(value % 10) as usize][l as usize]) .unwrap(); } } @@ -195,33 +187,13 @@ impl Clock { ) { let dot = if plain {'█'} else {'■'}; - if self.paused { - write!(stdout, "{}", style::Faint).unwrap(); - } - - if let Some(c) = self.color_index { - write!(stdout, - "{}{}{}{}{}{}", - cursor::Goto(pos.col, pos.line + 1), - color::Fg(COLOR[c]), - dot, - cursor::Goto(pos.col, pos.line + 3), - dot, - color::Fg(color::Reset)) - .unwrap(); - } else { - write!(stdout, - "{}{}{}{}", - cursor::Goto(pos.col, pos.line + 1), - dot, - cursor::Goto(pos.col, pos.line + 3), - dot) - .unwrap(); - } - - if self.paused { - write!(stdout, "{}", style::NoFaint).unwrap(); - } + write!(stdout, + "{}{}{}{}", + cursor::Goto(pos.col, pos.line + 1), + dot, + cursor::Goto(pos.col, pos.line + 3), + dot) + .unwrap(); } } diff --git a/src/lib.rs b/src/lib.rs index e684956..d0b69aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,7 +167,10 @@ pub fn run( true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS, false if layout.can_hold(MENUBAR) => MENUBAR, false if layout.can_hold(MENUBAR_SHORT) => MENUBAR_SHORT, - _ => "", + // Clearing the screen from position 1, 1 seems to have + // unwanted side effects. We avoid this by writing a + // single space here. + _ => " ", }, clear::AfterCursor, style::NoFaint)?;