Fix battery widget charging/discharging state and use direct io instead of cat command

This commit is contained in:
Alice Gaudon 2021-07-18 16:44:56 +02:00
parent 2d79eb8a5e
commit b39eac91a5
1 changed files with 15 additions and 8 deletions

View File

@ -51,6 +51,13 @@ function readCommand(command)
return r return r
end 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] local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData() function getCpuData()
@ -144,13 +151,13 @@ function getBatteryLevel(battery)
local charge = 0 local charge = 0
local capacity = 0 local capacity = 0
if fileExists(battery .. "/energy_now") then if fileExists(battery .. "/energy_now") then
charge = charge + readCommand("cat " .. battery .. "/energy_now") charge = charge + readFile(battery .. "/energy_now")
capacity = capacity + readCommand("cat " .. battery .. "/energy_full") capacity = capacity + readFile(battery .. "/energy_full")
elseif fileExists(battery .. "/charge_now") then elseif fileExists(battery .. "/charge_now") then
charge = charge + readCommand("cat " .. battery .. "/charge_now") charge = charge + readFile(battery .. "/charge_now")
capacity = capacity + readCommand("cat " .. battery .. "/charge_full") capacity = capacity + readFile(battery .. "/charge_full")
elseif fileExists(battery .. "/capacity_level") then elseif fileExists(battery .. "/capacity_level") then
return readCommand("cat " .. battery .. "/capacity_level") return readFile(battery .. "/capacity_level")
else else
return "Unsupported" return "Unsupported"
end end
@ -158,7 +165,7 @@ function getBatteryLevel(battery)
end end
function isCharging(battery) function isCharging(battery)
return not readCommand("cat " .. battery .. "/status") == "Discharging" return readFile(battery .. "/status") == "Charging\n"
end end
@ -185,7 +192,7 @@ function refreshBatteries()
for battery in rawBatteries:gmatch("([^\n]+)") do for battery in rawBatteries:gmatch("([^\n]+)") do
local path = "/sys/class/power_supply/" .. battery local path = "/sys/class/power_supply/" .. battery
if readCommand("cat " .. path .. "/type"):match("Battery") then if readFile(path .. "/type"):match("Battery") then
-- widget -- widget
local widget = wibox.widget { local widget = wibox.widget {
{ {
@ -199,7 +206,7 @@ function refreshBatteries()
} }
-- tooltip -- tooltip
local name = readCommand("cat " .. path .. "/model_name") local name = readFile(path .. "/model_name")
local tooltip = awful.tooltip { local tooltip = awful.tooltip {
text = name text = name
} }