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 { /// fmt to format the datetime, see /// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation. fmt: String, tz: Tz, colors: Option>, } #[derive(Clone)] pub struct TimeColor { pub start: NaiveTime, pub color: ColorRGB, } impl DateTimeWidget { pub fn new(fmt: String) -> Self { DateTimeWidget::tz(fmt, Local) } } impl DateTimeWidget where Tz: TimeZone, Tz::Offset: Display, { pub fn tz(fmt: String, tz: Tz) -> Self { DateTimeWidget { fmt, tz, colors: None, } } pub fn set_colors(&mut self, colors: &[TimeColor]) { let mut colors = colors.to_vec(); colors.sort_by(|l, r| l.start.cmp(&r.start)); self.colors = Some(colors); } } impl Widget for DateTimeWidget where Tz: TimeZone, 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 time_string = format!( r#"{}"#, time_string ); let mut data = Block::new() .append_full_text(&time_string) .use_pango() .clone(); if let Some(color) = color { data.color(color); } Some(WidgetUpdate { refresh_interval: std::time::Duration::from_secs(1), data: Some(data), }) } }