Created
August 13, 2017 14:51
-
-
Save valpackett/099cff602a3164d9c839c32ef3da773e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env run-cargo-script | |
//! ```cargo | |
//! [dependencies] | |
//! unixbar = "0" | |
//! systemstat = "0" | |
//! ``` | |
#[macro_use] extern crate unixbar; | |
extern crate systemstat; | |
use unixbar::*; | |
use systemstat::{System, Platform}; | |
fn load_color(percent: f32) -> String { | |
match percent { | |
x if x >= 80.0 => "#d24b58", | |
60.0...80.0 => "#daa345", | |
_ => "#94aa82", | |
}.to_owned() | |
} | |
fn main() { | |
UnixBar::new(I3BarFormatter::new()) | |
.add(Text::new(bfmt![right])) | |
.add(Periodic::new( | |
Duration::from_secs(2), | |
|| match System::new().memory() { | |
Ok(mem) => { | |
let percent = (1.0 - mem.free.as_usize() as f32/mem.total.as_usize() as f32) * 100.0; | |
bfmt![fg[load_color(percent)] fmt[" {:02.0}% \u{f1c0} ", percent]] | |
}, | |
Err(_) => bfmt![fg["#bb1155"] text["error"]], | |
})) | |
.add(Delayed::new( | |
Duration::from_secs(2), | |
|| System::new().cpu_load_aggregate().unwrap(), | |
|res| match res { | |
Ok(cpu) => { | |
let percent = (1.0 - cpu.idle) * 100.0; | |
bfmt![fg[load_color(percent)] fmt[" {:02.0}% \u{f0e4} ", percent]] | |
}, | |
Err(_) => bfmt![fg["#bb1155"] text["error"]], | |
})) | |
.add(Periodic::new( | |
Duration::from_secs(30), | |
|| match System::new().battery_life() { | |
Ok(battery) => { | |
let percent = (battery.remaining_capacity * 100.0) as u8; | |
let color = match percent { | |
x if x >= 20 => "#94aa82", | |
20...40 => "#daa345", | |
_ => "#d24b58", | |
}; | |
let icon = match percent { | |
x if x >= 80 => "\u{f240}", | |
60...80 => "\u{f241}", | |
40...60 => "\u{f242}", | |
20...40 => "\u{f243}", | |
_ => "\u{f244}", | |
}; | |
bfmt![fg[color] fmt[" {:02}% {} ", percent, icon]] | |
}, | |
Err(_) => bfmt![text[""]], | |
})) | |
.add(Xkb::new(|id| { | |
let layout = match id { | |
0 => "EN", | |
1 => "RU", | |
_ => "??", | |
}; | |
bfmt![ fg["#bebebe"] fmt[" {} \u{f11c} ", layout] ] | |
})) | |
.add(Wrap::new( | |
|f| bfmt![fg["#ececec"] f], | |
DateTime::new(" %a %b %e %H:%M "))) | |
//.add(Text::new(bfmt![ fg["#bebebe"] click[MouseButton::Left => format!("xautolock -locknow")] text[" \u{f023} "] ])) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment