forked from ashpie/simple-awesome
Refresh battery list on change checked every 2s (configurable)
This commit is contained in:
parent
00533c13f6
commit
ecbf661115
@ -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
|
||||
},
|
||||
},
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user