summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstreetturtle <streetturtle@users.noreply.github.com>2018-11-26 16:33:03 -0500
committerGitHub <noreply@github.com>2018-11-26 16:33:03 -0500
commit7adf7e7641b6d4a0b621b9ee386c44a2f0ae865b (patch)
treea257f029216862d7525ca7e6e1818982b4a03085
parent1eefac87faefa22b0d3941a9fd4c76010688f59e (diff)
parent2cf217dc6c45f206aef0e065fa3ede4c36d73e3b (diff)
Merge pull request #59 from stnma7e/master
Battery level is an accurate average of multiple batteries with differing capacities
-rw-r--r--battery-widget/battery.lua21
-rw-r--r--batteryarc-widget/batteryarc.lua24
2 files changed, 34 insertions, 11 deletions
diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua
index 54a1399..a472bbf 100644
--- a/battery-widget/battery.lua
+++ b/battery-widget/battery.lua
@@ -69,14 +69,25 @@ end
local last_battery_check = os.time()
-watch("acpi", 10,
+watch("acpi -i", 10,
function(widget, stdout, stderr, exitreason, exitcode)
local batteryType
local battery_info = {}
+ local capacities = {}
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)})
+ local status, charge_str, time = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?.*')
+ if status ~= nil then
+ table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
+ else
+ local cap_str = string.match(s, '.+:.+last full capacity (%d+)')
+ table.insert(capacities, tonumber(cap_str))
+ end
+ end
+
+ local capacity = 0
+ for i, cap in ipairs(capacities) do
+ capacity = capacity + cap
end
local charge = 0
@@ -87,9 +98,9 @@ watch("acpi", 10,
-- this is arbitrary, and maybe another metric should be used
end
- charge = charge + batt.charge
+ charge = charge + batt.charge * capacities[i]
end
- charge = charge / #battery_info -- use average charge for battery icon
+ charge = charge / capacity
if (charge >= 0 and charge < 15) then
batteryType = "battery-empty%s-symbolic"
diff --git a/batteryarc-widget/batteryarc.lua b/batteryarc-widget/batteryarc.lua
index 54b4cf2..6b28c46 100644
--- a/batteryarc-widget/batteryarc.lua
+++ b/batteryarc-widget/batteryarc.lua
@@ -40,14 +40,25 @@ local batteryarc_widget = wibox.container.mirror(batteryarc, { horizontal = true
local last_battery_check = os.time()
-watch("acpi", 10,
+watch("acpi -i", 10,
function(widget, stdout, stderr, exitreason, exitcode)
local batteryType
local battery_info = {}
+ local capacities = {}
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)})
+ local status, charge_str, time = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?.*')
+ if status ~= nil then
+ table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
+ else
+ local cap_str = string.match(s, '.+:.+last full capacity (%d+)')
+ table.insert(capacities, tonumber(cap_str))
+ end
+ end
+
+ local capacity = 0
+ for i, cap in ipairs(capacities) do
+ capacity = capacity + cap
end
local charge = 0
@@ -58,9 +69,9 @@ watch("acpi", 10,
-- this is arbitrary, and maybe another metric should be used
end
- charge = charge + batt.charge
+ charge = charge + batt.charge * capacities[i]
end
- charge = charge / #battery_info -- use average charge for battery icon
+ charge = charge / capacity
widget.value = charge / 100
if status == 'Charging' then
@@ -71,6 +82,8 @@ watch("acpi", 10,
mirrored_text_with_background.fg = beautiful.widget_main_color
end
+ text.text = charge
+
if charge < 15 then
batteryarc.colors = { beautiful.widget_red }
if status ~= 'Charging' and os.difftime(os.time(), last_battery_check) > 300 then
@@ -84,7 +97,6 @@ watch("acpi", 10,
else
batteryarc.colors = { beautiful.widget_main_color }
end
- text.text = charge
end,
batteryarc)