Created
August 26, 2017 12:51
-
-
Save Ploppz/ec609784491ae2c2ecbc98d4532c1f0d 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
#[macro_use] extern crate unixbar; | |
extern crate systemstat; | |
use unixbar::*; | |
use systemstat::{System, Platform, ByteSize}; | |
const BGS: &'static [&'static str] = &["#111111", "#1F4B17", "#111111", "#497933"]; | |
const BGS_c: &'static [&'static str] = &["#111111", "#"]; | |
// Colors for center | |
const FG_WINGS: &'static str = "#ffaacc"; | |
const BG_WINGS: &'static str = "#00000000"; | |
const BG_INACTIVE: &'static str = "#111111"; | |
const BG_ACTIVE: &'static str = "#99aa11"; | |
const GREEK_BIG: [char; 24] = ['Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ', 'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω']; | |
const GREEK_SMALL: [char; 24] = ['α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο', 'π', 'ρ', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω']; | |
fn main() { | |
let mut sep = Separator::new("#770000", "#00000000"); | |
UnixBar::new(LemonbarFormatter::new()) | |
.add(Volume::new(ALSA::new(), | |
|volume| { | |
match volume.muted { | |
false => bfmt![fmt["⮞ {}", (volume.volume * 100.0) as i32]], | |
true => bfmt![fmt["⮝ {}", (volume.volume * 100.0) as i32]] | |
} | |
} | |
)) | |
.add(Text::new(sep.right("#ffffff", BGS[3]))) | |
.add(Periodic::new( | |
Duration::from_secs(15), | |
move || { | |
match (System::new().battery_life(), System::new().on_ac_power()) { | |
(Ok(battery_life), Ok(true)) => bfmt![fg["#ffffff"] bg["#000000"] fmt[" {} {} ", "⮒", battery_life.remaining_capacity]], | |
(Ok(battery_life), Ok(false)) => { | |
let battery_life = battery_life.remaining_capacity; | |
if battery_life > 0.5 { | |
bfmt![fg["#ffffff"] bg["#000000"] fmt[" {} {} ", "⮏", battery_life]] | |
} else if battery_life > 0.2 { | |
bfmt![fg["#ffffff"] bg["#000000"] fmt[" {} {} ", "⮑", battery_life]] | |
} else { | |
bfmt![fg["#ffffff"] bg["#000000"] fmt[" {} {} ", "⮐", battery_life]] | |
} | |
} | |
_ => bfmt![text[""]], | |
} | |
} | |
) | |
) | |
.add(Mpd::new( | |
|mpd| { | |
if let Some(true) = mpd.playback.map(|playback| playback.playing) { | |
if mpd.title == "" { | |
bfmt![fmt["{}", mpd.filename]] | |
} else if mpd.artist == "" { | |
bfmt![fmt["{}", mpd.title]] | |
} else { | |
bfmt![fmt["{} by {}", mpd.title, mpd.artist ]] | |
} | |
} else { | |
bfmt![text["⮔"]] | |
} | |
} | |
)) | |
/* Center */ | |
.add(Text::new(bfmt![center])) | |
.add(Bspwm::new( | |
move |state| { | |
let mut sep = sep.clone(); | |
let mut formats = Vec::new(); | |
let window_mode = match state.window_mode { | |
WindowMode::Tiled => "tiled ", | |
WindowMode::PseudoTiled => "pseudo", | |
WindowMode::Floating => "float ", | |
WindowMode::FullScreen => "full ", | |
WindowMode::None => "none " | |
}; | |
let layout = if state.monocle { "monocle" } else { " tile" }; | |
formats.push(Box::new({ | |
bfmt![ fg[FG_WINGS] bg[BG_WINGS] fmt[" {} ", window_mode]] | |
})); | |
formats.push(Box::new(sep.left("#ffffff", BGS[0]))); | |
for (i, d) in state.desktops.iter().enumerate() { | |
formats.push(Box::new({ | |
let index = d.name.parse::<usize>().unwrap(); | |
let (fg, bg, name) = match (d.focused, d.occupied) { | |
(true, _) => ("#ffffff", BG_ACTIVE, GREEK_BIG[index]), | |
(_, true) => (BG_ACTIVE, BG_INACTIVE, GREEK_BIG[index]), | |
_ => ("#555544", BG_INACTIVE, GREEK_SMALL[index]) | |
}; | |
bfmt![ fg[fg] bg[bg] fmt[" {} ", name] ] | |
})); | |
if i % 3 == 2 && i != 8 { | |
formats.push(Box::new( | |
bfmt![ fg["#ffffff"] bg["#111111"] text["|"] ] | |
)); | |
} | |
} | |
formats.push(Box::new(sep.right(FG_WINGS, BG_WINGS))); | |
formats.push(Box::new({ | |
bfmt![ fg[FG_WINGS] bg[BG_WINGS] fmt[" {} ", layout]] | |
})); | |
Format::Concat(formats) | |
} | |
)) | |
/* Right */ | |
.add(Text::new(bfmt![right])) | |
.add(Text::new(sep.left("#ffffff", BGS[3]))) | |
.add(Periodic::new( | |
Duration::from_secs(2), | |
move || match System::new().memory() { | |
Ok(mem) => bfmt![fg[sep.fg] bg[sep.bg] fmt[" {}/{} RAM ", as_string_gb(mem.total - mem.free), mem.total]], | |
Err(_) => bfmt![fg["#bb1155"] bg[sep.bg] text["error"]], | |
})) | |
.add(Delayed::new( | |
Duration::from_secs(1), | |
|| System::new().cpu_load_aggregate().unwrap(), | |
|res| match res { | |
Ok(cpu) => bfmt![fg["#99aaff"] fmt[" {:.1}% CPU ", (1.0 - cpu.idle) * 100.0]], | |
Err(_) => bfmt![fg["#bb1155"] text["error"]], | |
})) | |
.add(Text::new(sep.left("#ffffff", BGS[2]))) | |
.add(Wrap::new( | |
move |f| bfmt![fg[sep.fg] bg[sep.bg] f], | |
DateTime::new(" %H:%M:%S ")) | |
) | |
.add(Text::new(sep.left("#ffffff", BGS[1]))) | |
.add(Wrap::new( | |
move |f| bfmt![fg[sep.fg] bg[sep.bg] f], | |
DateTime::new(" %W. %A ")) // week, day | |
) | |
.add(Text::new(sep.left("#ffffff", BGS[0]))) | |
.add(Wrap::new( | |
move |f| bfmt![fg[sep.fg] bg[sep.bg] f], | |
DateTime::new(" %d. %B ")) // date, month | |
) | |
.run(); | |
} | |
fn as_string_gb(bytes: ByteSize) -> String { | |
format!("{}", (bytes.as_usize()/1000000) as f32 / 1000.0) | |
} | |
#[derive(Copy,Clone)] // Copy,Clone to be used within closures | |
struct Separator { | |
fg: &'static str, | |
bg: &'static str, | |
} | |
impl Separator { | |
pub fn new(fg: &'static str, bg: &'static str) -> Separator { | |
Separator { | |
fg: fg, | |
bg: bg, | |
} | |
} | |
pub fn left(&mut self, fg: &'static str, bg: &'static str) -> Format { | |
let ret = bfmt![ | |
fg[bg] bg[self.bg] text[" ⮃⮂"] | |
]; | |
self.fg = fg; | |
self.bg = bg; | |
ret | |
} | |
pub fn right(&mut self, fg: &'static str, bg: &'static str) -> Format { | |
let ret = bfmt![ | |
fg[self.bg] bg[bg] text["⮀⮁ "] | |
]; | |
self.fg = fg; | |
self.bg = bg; | |
ret | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its not working for me. Any solution ? :(