Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
grtcdr committed Aug 12, 2021
2 parents 4a4edde + 37e7743 commit 86667fa
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libmacchina"
version = "0.6.2"
version = "0.6.3"
authors = ["grtcdr <[email protected]>", "Marvin Haschker <[email protected]>"]
edition = "2018"
description = "A library that can fetch all sorts of system information, super-duper fast!"
Expand Down
129 changes: 81 additions & 48 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod sysinfo_ffi;
mod x11_ffi;

use crate::extra;
use crate::extra::list_dir_entries;
use crate::traits::*;
use aparato::{Fetch, PCIDevice};
use itertools::Itertools;
Expand Down Expand Up @@ -44,68 +45,100 @@ impl BatteryReadout for LinuxBatteryReadout {
}

fn percentage(&self) -> Result<u8, ReadoutError> {
let mut bat_path = Path::new("/sys/class/power_supply/BAT0/capacity");
if !Path::exists(bat_path) {
bat_path = Path::new("/sys/class/power_supply/BAT1/capacity");
}

let percentage_text = extra::pop_newline(fs::read_to_string(bat_path)?);
let percentage_parsed = percentage_text.parse::<u8>();

match percentage_parsed {
Ok(p) => Ok(p),
Err(e) => Err(ReadoutError::Other(format!(
"Could not parse the value '{}' of {} into a \
let mut dirs = list_dir_entries(&PathBuf::from("/sys/class/power_supply"));
let index = dirs
.iter()
.position(|f| f.to_string_lossy().contains("ADP"));
if let Some(i) = index {
dirs.remove(i);
}

let bat = dirs.first();
if let Some(b) = bat {
let path_to_capacity = b.join("capacity");
let percentage_text = extra::pop_newline(fs::read_to_string(path_to_capacity)?);
let percentage_parsed = percentage_text.parse::<u8>();

match percentage_parsed {
Ok(p) => return Ok(p),
Err(e) => {
return Err(ReadoutError::Other(format!(
"Could not parse the value '{}' into a \
digit: {:?}",
percentage_text,
bat_path.to_str().unwrap_or_default(),
e
))),
percentage_text, e
)))
}
};
}

Err(ReadoutError::Other(format!("No batteries detected.")))
}

fn status(&self) -> Result<BatteryState, ReadoutError> {
let mut bat_path = Path::new("/sys/class/power_supply/BAT0/status");
if !Path::exists(bat_path) {
bat_path = Path::new("/sys/class/power_supply/BAT1/status");
let mut dirs = list_dir_entries(&PathBuf::from("/sys/class/power_supply"));
let index = dirs
.iter()
.position(|f| f.to_string_lossy().contains("ADP"));
if let Some(i) = index {
dirs.remove(i);
}

let bat = dirs.first();
if let Some(b) = bat {
let path_to_status = b.join("status");
let status_text =
extra::pop_newline(fs::read_to_string(path_to_status)?).to_lowercase();

match &status_text[..] {
"charging" => return Ok(BatteryState::Charging),
"discharging" | "full" => return Ok(BatteryState::Discharging),
s => {
return Err(ReadoutError::Other(format!(
"Got an unexpected value \"{}\" reading battery status",
s,
)))
}
}
}

let status_text = extra::pop_newline(fs::read_to_string(bat_path)?).to_lowercase();
match &status_text[..] {
"charging" => Ok(BatteryState::Charging),
"discharging" | "full" => Ok(BatteryState::Discharging),
s => Err(ReadoutError::Other(format!(
"Got unexpected value '{}' from {}.",
s,
bat_path.to_str().unwrap_or_default()
))),
}
Err(ReadoutError::Other(format!("No batteries detected.")))
}

fn health(&self) -> Result<u64, ReadoutError> {
let mut bat_root = Path::new("/sys/class/power_supply/BAT0");
if !Path::exists(bat_root) {
bat_root = Path::new("/sys/class/power_supply/BAT1");
}
let energy_full =
extra::pop_newline(fs::read_to_string(bat_root.join("energy_full"))?).parse::<u64>();

let energy_full_design =
extra::pop_newline(fs::read_to_string(bat_root.join("energy_full_design"))?)
.parse::<u64>();

match (energy_full, energy_full_design) {
(Ok(mut ef), Ok(efd)) => {
if ef > efd {
ef = efd;
let mut dirs = list_dir_entries(&PathBuf::from("/sys/class/power_supply"));
let index = dirs
.iter()
.position(|f| f.to_string_lossy().contains("ADP"));
if let Some(i) = index {
dirs.remove(i);
}

let bat = dirs.first();
if let Some(b) = bat {
let energy_full =
extra::pop_newline(fs::read_to_string(b.join("energy_full"))?).parse::<u64>();

let energy_full_design =
extra::pop_newline(fs::read_to_string(b.join("energy_full_design"))?)
.parse::<u64>();

match (energy_full, energy_full_design) {
(Ok(mut ef), Ok(efd)) => {
if ef > efd {
ef = efd;
return Ok(((ef as f64 / efd as f64) * 100 as f64) as u64);
}
return Ok(((ef as f64 / efd as f64) * 100 as f64) as u64);
}
Ok(((ef as f64 / efd as f64) * 100 as f64) as u64)
_ => {
return Err(ReadoutError::Other(format!(
"Error calculating battery health.",
)))
}
}
_ => Err(ReadoutError::Other(format!(
"Error while calculating battery health.",
))),
}

Err(ReadoutError::Other(format!("No batteries detected.")))
}
}

Expand Down

0 comments on commit 86667fa

Please sign in to comment.