revert previous commit (e0abe1c474), as it broke widget when there are more than one battery

This commit is contained in:
Pavel Makhov 2019-03-29 17:33:00 -04:00
parent 5c637b1daf
commit 81ca86abde
1 changed files with 75 additions and 99 deletions

View File

@ -50,66 +50,44 @@ watch("acpi -i", 10,
local battery_info = {}
local capacities = {}
-- Change the logic of processing battery information from 'acpi -i'
for s in stdout:gmatch("[^\r\n]+") do
local status, charge_str = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?.*')
if charge_str ~= nil then
local status, charge_str, time = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?.*')
if string.match(s, 'rate information') then
-- ignore such line
elseif status ~= nil then
table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
else
local cap_str = string.match(s, '.+:.+last full capacity (%d+)')
if cap_str ~= nil then
table.insert(capacities, tonumber(cap_str))
end
end
end
-- total battery capacity
local total_capacity = 0
local capacity = 0
for i, cap in ipairs(capacities) do
total_capacity = total_capacity + cap
capacity = capacity + cap
end
-- capacity charged into all batteries
local charge_cap = 0
-- battery charge percentage 0~100
local charge_perc = 0
for i, batt in ipairs(battery_info) do
-- BUG: batt.charge ranges from 0 to 100, should be divided by 100
charge_cap = charge_cap + batt.charge/100 * capacities[i]
end
local charge = 0
local status
-- new logic to determine status
status = 'Full'
for i, batt in ipairs(battery_info) do
if batt.status == 'Charging' then
status = 'Charging'
break
end
if batt.status == 'Discharging' then
status = 'Discharging'
break
end
if batt.charge >= charge then
-- use most charged battery status. This is arbitrary, and maybe another metric should be used
status = batt.status
end
if total_capacity > 0 then
charge_perc = charge_cap / total_capacity * 100
charge = charge + batt.charge * capacities[i]
end
-- when widget.value is < 0.04, the widget shows a full circle (as widget.value=1)
-- so the charge_perc value is checked first
if charge_perc >= 5 then
widget.value = charge_perc / 100
local charge_percentage
if capacity > 5 then
charge = charge / capacity
charge_percentage = charge / 100
else
widget.value = 0.05
-- when widget.value is < 0.04, the widget shows a full circle (as widget.value=1)
charge_percentage = 0.05
end
widget.value = charge / 100
if status == 'Charging' then
text_with_background.bg = beautiful.widget_green
@ -119,14 +97,12 @@ watch("acpi -i", 10,
text_with_background.fg = beautiful.widget_main_color
end
text.text = string.format('%d', charge_perc)
--- if battery is fully charged (100) there is not enough place for three digits, so we don't show any text
text.text = charge == 100
and ''
or string.format('%d', charge)
-- add variables to make it easy to change settings
local bat_high = 75
local bat_low = 30
-- if charge_perc <= bat_low then
if charge_perc <= bat_low then
if charge < 15 then
batteryarc.colors = { beautiful.widget_red }
if status ~= 'Charging' and os.difftime(os.time(), last_battery_check) > 300 then
-- if 5 minutes have elapsed since the last warning
@ -134,7 +110,7 @@ watch("acpi -i", 10,
show_battery_warning()
end
elseif charge_perc > bat_low and charge_perc < bat_high then
elseif charge > 15 and charge < 40 then
batteryarc.colors = { beautiful.widget_yellow }
else
batteryarc.colors = { beautiful.widget_main_color }