From faae3bebcacf296248bc82dbab6f94471705b730 Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Mon, 30 Sep 2019 15:51:42 -0700 Subject: [PATCH 1/2] Have DateTimeWidget include the time zone in its type. --- src/bin/work.rs | 2 +- src/widgets/datetime.rs | 52 +++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/bin/work.rs b/src/bin/work.rs index 184f772..918f379 100644 --- a/src/bin/work.rs +++ b/src/bin/work.rs @@ -25,7 +25,7 @@ fn main() { // Realtime upload/download rate for a interface bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); - let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), "Europe/London".to_string()); + let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), chrono_tz::Europe::London); dt.set_colors(&vec![ TimeColor { start: NaiveTime::from_hms(0, 0, 0), diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs index 7e899e8..35886f9 100644 --- a/src/widgets/datetime.rs +++ b/src/widgets/datetime.rs @@ -1,18 +1,16 @@ -use chrono::NaiveTime; -use chrono_tz::Tz; +use std::fmt::Display; +use chrono::{Local, Utc, NaiveTime, offset::TimeZone}; use i3monkit::Block; use i3monkit::ColorRGB; use i3monkit::{Widget, WidgetUpdate}; /// The widget that shows local time -pub struct DateTimeWidget { +pub struct DateTimeWidget { /// fmt to format the datetime, see /// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation. fmt: String, - /// tz string, values from https://docs.rs/chrono-tz/*/chrono_tz/ - /// None will use local time. - tz: Option, + tz: Tz, colors: Option>, } @@ -22,19 +20,21 @@ pub struct TimeColor { pub color: ColorRGB, } -impl DateTimeWidget { - // Create a new time widget +impl DateTimeWidget { pub fn new(fmt: String) -> Self { - DateTimeWidget { - fmt, - tz: None, - colors: None, - } + DateTimeWidget::tz(fmt, Local) } - pub fn tz(fmt: String, tz: String) -> Self { +} + +impl DateTimeWidget +where + Tz: TimeZone, + Tz::Offset: Display, +{ + pub fn tz(fmt: String, tz: Tz) -> Self { DateTimeWidget { fmt, - tz: Some(tz), + tz, colors: None, } } @@ -45,19 +45,15 @@ impl DateTimeWidget { } } -impl Widget for DateTimeWidget { +impl Widget for DateTimeWidget +where + Tz: TimeZone, + Tz::Offset: Display, +{ fn update(&mut self) -> Option { - let (time, time_string) = match &self.tz { - Some(tz) => { - let tz: Tz = tz.parse().unwrap(); - let now = chrono::Local::now(); - let now = now.with_timezone(&tz); - (now.time(), now.format(&self.fmt).to_string()) - } - None => { - let now = chrono::Local::now(); - (now.time(), now.format(&self.fmt).to_string()) - } + let (time, time_string) = { + let now = Utc::now().with_timezone(&self.tz); + (now.time(), now.format(&self.fmt).to_string()) }; let color = if let Some(colors) = &self.colors { colors @@ -81,7 +77,7 @@ impl Widget for DateTimeWidget { data.color(color); } Some(WidgetUpdate { - refresh_interval: std::time::Duration::new(1, 0), + refresh_interval: std::time::Duration::from_secs(1), data: Some(data), }) } -- 2.50.1 From de6bef6e364e85734b72971f1a3fae718fdc2701 Mon Sep 17 00:00:00 2001 From: Glenn Griffin Date: Tue, 1 Oct 2019 11:27:06 -0700 Subject: [PATCH 2/2] More datetime changes * Accept the format string as Into rather than String to allow &str to be provided. * set_colors accepts Into> rather than &[TimeColor]. The function needs a Vec. Previously it would unconditionally clone the slice into a Vec, now it can accept a Vec if the user provides one and avoid an allocation in those cases (while still cloning a slice if that's what the user provides). * DateTime::colors was an Option> it's now simply a Vec. There was no difference in logic between a None and an empty Vec so the code could be simplified by removing the Option. * No longer allocate a String when formatting the time. * Collapse the logic setting color. Calculating whether to set a color and storing the result in Option only to then set data.color if the Option is Some, can be collapsed to simply set data.color if the configuration dictates. --- src/bin/home.rs | 4 ++-- src/bin/work.rs | 8 +++---- src/widgets/datetime.rs | 47 ++++++++++++++++++++--------------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/bin/home.rs b/src/bin/home.rs index 6a5b29a..caac55a 100644 --- a/src/bin/home.rs +++ b/src/bin/home.rs @@ -25,8 +25,8 @@ fn main() { // Realtime upload/download rate for a interface bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); - let mut dt = DateTimeWidget::new("%m/%d %H:%M".to_string()); - dt.set_colors(&vec![ + let mut dt = DateTimeWidget::new("%m/%d %H:%M"); + dt.set_colors(vec![ TimeColor { start: NaiveTime::from_hms(19, 0, 0), color: ColorRGB::yellow(), diff --git a/src/bin/work.rs b/src/bin/work.rs index 918f379..c8a5074 100644 --- a/src/bin/work.rs +++ b/src/bin/work.rs @@ -25,8 +25,8 @@ fn main() { // Realtime upload/download rate for a interface bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); - let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), chrono_tz::Europe::London); - dt.set_colors(&vec![ + let mut dt = DateTimeWidget::tz("%H:%M %Z", chrono_tz::Europe::London); + dt.set_colors(vec![ TimeColor { start: NaiveTime::from_hms(0, 0, 0), color: ColorRGB::red(), @@ -46,8 +46,8 @@ fn main() { ]); bar.push(dt); - let mut dt = DateTimeWidget::new("%m/%d %H:%M".to_string()); - dt.set_colors(&vec![ + let mut dt = DateTimeWidget::new("%m/%d %H:%M"); + dt.set_colors(vec![ TimeColor { start: NaiveTime::from_hms(0, 0, 0), color: ColorRGB::red(), diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs index 35886f9..4d1a25e 100644 --- a/src/widgets/datetime.rs +++ b/src/widgets/datetime.rs @@ -1,5 +1,5 @@ +use chrono::{offset::TimeZone, Local, NaiveTime, Utc}; use std::fmt::Display; -use chrono::{Local, Utc, NaiveTime, offset::TimeZone}; use i3monkit::Block; use i3monkit::ColorRGB; @@ -11,7 +11,7 @@ pub struct DateTimeWidget { /// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation. fmt: String, tz: Tz, - colors: Option>, + colors: Vec, } #[derive(Clone)] @@ -21,7 +21,7 @@ pub struct TimeColor { } impl DateTimeWidget { - pub fn new(fmt: String) -> Self { + pub fn new(fmt: impl Into) -> Self { DateTimeWidget::tz(fmt, Local) } } @@ -31,17 +31,20 @@ where Tz: TimeZone, Tz::Offset: Display, { - pub fn tz(fmt: String, tz: Tz) -> Self { + pub fn tz(fmt: impl Into, tz: Tz) -> Self { DateTimeWidget { - fmt, + fmt: fmt.into(), tz, - colors: None, + colors: Vec::new(), } } - pub fn set_colors(&mut self, colors: &[TimeColor]) { - let mut colors = colors.to_vec(); + pub fn set_colors(&mut self, colors: impl Into>) { + let mut colors = colors.into(); colors.sort_by(|l, r| l.start.cmp(&r.start)); - self.colors = Some(colors); + self.colors = colors; + } + fn now(&self) -> chrono::DateTime { + Utc::now().with_timezone(&self.tz) } } @@ -51,29 +54,25 @@ where Tz::Offset: Display, { fn update(&mut self) -> Option { - let (time, time_string) = { - let now = Utc::now().with_timezone(&self.tz); - (now.time(), now.format(&self.fmt).to_string()) - }; - let color = if let Some(colors) = &self.colors { - colors - .iter() - .filter(|tc| tc.start < time) - .last() - .map(|tc| tc.color.clone()) - } else { - None - }; + let now = self.now(); let time_string = format!( r#"{}"#, - time_string + now.format(&self.fmt) ); let mut data = Block::new() .append_full_text(&time_string) .use_pango() .clone(); - if let Some(color) = color { + + let time = now.time(); + if let Some(color) = self + .colors + .iter() + .filter(|tc| tc.start < time) + .last() + .map(|tc| tc.color.clone()) + { data.color(color); } Some(WidgetUpdate { -- 2.50.1