diff --git a/Cargo.lock b/Cargo.lock index 307d034..f8640fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,12 +28,35 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + [[package]] name = "cfg-if" version = "1.0.0" @@ -83,6 +106,22 @@ dependencies = [ "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]] name = "core-foundation-sys" version = "0.8.3" @@ -232,6 +271,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.121" @@ -247,6 +292,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "mach" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.4.1" @@ -262,6 +316,18 @@ dependencies = [ "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]] name = "ntapi" version = "0.3.7" @@ -550,6 +616,7 @@ dependencies = [ name = "swayr" version = "0.16.1" dependencies = [ + "battery", "chrono", "clap", "directories", @@ -585,7 +652,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f3c0db8e08e06cfd352a043bd0143498fb7d42783a6e941da83b55464eb27d2" dependencies = [ "cfg-if", - "core-foundation-sys", + "core-foundation-sys 0.8.3", "libc", "ntapi", "once_cell", @@ -647,12 +714,28 @@ dependencies = [ "serde", ] +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + [[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index dc47626..e34d06c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,21 +10,22 @@ edition = "2018" exclude = ["misc/"] [dependencies] -serde = { version = "1.0.126", features = ["derive"] } -serde_json = "1.0.64" +battery = "0.7.8" +chrono = "0.4" clap = {version = "3.0.0", features = ["derive"] } -swayipc = "3.0.0" -toml = "0.5.8" 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" +regex = "1.5.5" rt-format = "0.3.0" -log = "0.4" -env_logger = { version = "0.9.0", default-features = false, features = ["termcolor", "atty", "humantime"] } # without regex +serde = { version = "1.0.126", features = ["derive"] } +serde_json = "1.0.64" swaybar-types = "3.0.0" -chrono = "0.4" +swayipc = "3.0.0" sysinfo = "0.23" -once_cell = "1.10.0" +toml = "0.5.8" [profile.release] lto = "thin" diff --git a/src/bar.rs b/src/bar.rs index cfdaf36..4b6f4a1 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -30,6 +30,7 @@ pub fn start() { let mods: Vec> = vec![ crate::bar::module::window::BarModuleWindow::init(), crate::bar::module::sysinfo::BarModuleSysInfo::init(), + crate::bar::module::battery::BarModuleBattery::init(), crate::bar::module::date::BarModuleDate::init(), ]; generate_status(&mods); diff --git a/src/bar/module.rs b/src/bar/module.rs index f39c7bb..5d8238f 100644 --- a/src/bar/module.rs +++ b/src/bar/module.rs @@ -15,6 +15,7 @@ use swaybar_types as s; +pub mod battery; pub mod date; pub mod sysinfo; pub mod window; diff --git a/src/bar/module/battery.rs b/src/bar/module/battery.rs new file mode 100644 index 0000000..d3920ac --- /dev/null +++ b/src/bar/module/battery.rs @@ -0,0 +1,110 @@ +// Copyright (C) 2022 Tassilo Horn +// +// 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 . + +//! 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, +} + +fn get_refreshed_batteries( + manager: &RefCell, +) -> Result, 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, 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::() + / bats.len() as f32 * 100 as f32, + "state_of_health" => bats.iter() + .map(|b| b.state_of_health().value) + .sum::() + / 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 { + 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, + } + } +} diff --git a/src/bar/module/sysinfo.rs b/src/bar/module/sysinfo.rs index a0404b5..1bb8845 100644 --- a/src/bar/module/sysinfo.rs +++ b/src/bar/module/sysinfo.rs @@ -115,7 +115,7 @@ impl BarModuleFn for BarModuleSysInfo { "load_avg_15" => get_load_average(&self.system, LoadAvg::Fifteen, &updater), }), - align: Some(s::Align::Right), + align: Some(s::Align::Left), markup: Some(s::Markup::Pango), short_text: None, color: None,