86 lines
2.1 KiB
Lua
86 lines
2.1 KiB
Lua
local awful = require("awful")
|
|
local beautiful = require("beautiful")
|
|
local wibox = require("wibox")
|
|
|
|
local _M = {}
|
|
|
|
function _M.update(stdout)
|
|
local bat_now = {
|
|
present = "N/A",
|
|
state = "N/A",
|
|
warninglevel = "N/A",
|
|
energy = "N/A",
|
|
energyfull = "N/A",
|
|
energyrate = "N/A",
|
|
voltage = "N/A",
|
|
percentage = "N/A",
|
|
capacity = "N/A",
|
|
icon = "N/A",
|
|
}
|
|
|
|
for k, v in stdout:gmatch("([%a]+[%a|-]+):%s*([%a|%d|']+[,|%a|%d|'|-]+)") do
|
|
if k == "present" then
|
|
bat_now.present = v
|
|
elseif k == "state" then
|
|
bat_now.state = v
|
|
elseif k == "warning-level" then
|
|
bat_now.warninglevel = v
|
|
elseif k == "energy" then
|
|
bat_now.energy = v:gsub(",", ".") -- Wh
|
|
elseif k == "energy-full" then
|
|
bat_now.energyfull = v:gsub(",", ".") -- Wh
|
|
elseif k == "energy-rate" then
|
|
bat_now.energyrate = v:gsub(",", ".") -- W
|
|
elseif k == "voltage" then
|
|
bat_now.voltage = v:gsub(",", ".") -- V
|
|
elseif k == "percentage" then
|
|
bat_now.percentage = tonumber(v) -- %
|
|
elseif k == "capacity" then
|
|
bat_now.capacity = v:gsub(",", ".") -- %
|
|
elseif k == "icon-name" then
|
|
bat_now.icon = v
|
|
end
|
|
end
|
|
|
|
return bat_now
|
|
end
|
|
|
|
function _M.get(show_percentage)
|
|
_M.show_percentage = show_percentage or true
|
|
local battery = awful.widget.watch(
|
|
{
|
|
awful.util.shell,
|
|
"-c",
|
|
"upower -i /org/freedesktop/UPower/devices/battery_BAT0 | sed -n '/present/,/icon-name/p'",
|
|
},
|
|
1,
|
|
function(widget, stdout)
|
|
local bat_now = _M.update(stdout)
|
|
|
|
local icon = wibox.widget.imagebox(beautiful.battery_icon(bat_now.icon) or beautiful.battery_icon_full)
|
|
local text = wibox.widget.textbox(bat_now.percentage .. "%")
|
|
|
|
widget.children = {
|
|
(_M.show_percentage and text) or wibox.widget.textbox(),
|
|
icon
|
|
}
|
|
end,
|
|
wibox.widget {
|
|
layout = wibox.layout.fixed.horizontal,
|
|
}
|
|
)
|
|
|
|
battery.toggle_percentage = function()
|
|
_M.show_percentage = not _M.show_percentage
|
|
battery:emit_signal("widget::redraw_needed")
|
|
end
|
|
|
|
return battery
|
|
end
|
|
|
|
return setmetatable({}, {
|
|
__call = function(_, ...)
|
|
return _M.get(...)
|
|
end,
|
|
})
|