Slightly refactored clock drawing code.

This commit is contained in:
shy 2021-04-11 19:04:09 +02:00
parent 85f7b7af03
commit 557358a4c0
2 changed files with 35 additions and 60 deletions

View file

@ -82,6 +82,14 @@ impl Clock {
mut stdout: &mut RawTerminal<W>,
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<W: Write>(
@ -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],
digits[(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))
digits[(value % 10) as usize][l as usize])
.unwrap();
}
}
@ -195,21 +187,6 @@ 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),
@ -218,10 +195,5 @@ impl Clock {
dot)
.unwrap();
}
if self.paused {
write!(stdout, "{}", style::NoFaint).unwrap();
}
}
}

View file

@ -167,6 +167,9 @@ 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,