From b39eac91a5591d3c4e274a9d7ec43f9ff577b9bc Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 18 Jul 2021 16:44:56 +0200 Subject: [PATCH] Fix battery widget charging/discharging state and use direct io instead of cat command --- simple/widgets/system_resources.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/simple/widgets/system_resources.lua b/simple/widgets/system_resources.lua index 2d02f57..2996800 100644 --- a/simple/widgets/system_resources.lua +++ b/simple/widgets/system_resources.lua @@ -51,6 +51,13 @@ function readCommand(command) return r end +function readFile(file) + local handle = io.open(file, "r") + local r = handle:read("*a") + handle:close() + return r +end + local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1] function getCpuData() @@ -144,13 +151,13 @@ function getBatteryLevel(battery) local charge = 0 local capacity = 0 if fileExists(battery .. "/energy_now") then - charge = charge + readCommand("cat " .. battery .. "/energy_now") - capacity = capacity + readCommand("cat " .. battery .. "/energy_full") + charge = charge + readFile(battery .. "/energy_now") + capacity = capacity + readFile(battery .. "/energy_full") elseif fileExists(battery .. "/charge_now") then - charge = charge + readCommand("cat " .. battery .. "/charge_now") - capacity = capacity + readCommand("cat " .. battery .. "/charge_full") + charge = charge + readFile(battery .. "/charge_now") + capacity = capacity + readFile(battery .. "/charge_full") elseif fileExists(battery .. "/capacity_level") then - return readCommand("cat " .. battery .. "/capacity_level") + return readFile(battery .. "/capacity_level") else return "Unsupported" end @@ -158,7 +165,7 @@ function getBatteryLevel(battery) end function isCharging(battery) - return not readCommand("cat " .. battery .. "/status") == "Discharging" + return readFile(battery .. "/status") == "Charging\n" end @@ -185,7 +192,7 @@ function refreshBatteries() for battery in rawBatteries:gmatch("([^\n]+)") do local path = "/sys/class/power_supply/" .. battery - if readCommand("cat " .. path .. "/type"):match("Battery") then + if readFile(path .. "/type"):match("Battery") then -- widget local widget = wibox.widget { { @@ -199,7 +206,7 @@ function refreshBatteries() } -- tooltip - local name = readCommand("cat " .. path .. "/model_name") + local name = readFile(path .. "/model_name") local tooltip = awful.tooltip { text = name }