Allow DateTimeWidget to use timezones and specify format.

This commit is contained in:
2019-09-21 20:15:41 -07:00
parent df5308e3dd
commit cbb709afda
3 changed files with 114 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
use i3monkit::{I3Protocol, Header, WidgetCollection};
use i3monkit::widgets::{NetworkSpeedWidget, CpuWidget};
use i3monkit::widgets::{CpuWidget, NetworkSpeedWidget};
use i3monkit::{Header, I3Protocol, WidgetCollection};
use num_cpus;
@@ -8,7 +8,7 @@ use structopt::StructOpt;
use i3xs::widgets::DateTimeWidget;
#[derive(Debug, StructOpt)]
#[structopt(name="i3xs", about="Custom i3 status bar program.")]
#[structopt(name = "i3xs", about = "Custom i3 status bar program.")]
struct Opt {
#[structopt(short, long)]
nic: String,
@@ -17,18 +17,21 @@ struct Opt {
fn main() {
let mut bar = WidgetCollection::new();
let opts=Opt::from_args();
// Realtime upload/download rate for a interface
bar.push(NetworkSpeedWidget::new(&opts.nic));
let opts = Opt::from_args();
//Display all the cpu usage for each core
for i in 0..num_cpus::get() as u32 {
bar.push(CpuWidget::new(i));
}
bar.push(DateTimeWidget::new());
bar.push(DateTimeWidget::new());
// Realtime upload/download rate for a interface
bar.push(NetworkSpeedWidget::new(&opts.nic));
bar.push(DateTimeWidget::tz(
"%H:%M %Z".to_string(),
"Europe/London".to_string(),
));
bar.push(DateTimeWidget::new("%m/%d %H:%M".to_string()));
// Then start updating the satus bar
bar.update_loop(I3Protocol::new(Header::new(1), std::io::stdout()));

View File

@@ -1,52 +1,44 @@
use chrono::DateTime;
use chrono_tz::Tz;
use i3monkit::{Widget, WidgetUpdate};
use i3monkit::Block;
trait Clock<Tz> {
fn now() -> DateTime<Tz>;
}
struct LocalClock;
impl Clock<Tz> for LocalClock {
fn now() -> DateTime<Tz> {
chrono::Local::now()
}
}
use i3monkit::{Widget, WidgetUpdate};
/// The widget that shows local time
pub struct DateTimeWidget{
clock: dyn Clock<Tz>
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<String>,
}
impl DateTimeWidget {
/// Create a new time widget
pub fn new() -> Self {
DateTimeWidget{
clock: LocalClock{},
}
// Create a new time widget
pub fn new(fmt: String) -> Self {
DateTimeWidget { fmt, tz: None }
}
/*
pub fn tz(tz : String)->Self {
DateTimeWidget{
clock: tz.parse().unwrap(),
}
pub fn tz(fmt: String, tz: String) -> Self {
DateTimeWidget { fmt, tz: Some(tz) }
}
*/
}
impl Widget for DateTimeWidget {
fn update(&mut self) -> Option<WidgetUpdate> {
let time_string = if self.0 {
format!("{}", self.now().format("%H:%M"))
} else {
format!("{}", self.now().format("%H %M"))
};
let now = chrono::Local::now();
let time_string = match &self.tz {
Some(tz) => {
let tz: Tz = tz.parse().unwrap();
let now = now.with_timezone(&tz);
//TODO(wathiede): convert to timezone.
now.format(&self.fmt).to_string()
}
None => now.format(&self.fmt).to_string(),
};
Some(WidgetUpdate {
refresh_interval: std::time::Duration::new(1, 0),
data: Some(Block::new().append_full_text(&time_string).clone())
})
Some(WidgetUpdate {
refresh_interval: std::time::Duration::new(1, 0),
data: Some(Block::new().append_full_text(&time_string).clone()),
})
}
}