Working on multiple clock support.

This commit is contained in:
Bill Thiede 2019-09-21 18:00:16 -07:00
parent fd4d85a561
commit df5308e3dd
4 changed files with 68 additions and 4 deletions

View File

@ -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"

View File

@ -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

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod widgets;

52
src/widgets.rs Normal file
View File

@ -0,0 +1,52 @@
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()
}
}
/// The widget that shows local time
pub struct DateTimeWidget{
clock: dyn Clock<Tz>
}
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<WidgetUpdate> {
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())
})
}
}