From 23a125201121136185dac3b9ad04bfa5d7324812 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 12 Sep 2022 00:44:30 +1000 Subject: [PATCH] update API to match awesome-fswidget --- init.lua | 67 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/init.lua b/init.lua index 3102677..2fbed86 100644 --- a/init.lua +++ b/init.lua @@ -62,7 +62,7 @@ local function getBatteryState(path) percent = tonumber(f:read("*a"):match("%S+")) f:close() end - return state, percent + return state, tonumber(percent) end function batterystat.scan() @@ -78,40 +78,45 @@ function batterystat.scan() end end -local function bwidgetUpdate(self) - self:reset() - for k,v in pairs(batterystat.batteries) do - local name = v:match("/([^/]+)/$") - local state, percent = getBatteryState(v) - 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('%d%%',colour,percent) - } - }) - self:add(sbwidget) - end +local function updateBatteryWidget(self) + local state, percent = getBatteryState(self.path) + local colour = (state == "Charging") and "lime" or "red" + self:set(1,icons[chooseIcon(state, percent)]) + self.children[3].markup = string.format('%d%%',colour,percent) end -function batterystat.new() - local bwidget = wibox.widget{ - layout = wibox.layout.grid.horizontal, - update = bwidgetUpdate +function batterystat.new(path) + local state, percent = getBatteryState(path) + local name = path:match("/([^/]+)/$") + 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() - return bwidget + for k,v in pairs(batterystat.batteries) do + rt[#rt+1] = batterystat.new(v) + end + return rt end return batterystat