getting battery percentage programmatically (#50)

* getting battery percentage programmatically

* cargo.lock edit
This commit is contained in:
Mustafa Salih 2021-04-29 21:59:59 +03:00 committed by GitHub
parent cc0c16a3d0
commit 6b92cecef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 33 deletions

67
Cargo.lock generated
View File

@ -95,6 +95,23 @@ version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "battery"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4b624268937c0e0a3edb7c27843f9e547c320d730c610d3b8e6e8e95b2026e4"
dependencies = [
"cfg-if 1.0.0",
"core-foundation",
"lazycell",
"libc",
"mach",
"nix 0.19.1",
"num-traits",
"uom",
"winapi",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -200,6 +217,22 @@ dependencies = [
"cache-padded", "cache-padded",
] ]
[[package]]
name = "core-foundation"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d24c7a13c43e870e37c1556b74555437870a04514f7685f5b354e090567171"
dependencies = [
"core-foundation-sys",
"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 = "cty" name = "cty"
version = "0.2.1" version = "0.2.1"
@ -303,6 +336,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.84" version = "0.2.84"
@ -333,6 +372,15 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
] ]
[[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.3.4" version = "2.3.4"
@ -496,9 +544,10 @@ dependencies = [
[[package]] [[package]]
name = "rsblocks" name = "rsblocks"
version = "0.1.13" version = "0.1.14"
dependencies = [ dependencies = [
"alsa", "alsa",
"battery",
"breadx", "breadx",
"chrono", "chrono",
"dbus", "dbus",
@ -631,6 +680,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "typenum"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"
@ -643,6 +698,16 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[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 = "vec-arena" name = "vec-arena"
version = "1.0.0" version = "1.0.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rsblocks" name = "rsblocks"
version = "0.1.13" version = "0.1.14"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"] authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -20,7 +20,7 @@ mpd = "0.0.12"
nix = "0.19.1" nix = "0.19.1"
serde = {version = "1.0.120", features = ["derive"]} serde = {version = "1.0.120", features = ["derive"]}
serde_yaml = "0.8.15" serde_yaml = "0.8.15"
battery = "0.7"
[dependencies.breadx] [dependencies.breadx]
version = "0.1.11" version = "0.1.11"

View File

@ -13,8 +13,8 @@ mod uptime;
mod volume; mod volume;
mod weather; mod weather;
use self::battery::Battery;
use self::mpd::Mpd; use self::mpd::Mpd;
use battery::Battery;
use bitcoins::BitCoins; use bitcoins::BitCoins;
use cputemp::CpuTemp; use cputemp::CpuTemp;
use disk::Disk; use disk::Disk;

View File

@ -1,37 +1,32 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::types::ThreadsData; use crate::types::ThreadsData;
use std::fs::read_to_string; use battery::Manager;
// TODO: better error handeling
// getting battery percentage // getting battery percentage
pub fn get_battery() -> ThreadsData { pub fn get_battery() -> ThreadsData {
let error = ThreadsData::Battery(String::from("check your battery source name")); let battery_manager = if let Ok(manager) = Manager::new() {
manager
let battery_full_cap_file = format!( } else {
"/sys/class/power_supply/{}/charge_full_design", return ThreadsData::Battery(String::from("Cannot Create Battery Manager!"));
CONFIG.battery.source
);
let battery_charge_now_file = format!(
"/sys/class/power_supply/{}/charge_now",
CONFIG.battery.source
);
let buf = match read_to_string(battery_full_cap_file) {
Ok(file) => file,
Err(_) => return error,
};
let full_design = buf.trim().parse::<u32>().unwrap();
let buf = match read_to_string(&battery_charge_now_file) {
Ok(data) => data,
_ => return error,
}; };
let charge_now = buf.trim().parse::<u32>().unwrap(); let mut batteries = if let Ok(batteries) = battery_manager.batteries() {
batteries
} else {
return ThreadsData::Battery(String::from("Cannot Get Battery!"));
};
let percentage = if let Some(battery) = batteries.next() {
f32::from(battery.unwrap().state_of_charge()) * 100.0
} else {
return ThreadsData::Battery(String::from("Cannot Read Battery!"));
};
let battery_percentage = (charge_now as f32 / full_design as f32) * 100.0;
let result = format!( let result = format!(
" {} {:.0}% {}", " {} {:.0}% {}",
CONFIG.battery.icon, battery_percentage, CONFIG.seperator CONFIG.battery.icon, percentage, CONFIG.seperator
); );
ThreadsData::Battery(result) ThreadsData::Battery(result)
} }

View File

@ -2,7 +2,6 @@ use crate::config::CONFIG;
use crate::types::ThreadsData; use crate::types::ThreadsData;
use mpd::{Client, Song}; use mpd::{Client, Song};
// yes, error handling looks fucking sucks!
// getting mpd song file // getting mpd song file
pub fn get_mpd_current() -> ThreadsData { pub fn get_mpd_current() -> ThreadsData {
let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port); let stream_path = format!("{}:{}", CONFIG.mpd.host, CONFIG.mpd.port);
@ -12,10 +11,7 @@ pub fn get_mpd_current() -> ThreadsData {
_ => return empty_data, _ => return empty_data,
}; };
let current: Song = match conn.currentsong() { let current: Song = match conn.currentsong() {
Ok(opt) => match opt { Ok(Some(song)) => song,
Some(song) => song,
_ => return empty_data,
},
_ => return empty_data, _ => return empty_data,
}; };