update API to match awesome-fswidget

This commit is contained in:
Izaya 2022-09-12 00:44:30 +10:00
parent 897cce3241
commit 23a1252011

View File

@ -62,7 +62,7 @@ local function getBatteryState(path)
percent = tonumber(f:read("*a"):match("%S+")) percent = tonumber(f:read("*a"):match("%S+"))
f:close() f:close()
end end
return state, percent return state, tonumber(percent)
end end
function batterystat.scan() function batterystat.scan()
@ -78,40 +78,45 @@ function batterystat.scan()
end end
end end
local function bwidgetUpdate(self) local function updateBatteryWidget(self)
self:reset() local state, percent = getBatteryState(self.path)
for k,v in pairs(batterystat.batteries) do local colour = (state == "Charging") and "lime" or "red"
local name = v:match("/([^/]+)/$") self:set(1,icons[chooseIcon(state, percent)])
local state, percent = getBatteryState(v) self.children[3].markup = string.format('<span foreground="%s" size="x-large">%d%%</span>',colour,percent)
local colour = (state == "Charging") and "lime" or "red"
local sbwidget = (wibox.widget{
layout = wibox.layout.stack,
forced_width = batterystat.iconSize,
icons[chooseIcon(state,percent)],
wibox.widget{
widget = wibox.widget.textbox,
align = "center",
valign = "top",
text = name
},
wibox.widget{
widget = wibox.widget.textbox,
align = "right",
valign = "bottom",
markup = string.format('<span foreground="%s" size="x-large">%d%%</span>',colour,percent)
}
})
self:add(sbwidget)
end
end end
function batterystat.new() function batterystat.new(path)
local bwidget = wibox.widget{ local state, percent = getBatteryState(path)
layout = wibox.layout.grid.horizontal, local name = path:match("/([^/]+)/$")
update = bwidgetUpdate local sbwidget = wibox.widget{
layout = wibox.layout.stack,
forced_width = batterystat.iconSize,
path = path,
update = updateBatteryWidget,
icons[chooseIcon(state,percent)],
wibox.widget{
widget = wibox.widget.textbox,
align = "center",
valign = "top",
text = name
},
wibox.widget{
widget = wibox.widget.textbox,
align = "right",
valign = "bottom",
}
} }
sbwidget:update()
return sbwidget
end
function batterystat.all()
local rt = {}
batterystat.scan() batterystat.scan()
return bwidget for k,v in pairs(batterystat.batteries) do
rt[#rt+1] = batterystat.new(v)
end
return rt
end end
return batterystat return batterystat