Allow time color to change based on time of day.

This commit is contained in:
Bill Thiede 2019-09-21 22:10:03 -07:00
parent 2d054bb9a1
commit 4cb50597e1
4 changed files with 62 additions and 11 deletions

10
Cargo.lock generated
View File

@ -130,14 +130,13 @@ dependencies = [
[[package]]
name = "i3monkit"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/38/i3monkit#d72d94d103b2621ebfc06c9950d3bba9613e4936"
dependencies = [
"alsa 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"curl 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -147,7 +146,7 @@ version = "0.1.0"
dependencies = [
"chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono-tz 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"i3monkit 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"i3monkit 0.1.2 (git+https://github.com/38/i3monkit)",
"num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"spark 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
@ -330,6 +329,9 @@ dependencies = [
name = "serde"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_derive"
@ -493,7 +495,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum curl 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "d08ad3cb89d076a36b0ce5749eec2c9964f70c0c58480ab6b75a91ec4fc206d8"
"checksum curl-sys 0.4.21 (registry+https://github.com/rust-lang/crates.io-index)" = "520594da9914c1dc77ce3be450fc1c74fde67c82966d80f8e93c6d460eb0e9ae"
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
"checksum i3monkit 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "441cad16a81164a7d7e3c3eadff8e73f83834715554c735ec51b6d891878e141"
"checksum i3monkit 0.1.2 (git+https://github.com/38/i3monkit)" = "<none>"
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"

View File

@ -19,7 +19,8 @@ name = "i3xs-home"
path = "src/bin/home.rs"
[dependencies]
i3monkit = "0.1.2"
# Use git until ColorRGB is pub in crates
i3monkit = {git = "https://github.com/38/i3monkit"} # "0.1.2"
num_cpus = "1.0"
structopt = { version = "0.2", default-features = false }
chrono = "0.4"

View File

@ -1,11 +1,12 @@
use chrono::NaiveTime;
use i3monkit::widgets::CpuWidget;
use i3monkit::{Header, I3Protocol, WidgetCollection};
use i3monkit::{ColorRGB, Header, I3Protocol, WidgetCollection};
use num_cpus;
use structopt::StructOpt;
use i3xs::widgets::datetime::DateTimeWidget;
use i3xs::widgets::datetime::{DateTimeWidget, TimeColor};
use i3xs::widgets::network::NetworkSpeedWidget;
#[derive(Debug, StructOpt)]
@ -28,7 +29,18 @@ fn main() {
// Realtime upload/download rate for a interface
bar.push(NetworkSpeedWidget::new(&opts.nic, 6));
bar.push(DateTimeWidget::new("%m/%d %H:%M".to_string()));
let mut dt = DateTimeWidget::new("%m/%d %H:%M".to_string());
dt.set_colors(&vec![
TimeColor {
start: NaiveTime::from_hms(19, 0, 0),
color: ColorRGB::yellow(),
},
TimeColor {
start: NaiveTime::from_hms(20, 0, 0),
color: ColorRGB::red(),
},
]);
bar.push(dt);
// Then start updating the status bar
bar.update_loop(I3Protocol::new(Header::new(1), std::io::stdout()));

View File

@ -1,6 +1,8 @@
use chrono::NaiveTime;
use chrono_tz::Tz;
use i3monkit::Block;
use i3monkit::ColorRGB;
use i3monkit::{Widget, WidgetUpdate};
/// The widget that shows local time
@ -11,21 +13,51 @@ pub struct DateTimeWidget {
/// tz string, values from https://docs.rs/chrono-tz/*/chrono_tz/
/// None will use local time.
tz: Option<String>,
colors: Option<Vec<TimeColor>>,
}
#[derive(Clone)]
pub struct TimeColor {
pub start: NaiveTime,
pub color: ColorRGB,
}
impl DateTimeWidget {
// Create a new time widget
pub fn new(fmt: String) -> Self {
DateTimeWidget { fmt, tz: None }
DateTimeWidget {
fmt,
tz: None,
colors: None,
}
}
pub fn tz(fmt: String, tz: String) -> Self {
DateTimeWidget { fmt, tz: Some(tz) }
DateTimeWidget {
fmt,
tz: Some(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 {
fn update(&mut self) -> Option<WidgetUpdate> {
let now = chrono::Local::now();
let color = if let Some(colors) = &self.colors {
colors
.iter()
.filter(|tc| tc.start < now.time())
.last()
.map(|tc| tc.color.clone())
} else {
None
};
let time_string = match &self.tz {
Some(tz) => {
let tz: Tz = tz.parse().unwrap();
@ -36,9 +68,13 @@ impl Widget for DateTimeWidget {
None => now.format(&self.fmt).to_string(),
};
let mut data = Block::new().append_full_text(&time_string).clone();
if let Some(color) = color {
data.color(color);
}
Some(WidgetUpdate {
refresh_interval: std::time::Duration::new(1, 0),
data: Some(Block::new().append_full_text(&time_string).clone()),
data: Some(data),
})
}
}