Add support for coarse representation of battery capacity, refactor battery widget for multiple battery display

https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power
This commit is contained in:
Alice Gaudon 2021-07-18 15:34:55 +02:00
parent e36d42dfeb
commit 00533c13f6
1 changed files with 60 additions and 43 deletions

View File

@ -1,3 +1,4 @@
local awful = require("awful")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local gears = require("gears") local gears = require("gears")
local naughty = require("naughty") local naughty = require("naughty")
@ -131,20 +132,9 @@ gears.timer {
---
local battery = wibox.widget { --- Batteries
{ ---
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
local batteryText = battery:get_children()[1]
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply") local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
function getBatteries() function getBatteries()
@ -155,48 +145,75 @@ function hasBattery()
return rawBatteries:len() > 0 return rawBatteries:len() > 0
end end
function getBatteryCapacity() function getBatteryLevel(battery)
local charge = 0 local charge = 0
local capacity = 0 local capacity = 0
for b in getBatteries() do if fileExists(battery .. "/energy_now") then
if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then charge = charge + readCommand("cat " .. battery .. "/energy_now")
if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then capacity = capacity + readCommand("cat " .. battery .. "/energy_full")
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") elseif fileExists(battery .. "/charge_now") then
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") charge = charge + readCommand("cat " .. battery .. "/charge_now")
else capacity = capacity + readCommand("cat " .. battery .. "/charge_full")
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") elseif fileExists(battery .. "/capacity_level") then
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full") return readCommand("cat " .. battery .. "/capacity_level")
end else
end return "Unsupported"
end end
return charge / capacity return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
end end
function isCharging() function isCharging(battery)
for b in getBatteries() do return not readCommand("cat " .. battery .. "/status") == "Discharging"
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
return readCommand("cat /sys/class/power_supply/" .. b .. "/status") ~= "Discharging"
end
end
return nil
end end
local batteryContainer = wibox.widget {
layout = wibox.layout.flex.horizontal
}
if hasBattery() then if hasBattery() then
gears.timer { for battery in getBatteries() do
timeout = config.battery.refresh_interval, local path = "/sys/class/power_supply/" .. battery
call_now = true, if readCommand("cat " .. path .. "/type"):match("Battery") then
autostart = true, -- widget
callback = function() local widget = wibox.widget {
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%" {
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
-- tooltip
local name = readCommand("cat " .. path .. "/model_name")
local tooltip = awful.tooltip {
text = name
}
tooltip:add_to_object(widget)
-- update battery level
local batteryText = widget:get_children()[1]
gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
end
}
batteryContainer:add(widget)
end end
} end
else else
battery = nil batteryContainer = nil
end end
return { return {
bars = bars, bars = bars,
battery = battery, battery = batteryContainer,
} }