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 1267074193
commit 106294ce3e

View File

@ -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
}