From 91d3e81eca1dac65ca5331d08f7b01b537a1419c Mon Sep 17 00:00:00 2001 From: shy Date: Thu, 8 Apr 2021 09:46:36 +0200 Subject: [PATCH] Fix delayed update of roster_width. --- src/alarm.rs | 19 +++++++++---------- src/layout.rs | 7 +++++++ src/main.rs | 5 +++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/alarm.rs b/src/alarm.rs index 8403cd3..5d47ec3 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -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(&self, stdout: &mut RawTerminal, 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. diff --git a/src/layout.rs b/src/layout.rs index f934b4b..2fd6f09 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -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); + } + } } diff --git a/src/main.rs b/src/main.rs index 30711b0..9ec0bdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();