Battery bar module

main
Tassilo Horn 3 years ago
parent bf9337571a
commit 5d1fbeba06
  1. 85
      Cargo.lock
  2. 19
      Cargo.toml
  3. 1
      src/bar.rs
  4. 1
      src/bar/module.rs
  5. 110
      src/bar/module/battery.rs
  6. 2
      src/bar/module/sysinfo.rs

85
Cargo.lock generated

@ -28,12 +28,35 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "battery"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4b624268937c0e0a3edb7c27843f9e547c320d730c610d3b8e6e8e95b2026e4"
dependencies = [
"cfg-if",
"core-foundation",
"lazycell",
"libc",
"mach",
"nix",
"num-traits",
"uom",
"winapi",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -83,6 +106,22 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "core-foundation"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171"
dependencies = [
"core-foundation-sys 0.7.0",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac"
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.3" version = "0.8.3"
@ -232,6 +271,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.121" version = "0.2.121"
@ -247,6 +292,15 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "mach"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.4.1" version = "2.4.1"
@ -262,6 +316,18 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "nix"
version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2"
dependencies = [
"bitflags",
"cc",
"cfg-if",
"libc",
]
[[package]] [[package]]
name = "ntapi" name = "ntapi"
version = "0.3.7" version = "0.3.7"
@ -550,6 +616,7 @@ dependencies = [
name = "swayr" name = "swayr"
version = "0.16.1" version = "0.16.1"
dependencies = [ dependencies = [
"battery",
"chrono", "chrono",
"clap", "clap",
"directories", "directories",
@ -585,7 +652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f3c0db8e08e06cfd352a043bd0143498fb7d42783a6e941da83b55464eb27d2" checksum = "5f3c0db8e08e06cfd352a043bd0143498fb7d42783a6e941da83b55464eb27d2"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"core-foundation-sys", "core-foundation-sys 0.8.3",
"libc", "libc",
"ntapi", "ntapi",
"once_cell", "once_cell",
@ -647,12 +714,28 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "uom"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e76503e636584f1e10b9b3b9498538279561adcef5412927ba00c2b32c4ce5ed"
dependencies = [
"num-traits",
"typenum",
]
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

@ -10,21 +10,22 @@ edition = "2018"
exclude = ["misc/"] exclude = ["misc/"]
[dependencies] [dependencies]
serde = { version = "1.0.126", features = ["derive"] } battery = "0.7.8"
serde_json = "1.0.64" chrono = "0.4"
clap = {version = "3.0.0", features = ["derive"] } clap = {version = "3.0.0", features = ["derive"] }
swayipc = "3.0.0"
toml = "0.5.8"
directories = "4.0" directories = "4.0"
regex = "1.5.5" env_logger = { version = "0.9.0", default-features = false, features = ["termcolor", "atty", "humantime"] } # without regex
log = "0.4"
once_cell = "1.10.0"
rand = "0.8.4" rand = "0.8.4"
regex = "1.5.5"
rt-format = "0.3.0" rt-format = "0.3.0"
log = "0.4" serde = { version = "1.0.126", features = ["derive"] }
env_logger = { version = "0.9.0", default-features = false, features = ["termcolor", "atty", "humantime"] } # without regex serde_json = "1.0.64"
swaybar-types = "3.0.0" swaybar-types = "3.0.0"
chrono = "0.4" swayipc = "3.0.0"
sysinfo = "0.23" sysinfo = "0.23"
once_cell = "1.10.0" toml = "0.5.8"
[profile.release] [profile.release]
lto = "thin" lto = "thin"

@ -30,6 +30,7 @@ pub fn start() {
let mods: Vec<Box<dyn BarModuleFn>> = vec![ let mods: Vec<Box<dyn BarModuleFn>> = vec![
crate::bar::module::window::BarModuleWindow::init(), crate::bar::module::window::BarModuleWindow::init(),
crate::bar::module::sysinfo::BarModuleSysInfo::init(), crate::bar::module::sysinfo::BarModuleSysInfo::init(),
crate::bar::module::battery::BarModuleBattery::init(),
crate::bar::module::date::BarModuleDate::init(), crate::bar::module::date::BarModuleDate::init(),
]; ];
generate_status(&mods); generate_status(&mods);

@ -15,6 +15,7 @@
use swaybar_types as s; use swaybar_types as s;
pub mod battery;
pub mod date; pub mod date;
pub mod sysinfo; pub mod sysinfo;
pub mod window; pub mod window;

@ -0,0 +1,110 @@
// Copyright (C) 2022 Tassilo Horn <tsdh@gnu.org>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//! The date `swayrbar` module.
use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace;
use battery as bat;
use std::cell::RefCell;
use swaybar_types as s;
pub struct BarModuleBattery {
pub instance: String,
manager: RefCell<bat::Manager>,
}
fn get_refreshed_batteries(
manager: &RefCell<bat::Manager>,
) -> Result<Vec<bat::Battery>, bat::Error> {
let m = manager.borrow();
let mut bats = vec![];
for bat in m.batteries()? {
let mut bat = bat?;
if m.refresh(&mut bat).is_ok() {
bats.push(bat);
}
}
Ok(bats)
}
fn get_text(manager: &RefCell<bat::Manager>, fmt: &str) -> String {
match get_refreshed_batteries(manager) {
Ok(bats) => {
fmt_replace!(&fmt, false, {
"state_of_charge" => bats.iter()
.map(|b| b.state_of_charge().value)
.sum::<f32>()
/ bats.len() as f32 * 100 as f32,
"state_of_health" => bats.iter()
.map(|b| b.state_of_health().value)
.sum::<f32>()
/ bats.len() as f32 * 100 as f32,
"state" => bats.iter()
.map(|b| format!("{:?}", b.state()))
.next()
.unwrap_or(String::new()),
})
}
Err(err) => format!("{}", err),
}
}
impl BarModuleFn for BarModuleBattery {
fn init() -> Box<dyn BarModuleFn> {
Box::new(BarModuleBattery {
instance: "0".to_string(),
manager: RefCell::new(
bat::Manager::new().expect("Could not create Manager"),
),
})
}
fn name() -> String {
String::from("battery")
}
fn instance(&self) -> String {
self.instance.clone()
}
fn build(&self) -> s::Block {
let fmt =
"🔋 Bat: {state_of_charge:{:5.1}}% {state} Health: {state_of_health:{:5.1}}%";
let s = get_text(&self.manager, fmt);
s::Block {
name: Some(Self::name()),
instance: Some(self.instance.clone()),
full_text: s,
align: Some(s::Align::Right),
markup: Some(s::Markup::Pango),
short_text: None,
color: None,
background: None,
border: None,
border_top: None,
border_bottom: None,
border_left: None,
border_right: None,
min_width: None,
urgent: None,
separator: Some(true),
separator_block_width: None,
}
}
}

@ -115,7 +115,7 @@ impl BarModuleFn for BarModuleSysInfo {
"load_avg_15" => get_load_average(&self.system, "load_avg_15" => get_load_average(&self.system,
LoadAvg::Fifteen, &updater), LoadAvg::Fifteen, &updater),
}), }),
align: Some(s::Align::Right), align: Some(s::Align::Left),
markup: Some(s::Markup::Pango), markup: Some(s::Markup::Pango),
short_text: None, short_text: None,
color: None, color: None,

Loading…
Cancel
Save