Skip to content

Commit

Permalink
Try to fix #1445
Browse files Browse the repository at this point in the history
I hope that it's not just my machine and
`/sys/class/power_supply/<...>/present` always has a value of "1" for
present batteries.
  • Loading branch information
MaxVerevkin committed Mar 14, 2022
1 parent e7e2836 commit 24f432f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/blocks/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ impl PowerSupplyDevice {

impl BatteryDevice for PowerSupplyDevice {
fn is_available(&self) -> bool {
self.device_path.exists()
let path = self.device_path.join("present");
if !path.exists() {
return false;
}
read_file("battery", path).map_or(false, |x| x == "1")
}

fn refresh_device_info(&mut self) -> Result<()> {
Expand Down
15 changes: 9 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,18 @@ where
toml::from_str(&contents).configuration_error("failed to parse TOML from file contents")
}

pub fn read_file(blockname: &str, path: &Path) -> Result<String> {
let mut f = OpenOptions::new().read(true).open(path).block_error(
blockname,
&format!("failed to open file {}", path.to_string_lossy()),
)?;
pub fn read_file(blockname: &str, path: impl AsRef<Path>) -> Result<String> {
let mut f = OpenOptions::new()
.read(true)
.open(path.as_ref())
.block_error(
blockname,
&format!("failed to open file {}", path.as_ref().display()),
)?;
let mut content = String::new();
f.read_to_string(&mut content).block_error(
blockname,
&format!("failed to read {}", path.to_string_lossy()),
&format!("failed to read {}", path.as_ref().display()),
)?;
// Removes trailing newline
content.pop();
Expand Down

0 comments on commit 24f432f

Please sign in to comment.