svartalf / rust-battery

Rust crate providing cross-platform information about the notebook batteries.
https://crates.io/crates/battery
Apache License 2.0
354 stars 40 forks source link

Handle missing energy_full_design value for Linux #40

Closed svartalf closed 4 years ago

svartalf commented 4 years ago

According to https://github.com/starship/starship/issues/613#issuecomment-548873632, there exist a possibility when both energy_full_design and charge_full_design files are missing for the battery device, so fetching fails with an error here: https://github.com/svartalf/rust-battery/blob/08b2ad579d135dbe48d52ff31a35df307c173eb6/battery/src/platform/linux/sysfs/source.rs#L183

upower just falls back to 0.0 value, but that might affect state_of_health

https://github.com/svartalf/rust-battery/blob/08b2ad579d135dbe48d52ff31a35df307c173eb6/battery/src/platform/linux/sysfs/source.rs#L137

and energy parameters:

https://github.com/svartalf/rust-battery/blob/08b2ad579d135dbe48d52ff31a35df307c173eb6/battery/src/platform/linux/sysfs/source.rs#L168

svartalf commented 4 years ago

@pizzafox, I dropped a quick fix, would you be able to test it with the starship of yours?

You can do that by adding the following lines at the end of the Cargo.toml file and rebuilding the starship project:

[patch.crates-io]
battery = { git = "https://github.com/svartalf/rust-battery.git", branch = "issue-40" }

Or with the patch:

diff --git a/Cargo.toml b/Cargo.toml
index 2b5ee64..60c16ca 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -57,3 +57,6 @@ lto = true
 [[bin]]
 name = "starship"
 path = "src/main.rs"
+
+[patch.crates-io]
+battery = { git = "https://github.com/svartalf/rust-battery.git", branch = "issue-40" }
jonahsnider commented 4 years ago

This fixes the issue! The battery is displaying properly in Starship.

jonahsnider commented 4 years ago

I just upgraded to WSL 2 which has a much better integration to the system APIs... Including battery!

This bug is no longer occurring on for me despite not upgrading rust-battery to a patched version.

svartalf commented 4 years ago

@pizzafox thanks for an update, good to know!