From ecbf6611154ebdee23a06188dbf30b437d786110 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 18 Jul 2021 16:03:22 +0200 Subject: [PATCH] Refresh battery list on change checked every 2s (configurable) --- default_config.lua | 3 +- simple/widgets/system_resources.lua | 108 ++++++++++++++++------------ 2 files changed, 65 insertions(+), 46 deletions(-) diff --git a/default_config.lua b/default_config.lua index aac34f4..611d971 100644 --- a/default_config.lua +++ b/default_config.lua @@ -171,7 +171,8 @@ config.widgets = { battery = { enabled = true, screens = { 1, 2 }, - refresh_interval = 30, -- In seconds + refresh_interval = 15, -- In seconds + list_refresh_interval = 2, -- In seconds precision = 0, -- How many decimals }, }, diff --git a/simple/widgets/system_resources.lua b/simple/widgets/system_resources.lua index 08b0a3d..3e75830 100644 --- a/simple/widgets/system_resources.lua +++ b/simple/widgets/system_resources.lua @@ -135,14 +135,9 @@ gears.timer { --- --- Batteries --- -local rawBatteries = readCommand("ls -A1 /sys/class/power_supply") - -function getBatteries() - return rawBatteries:gmatch("([^\n]+)") -end - -function hasBattery() - return rawBatteries:len() > 0 +rawBatteries = {} +function refreshRawBatteries() + rawBatteries = readCommand("ls -A1 /sys/class/power_supply") end function getBatteryLevel(battery) @@ -170,48 +165,71 @@ end local batteryContainer = wibox.widget { layout = wibox.layout.flex.horizontal } -if hasBattery() then - for battery in getBatteries() do - local path = "/sys/class/power_supply/" .. battery - if readCommand("cat " .. path .. "/type"):match("Battery") then - -- widget - local widget = wibox.widget { - { - markup = "-- %", - align = "center", - valign = "center", - widget = wibox.widget.textbox, - }, - margins = beautiful.system_resources_widget_battery_margin, - widget = wibox.container.margin, - } +local timers = {} +function refreshBatteries() + -- refresh raw batteries and check for change + local oldBatteries = rawBatteries + refreshRawBatteries() + if rawBatteries == oldBatteries then + return + end - -- tooltip - local name = readCommand("cat " .. path .. "/model_name") - local tooltip = awful.tooltip { - text = name - } - tooltip:add_to_object(widget) + -- clear timers + for i,v in ipairs(timers) do + v:stop() + end + timers = {}; - -- update battery level - local batteryText = widget:get_children()[1] - gears.timer { - timeout = config.battery.refresh_interval, - call_now = true, - autostart = true, - callback = function() - batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path) - end - } + -- clear layout + batteryContainer:set_children({}) - batteryContainer:add(widget) - end - end -else - batteryContainer = nil + for battery in rawBatteries:gmatch("([^\n]+)") do + local path = "/sys/class/power_supply/" .. battery + if readCommand("cat " .. path .. "/type"):match("Battery") then + -- widget + 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 - +gears.timer { + timeout = config.battery.list_refresh_interval, + call_now = true, + autostart = true, + callback = function() + refreshBatteries() + end +} return { bars = bars,