From df5308e3ddc0f14995ede51c45794f98c2ae8d85 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 21 Sep 2019 18:00:16 -0700 Subject: [PATCH] Working on multiple clock support. --- Cargo.toml | 11 ++++++++- src/{ => bin}/main.rs | 8 ++++--- src/lib.rs | 1 + src/widgets.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) rename src/{ => bin}/main.rs (78%) create mode 100644 src/lib.rs create mode 100644 src/widgets.rs diff --git a/Cargo.toml b/Cargo.toml index 5ca0f76..8ca99fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,17 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "i3xs" +path = "src/lib.rs" + +[[bin]] +name = "i3xs" +path = "src/bin/main.rs" + [dependencies] i3monkit = "0.1.2" num_cpus = "1.0" structopt = { version = "0.2", default-features = false } - +chrono = "0.4" +chrono-tz = "0.5" diff --git a/src/main.rs b/src/bin/main.rs similarity index 78% rename from src/main.rs rename to src/bin/main.rs index 1013557..778059d 100644 --- a/src/main.rs +++ b/src/bin/main.rs @@ -1,10 +1,12 @@ -use i3monkit::widgets::*; -use i3monkit::*; +use i3monkit::{I3Protocol, Header, WidgetCollection}; +use i3monkit::widgets::{NetworkSpeedWidget, CpuWidget}; use num_cpus; use structopt::StructOpt; +use i3xs::widgets::DateTimeWidget; + #[derive(Debug, StructOpt)] #[structopt(name="i3xs", about="Custom i3 status bar program.")] struct Opt { @@ -25,7 +27,7 @@ fn main() { bar.push(CpuWidget::new(i)); } - //Time + bar.push(DateTimeWidget::new()); bar.push(DateTimeWidget::new()); // Then start updating the satus bar diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..42e1b6d --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod widgets; diff --git a/src/widgets.rs b/src/widgets.rs new file mode 100644 index 0000000..a58d406 --- /dev/null +++ b/src/widgets.rs @@ -0,0 +1,52 @@ +use chrono::DateTime; +use chrono_tz::Tz; + +use i3monkit::{Widget, WidgetUpdate}; +use i3monkit::Block; + +trait Clock { + fn now() -> DateTime; +} + +struct LocalClock; +impl Clock for LocalClock { + fn now() -> DateTime { + chrono::Local::now() + } +} + +/// The widget that shows local time +pub struct DateTimeWidget{ + clock: dyn Clock +} + +impl DateTimeWidget { + /// Create a new time widget + pub fn new() -> Self { + DateTimeWidget{ + clock: LocalClock{}, + } + } + /* + pub fn tz(tz : String)->Self { + DateTimeWidget{ + clock: tz.parse().unwrap(), + } + } + */ +} + +impl Widget for DateTimeWidget { + fn update(&mut self) -> Option { + let time_string = if self.0 { + format!("{}", self.now().format("%H:%M")) + } else { + format!("{}", self.now().format("%H %M")) + }; + + Some(WidgetUpdate { + refresh_interval: std::time::Duration::new(1, 0), + data: Some(Block::new().append_full_text(&time_string).clone()) + }) + } +}