Working on multiple clock support.
This commit is contained in:
parent
fd4d85a561
commit
df5308e3dd
11
Cargo.toml
11
Cargo.toml
@ -6,8 +6,17 @@ edition = "2018"
|
|||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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]
|
[dependencies]
|
||||||
i3monkit = "0.1.2"
|
i3monkit = "0.1.2"
|
||||||
num_cpus = "1.0"
|
num_cpus = "1.0"
|
||||||
structopt = { version = "0.2", default-features = false }
|
structopt = { version = "0.2", default-features = false }
|
||||||
|
chrono = "0.4"
|
||||||
|
chrono-tz = "0.5"
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
use i3monkit::widgets::*;
|
use i3monkit::{I3Protocol, Header, WidgetCollection};
|
||||||
use i3monkit::*;
|
use i3monkit::widgets::{NetworkSpeedWidget, CpuWidget};
|
||||||
|
|
||||||
use num_cpus;
|
use num_cpus;
|
||||||
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
use i3xs::widgets::DateTimeWidget;
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(name="i3xs", about="Custom i3 status bar program.")]
|
#[structopt(name="i3xs", about="Custom i3 status bar program.")]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
@ -25,7 +27,7 @@ fn main() {
|
|||||||
bar.push(CpuWidget::new(i));
|
bar.push(CpuWidget::new(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Time
|
bar.push(DateTimeWidget::new());
|
||||||
bar.push(DateTimeWidget::new());
|
bar.push(DateTimeWidget::new());
|
||||||
|
|
||||||
// Then start updating the satus bar
|
// Then start updating the satus bar
|
||||||
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod widgets;
|
||||||
52
src/widgets.rs
Normal file
52
src/widgets.rs
Normal 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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user