From 24f432fb67e5ba3cadddf5084b60c15e392f5e44 Mon Sep 17 00:00:00 2001 From: Max Verevkin Date: Mon, 14 Mar 2022 22:26:57 +0200 Subject: [PATCH] Try to fix #1445 I hope that it's not just my machine and `/sys/class/power_supply/<...>/present` always has a value of "1" for present batteries. --- src/blocks/battery.rs | 6 +++++- src/util.rs | 15 +++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/blocks/battery.rs b/src/blocks/battery.rs index d76abbe074..2f54c7583f 100644 --- a/src/blocks/battery.rs +++ b/src/blocks/battery.rs @@ -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<()> { diff --git a/src/util.rs b/src/util.rs index e93d11a8d1..90fc126238 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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 { - 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) -> Result { + 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();