forked from wathiede/i3xs
85 lines
2.1 KiB
Rust
85 lines
2.1 KiB
Rust
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<Tz> {
|
|
/// fmt to format the datetime, see
|
|
/// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation.
|
|
fmt: String,
|
|
tz: Tz,
|
|
colors: Option<Vec<TimeColor>>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct TimeColor {
|
|
pub start: NaiveTime,
|
|
pub color: ColorRGB,
|
|
}
|
|
|
|
impl DateTimeWidget<Local> {
|
|
pub fn new(fmt: String) -> Self {
|
|
DateTimeWidget::tz(fmt, Local)
|
|
}
|
|
}
|
|
|
|
impl<Tz> DateTimeWidget<Tz>
|
|
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<Tz> Widget for DateTimeWidget<Tz>
|
|
where
|
|
Tz: TimeZone,
|
|
Tz::Offset: Display,
|
|
{
|
|
fn update(&mut self) -> Option<WidgetUpdate> {
|
|
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#"<span size="x-large" weight="heavy">{}</span>"#,
|
|
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),
|
|
})
|
|
}
|
|
}
|