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>, mut stdout: &mut RawTerminal<W>,
layout: &Layout, 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. // Draw hours if necessary.
if layout.force_redraw || self.elapsed % 3600 == 0 { if layout.force_redraw || self.elapsed % 3600 == 0 {
if self.elapsed >= 3600 { if self.elapsed >= 3600 {
@ -139,6 +147,15 @@ impl Clock {
self.elapsed % 60, self.elapsed % 60,
&layout.clock_sec, &layout.clock_sec,
layout.plain); 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>( fn draw_digit_pair<W: Write>(
@ -148,41 +165,16 @@ impl Clock {
pos: &Position, pos: &Position,
plain: bool, plain: bool,
) { ) {
if self.paused { let digits = if plain { DIGITS_PLAIN } else { DIGITS };
write!(stdout, "{}", style::Faint).unwrap();
}
if let Some(c) = self.color_index {
write!(stdout, "{}", color::Fg(COLOR[c])).unwrap();
}
for l in 0..DIGIT_HEIGHT { 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, write!(stdout,
"{}{}", "{}{} {}",
style::NoFaint, cursor::Goto(pos.col, pos.line + l),
color::Fg(color::Reset)) // First digit.
digits[(value / 10) as usize][l as usize],
// Second digit.
digits[(value % 10) as usize][l as usize])
.unwrap(); .unwrap();
} }
} }
@ -195,33 +187,13 @@ impl Clock {
) { ) {
let dot = if plain {'█'} else {'■'}; let dot = if plain {'█'} else {'■'};
if self.paused { write!(stdout,
write!(stdout, "{}", style::Faint).unwrap(); "{}{}{}{}",
} cursor::Goto(pos.col, pos.line + 1),
dot,
if let Some(c) = self.color_index { cursor::Goto(pos.col, pos.line + 3),
write!(stdout, dot)
"{}{}{}{}{}{}", .unwrap();
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();
}
} }
} }

View file

@ -167,7 +167,10 @@ pub fn run(
true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS, true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS,
false if layout.can_hold(MENUBAR) => MENUBAR, false if layout.can_hold(MENUBAR) => MENUBAR,
false if layout.can_hold(MENUBAR_SHORT) => MENUBAR_SHORT, 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, clear::AfterCursor,
style::NoFaint)?; style::NoFaint)?;