From 7ed0fc752211522db6070df0ceef4c5b8dfb6c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Hern=C3=A1n=20Tarche?= Date: Sat, 3 Jul 2021 16:41:03 -0300 Subject: [PATCH 1/2] Fixed battery widget opening missing file --- simple/widgets/system_resources.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/simple/widgets/system_resources.lua b/simple/widgets/system_resources.lua index b20592c..f338bcc 100644 --- a/simple/widgets/system_resources.lua +++ b/simple/widgets/system_resources.lua @@ -151,8 +151,13 @@ function getBatteryCapacity() local capacity = 0 for b in getBatteries() do if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then - charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") - capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") + if type(readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now")) == "string" then + charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") + capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full") + else + charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") + capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") + end end end return charge / capacity -- 2.47.0 From 16772da504836b33cb50491e46b19e078b35f08e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Hern=C3=A1n=20Tarche?= Date: Sun, 4 Jul 2021 10:10:25 -0300 Subject: [PATCH 2/2] Added `fileExists()` --- simple/widgets/system_resources.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/simple/widgets/system_resources.lua b/simple/widgets/system_resources.lua index f338bcc..03c2eeb 100644 --- a/simple/widgets/system_resources.lua +++ b/simple/widgets/system_resources.lua @@ -34,6 +34,15 @@ local bars = wibox.widget { layout = wibox.layout.flex.horizontal } +function fileExists(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true + end + return false +end + function readCommand(command) local handle = io.popen(command, "r") local r = handle:read("*a") @@ -151,12 +160,12 @@ function getBatteryCapacity() local capacity = 0 for b in getBatteries() do if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then - if type(readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now")) == "string" then - charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") - capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full") - else + if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") + else + charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") + capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full") end end end -- 2.47.0