diff options
author | streetturtle <streetturtle@users.noreply.github.com> | 2018-07-11 10:17:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-11 10:17:36 -0400 |
commit | 23a85097fe4e7de166a07b8cf64e7effb0ded64a (patch) | |
tree | e5a4035d8433db9178cb277c0189e47820e12d84 | |
parent | 3de116b467e1b3185cc5fd7a363a6305a6ec562e (diff) | |
parent | 3d38cdb866bc6360b1d00d6b8ebdc695fa41bb0d (diff) |
Merge pull request #45 from stnma7e/master
battery widgets support multiple batteries
-rw-r--r-- | battery-widget/battery.lua | 30 | ||||
-rw-r--r-- | batteryarc-widget/batteryarc.lua | 30 |
2 files changed, 53 insertions, 7 deletions
diff --git a/battery-widget/battery.lua b/battery-widget/battery.lua index 01a3ddd..4cbc314 100644 --- a/battery-widget/battery.lua +++ b/battery-widget/battery.lua @@ -67,14 +67,36 @@ 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 + last_battery_check = time() + show_battery_warning() end elseif (charge >= 15 and charge < 40) then batteryType = "battery-caution%s-symbolic" @@ -82,11 +104,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 diff --git a/batteryarc-widget/batteryarc.lua b/batteryarc-widget/batteryarc.lua index e1ab203..48233f3 100644 --- a/batteryarc-widget/batteryarc.lua +++ b/batteryarc-widget/batteryarc.lua @@ -38,11 +38,30 @@ local batteryarc = wibox.widget { -- mirror the widget, so that chart value increases clockwise local batteryarc_widget = wibox.container.mirror(batteryarc, { horizontal = true }) +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 + widget.value = charge / 100 if status == 'Charging' then mirrored_text_with_background.bg = beautiful.widget_green @@ -54,7 +73,10 @@ watch("acpi", 10, if charge < 15 then batteryarc.colors = { beautiful.widget_red } - 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 + last_battery_check = time() + show_battery_warning() end elseif charge > 15 and charge < 40 then @@ -110,4 +132,4 @@ function show_battery_warning() } end -return batteryarc_widget
\ No newline at end of file +return batteryarc_widget |