Battery Widget using powersave
From awesome
I've written a simple Lua function for printing the battery state - [ 44%/77m▾ ] - to a widget. It uses the (quite common) "powersave" command - which is called directly from Lua via popen(). You do not have to run some other script at session startup, and you do not have to compile additional C bindings. Just install powersave and add the following lines to your rc.lua:
-- the widget
mybattmon = widget({ type = "textbox", name = "mybattmon", align = "right" })
-- returns a string with battery info
function battery_status ()
local battery = 0
local time = 0
local state = 0 -- discharging = -1, charging = 1, nothing = 0
local icon = ""
local fd = io.popen("powersave -b", "r")
if not fd then
do return "no info" end
end
local text = fd:read("*a")
io.close(fd)
if string.match(text, "discharging") then
state = -1
icon = "▾"
else
state = 1
icon = "▴"
end
battery = string.match(text, "Remaining percent: (%d+)")
time = string.match(text, "Remaining minutes: (%d+)")
-- above string does not always match
if not time then
time = string.match(text, "(%d+) minutes until fully charged")
end
return battery .. "%/" .. time .. "m" .. "<b>" .. icon .."</b>"
end
-- Hook called every second
function hook_timer ()
--mytextbox.text = " " .. os.date() .. " "
mybattmon.text = " " .. battery_status() .. " "
end