Fix delayed update of roster_width.

This commit is contained in:
shy 2021-04-08 09:46:36 +02:00
parent 1e3e3d9fea
commit 91d3e81eca
3 changed files with 21 additions and 10 deletions

View file

@ -1,6 +1,5 @@
use std::io::Write;
use std::process::{Command, Stdio};
use std::sync::atomic::Ordering;
use termion::{color, cursor, style};
use termion::raw::RawTerminal;
use crate::{Clock, Config, Layout, Position};
@ -207,7 +206,6 @@ impl AlarmRoster {
// Draw alarm roster according to layout.
pub fn draw<W: Write>(&self, stdout: &mut RawTerminal<W>, layout: &mut Layout) {
let mut width: u16 = 0;
let mut index = 0;
// Find first item to print in case we lack space to print them all.
@ -247,16 +245,17 @@ impl AlarmRoster {
.unwrap();
}
index += 1;
// Calculate roster width. Actual display width is 3 chars wider.
if 3 + alarm.display.len() as u16 > width {
width = 3 + alarm.display.len() as u16;
}
}
// Update layout information.
if layout.roster_width != width {
layout.roster_width = width;
layout.force_recalc.store(true, Ordering::Relaxed);
}
// Return width of roster.
pub fn width(&self) -> u16 {
let mut width: u16 = 0;
for alarm in &self.list {
if alarm.display.len() as u16 > width { width = alarm.display.len() as u16; }
}
// Actual width is 3 columns wider if it's not 0.
if width == 0 { 0 } else { width.saturating_add(3) }
}
// Reset every alarm.

View file

@ -127,5 +127,12 @@ impl Layout {
col: 12,
};
}
pub fn set_roster_width(&mut self, width: u16) {
if self.width != width {
self.roster_width = width;
self.force_recalc.store(true, Ordering::Relaxed);
}
}
}

View file

@ -79,6 +79,9 @@ fn main() {
let mut buffer_updated: bool = false;
let mut countdown = Countdown::new();
// Initialise roster_width.
layout.set_roster_width(alarm_roster.width());
// Register signal handlers.
let signal = Arc::new(AtomicUsize::new(0));
register_signal_handlers(&signal, &layout);
@ -154,6 +157,7 @@ fn main() {
if alarm_roster.drop_last() {
// If we remove the last alarm we have to reset "countdown"
// manually. It is safe to do it anyway.
layout.set_roster_width(alarm_roster.width());
countdown.reset();
layout.force_redraw = true;
}
@ -180,6 +184,7 @@ fn main() {
error_msg(&mut stdout, &layout, e);
} else {
// Input buffer processed without error.
layout.set_roster_width(alarm_roster.width());
layout.force_redraw = true;
}
buffer.clear();