Refresh battery list on change checked every 2s (configurable)

This commit is contained in:
Alice Gaudon 2021-07-18 16:03:22 +02:00
parent 81dc1817f5
commit ea20dae2b7
2 changed files with 65 additions and 46 deletions

View File

@ -171,7 +171,8 @@ config.widgets = {
battery = { battery = {
enabled = true, enabled = true,
screens = { 1, 2 }, screens = { 1, 2 },
refresh_interval = 30, -- In seconds refresh_interval = 15, -- In seconds
list_refresh_interval = 2, -- In seconds
precision = 0, -- How many decimals precision = 0, -- How many decimals
}, },
}, },

View File

@ -135,14 +135,9 @@ gears.timer {
--- ---
--- Batteries --- Batteries
--- ---
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply") rawBatteries = {}
function refreshRawBatteries()
function getBatteries() rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
return rawBatteries:gmatch("([^\n]+)")
end
function hasBattery()
return rawBatteries:len() > 0
end end
function getBatteryLevel(battery) function getBatteryLevel(battery)
@ -170,48 +165,71 @@ end
local batteryContainer = wibox.widget { local batteryContainer = wibox.widget {
layout = wibox.layout.flex.horizontal layout = wibox.layout.flex.horizontal
} }
if hasBattery() then local timers = {}
for battery in getBatteries() do function refreshBatteries()
local path = "/sys/class/power_supply/" .. battery -- refresh raw batteries and check for change
if readCommand("cat " .. path .. "/type"):match("Battery") then local oldBatteries = rawBatteries
-- widget refreshRawBatteries()
local widget = wibox.widget { if rawBatteries == oldBatteries then
{ return
markup = "-- %", end
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
-- tooltip -- clear timers
local name = readCommand("cat " .. path .. "/model_name") for i,v in ipairs(timers) do
local tooltip = awful.tooltip { v:stop()
text = name end
} timers = {};
tooltip:add_to_object(widget)
-- update battery level -- clear layout
local batteryText = widget:get_children()[1] batteryContainer:set_children({})
gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
end
}
batteryContainer:add(widget) for battery in rawBatteries:gmatch("([^\n]+)") do
end local path = "/sys/class/power_supply/" .. battery
end if readCommand("cat " .. path .. "/type"):match("Battery") then
else -- widget
batteryContainer = nil local widget = wibox.widget {
{
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
-- tooltip
local name = readCommand("cat " .. path .. "/model_name")
local tooltip = awful.tooltip {
text = name
}
tooltip:add_to_object(widget)
-- update battery level
local batteryText = widget:get_children()[1]
local timer = gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
end
}
table.insert(timers, timer)
batteryContainer:add(widget)
end
end
end end
gears.timer {
timeout = config.battery.list_refresh_interval,
call_now = true,
autostart = true,
callback = function()
refreshBatteries()
end
}
return { return {
bars = bars, bars = bars,