summaryrefslogtreecommitdiff
path: root/battery-widget
diff options
context:
space:
mode:
authorSam Delmerico <svdelmerico@gmail.com>2018-06-16 00:20:30 -0400
committerSam Delmerico <svdelmerico@gmail.com>2018-06-16 00:20:30 -0400
commitbfe818b11e3a15fefc1b76383a7e1b5e1f7827ff (patch)
tree1da601306423ab137b87dc4d746556fa197dc9cd /battery-widget
parent70a82b7fd47bde2ca1107419c78b6c0f023c4cea (diff)
battery widgets support multiple batteries
Added support for the battery widgets to read multiple battery values from the acpi output. The total charge becomes average of all of the available batteries, the status is that of the highest battery.
Diffstat (limited to 'battery-widget')
-rw-r--r--battery-widget/battery.lua28
1 files changed, 25 insertions, 3 deletions
diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua
index 01a3ddd..b8e70bb 100644
--- a/battery-widget/battery.lua
+++ b/battery-widget/battery.lua
@@ -67,14 +67,34 @@ local function show_battery_warning()
}
end
+local last_battery_check = os.time()
+
watch("acpi", 10,
function(widget, stdout, stderr, exitreason, exitcode)
local batteryType
- local _, status, charge_str, time = string.match(stdout, '(.+): (%a+), (%d?%d?%d)%%,? ?.*')
- local charge = tonumber(charge_str)
+
+ local battery_info = {}
+ for s in stdout:gmatch("[^\r\n]+") do
+ local _, status, charge_str, time = string.match(s, '(.+): (%a+), (%d?%d?%d)%%,? ?.*')
+ table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
+ end
+
+ local charge = 0
+ local status
+ for i, batt in ipairs(battery_info) do
+ if batt.charge >= charge then
+ status = batt.status -- use most charged battery status
+ -- this is arbitrary, and maybe another metric should be used
+ end
+
+ charge = charge + batt.charge
+ end
+ charge = charge // #battery_info -- use average charge for battery icon
+
if (charge >= 0 and charge < 15) then
batteryType = "battery-empty%s-symbolic"
- if status ~= 'Charging' then
+ if status ~= 'Charging' and os.difftime(os.time(), last_battery_check) > 300 then
+ -- if 5 minutes have elapsed since the last warning
show_battery_warning()
end
elseif (charge >= 15 and charge < 40) then batteryType = "battery-caution%s-symbolic"
@@ -82,11 +102,13 @@ watch("acpi", 10,
elseif (charge >= 60 and charge < 80) then batteryType = "battery-good%s-symbolic"
elseif (charge >= 80 and charge <= 100) then batteryType = "battery-full%s-symbolic"
end
+
if status == 'Charging' then
batteryType = string.format(batteryType, '-charging')
else
batteryType = string.format(batteryType, '')
end
+
widget.icon:set_image(PATH_TO_ICONS .. batteryType .. ".svg")
-- Update popup text