122 lines
3.1 KiB
Lua
122 lines
3.1 KiB
Lua
local awful = require("awful")
|
|
local wibox = require("wibox")
|
|
local gears = require("gears")
|
|
local beautiful = require("beautiful")
|
|
local menubar = require("menubar")
|
|
local cairo = require("lgi").cairo
|
|
local Rsvg = require("lgi").Rsvg
|
|
local longpress = require("awesome-longpress")
|
|
local posix = require "posix"
|
|
local batterystat = {
|
|
batteries = {},
|
|
iconSize = 96,
|
|
iconPath = gears.filesystem.get_configuration_dir() .. "/awesome-batterystat/"
|
|
}
|
|
|
|
local iconNames = {
|
|
["battery-full"] = 95,
|
|
["battery-good"] = 70,
|
|
["battery-low"] = 40,
|
|
["battery-caution"] = 20,
|
|
["battery-empty"] = 0}
|
|
local function chooseIcon(status,percent)
|
|
local bestIcon,bestPercent = "", -1
|
|
for k,v in pairs(iconNames) do
|
|
if v > bestPercent and percent >= v then
|
|
bestIcon, bestPercent = k, v
|
|
end
|
|
end
|
|
if status == "Charging" then
|
|
return bestIcon.."-charging"
|
|
end
|
|
return bestIcon
|
|
end
|
|
|
|
-- preload icon widgets to avoid loading them on every update
|
|
local icons = {}
|
|
for k,v in pairs(iconNames) do
|
|
icons[k] = wibox.widget{
|
|
widget = wibox.widget.imagebox,
|
|
forced_height = batterystat.iconSize,
|
|
forced_width = batterystat.iconSize,
|
|
image = string.format("%s/%s.svg",batterystat.iconPath,k)
|
|
}
|
|
icons[k.."-charging"] = wibox.widget{
|
|
widget = wibox.widget.imagebox,
|
|
forced_height = batterystat.iconSize,
|
|
forced_width = batterystat.iconSize,
|
|
image = string.format("%s/%s-charging.svg",batterystat.iconPath,k)
|
|
}
|
|
end
|
|
|
|
local function getBatteryState(path)
|
|
local state, percent
|
|
local f = io.open(string.format("%s/status",path),"r")
|
|
if f then
|
|
state = f:read("*a"):match("%S+")
|
|
f:close()
|
|
end
|
|
local f = io.open(string.format("%s/capacity",path),"r")
|
|
if f then
|
|
percent = tonumber(f:read("*a"):match("%S+"))
|
|
f:close()
|
|
end
|
|
return state, tonumber(percent)
|
|
end
|
|
|
|
function batterystat.scan()
|
|
for _,d in pairs(posix.dir("/sys/class/power_supply")) do
|
|
local f = io.open(string.format("/sys/class/power_supply/%s/type",d))
|
|
if f then
|
|
local ptype = f:read("*a"):match("%S+")
|
|
if ptype == "Battery" then
|
|
batterystat.batteries[#batterystat.batteries+1] = string.format("/sys/class/power_supply/%s/",d)
|
|
end
|
|
f:close()
|
|
end
|
|
end
|
|
end
|
|
|
|
local function updateBatteryWidget(self)
|
|
local state, percent = getBatteryState(self.path)
|
|
local colour = (state == "Charging") and "lime" or "red"
|
|
self:set(1,icons[chooseIcon(state, percent)])
|
|
self.children[3].markup = string.format('<span foreground="%s" size="x-large">%d%%</span>',colour,percent)
|
|
end
|
|
|
|
function batterystat.new(path)
|
|
local state, percent = getBatteryState(path)
|
|
local name = path:match("/([^/]+)/$")
|
|
local sbwidget = wibox.widget{
|
|
layout = wibox.layout.stack,
|
|
forced_width = batterystat.iconSize,
|
|
path = path,
|
|
update = updateBatteryWidget,
|
|
icons[chooseIcon(state,percent)],
|
|
wibox.widget{
|
|
widget = wibox.widget.textbox,
|
|
align = "center",
|
|
valign = "top",
|
|
text = name
|
|
},
|
|
wibox.widget{
|
|
widget = wibox.widget.textbox,
|
|
align = "right",
|
|
valign = "bottom",
|
|
}
|
|
}
|
|
sbwidget:update()
|
|
return sbwidget
|
|
end
|
|
|
|
function batterystat.all()
|
|
local rt = {}
|
|
batterystat.scan()
|
|
for k,v in pairs(batterystat.batteries) do
|
|
rt[#rt+1] = batterystat.new(v)
|
|
end
|
|
return rt
|
|
end
|
|
|
|
return batterystat
|