Closured Battery Widget

This is yet another notebook battery (Linux-only) monitoring widget for Awesome. Some day it was based on Gigamo Battery Widget, but was rewritten completely since then. In fact, as a trivial text widget it lacks anything interesting. But function empowering it is somewhat original: utilizing closure concept supported by Lua, it keeps its state between calls, thus allowing for more advanced behaviour of function itself. Oh, and of course there's fancy Unicode symbols!

Take a look at $XDG_CONFIG_HOME/awesome/battery.lua (if you want to see remaining time and have ACPI command-line client installed, you can use Closured Battery Widget with time) (or make use of Vicious, which calculates it for you). local io = io local math = math local naughty = naughty local beautiful = beautiful local tonumber = tonumber local tostring = tostring local print = print local pairs = pairs

module("battery")

local limits = {{25, 5}, {12, 3},         { 7, 1},            {0}}

function get_bat_state (adapter) local fcur = io.open("/sys/class/power_supply/"..adapter.."/charge_now") local fcap = io.open("/sys/class/power_supply/"..adapter.."/charge_full") local fsta = io.open("/sys/class/power_supply/"..adapter.."/status") local cur = fcur:read local cap = fcap:read local sta = fsta:read fcur:close fcap:close fsta:close local battery = math.floor(cur * 100 / cap) if sta:match("Charging") then dir = 1 elseif sta:match("Discharging") then dir = -1 else dir = 0 battery = "" end return battery, dir end

function getnextlim (num) for ind, pair in pairs(limits) do       lim = pair[1]; step = pair[2]; nextlim = limits[ind+1][1] or 0 if num > nextlim then repeat lim = lim - step until num > lim if lim < nextlim then lim = nextlim end return lim end end end

function batclosure (adapter) local nextlim = limits[1][1] return function local prefix = "⚡" local battery, dir = get_bat_state(adapter) if dir == -1 then dirsign = "↓" prefix = "Bat:" if battery <= nextlim then naughty.notify({title = "⚡ Beware! ⚡",                           text = "Battery charge is low ( ⚡ "..battery.."%)!",                            timeout = 7,                            position = "bottom_right",                            fg = beautiful.fg_focus,                            bg = beautiful.bg_focus                            }) nextlim = getnextlim(battery) end elseif dir == 1 then dirsign = "↑" nextlim = limits[1][1] else dirsign = "" end if dir ~= 0 then battery = battery.."%" end return " "..prefix.." "..dirsign..battery..dirsign.." " end end

The idea is to naughtify you not every time function has been run (if charge is lower than limit, that is), but at certain intervals: every 5% below 25%, every 3% below 12% and every percent below 7% in this case (see limits table).

If you get the following error "battery.lua:21: attemp to index local 'fcur' (a nil value)", check whether you have in directory /sys/class/power_supply/BAT0/ files charge_now and charge_full (substitute BAT0 with your battery alias). There might be energy_now and energy_full files, in which case you need to change lines 18 and 19 in your battery.lua accordingly. Original author has switched to Vicious, which should take care of this differences (see link to repository below).

Now create new widget in your $XDG_CONFIG_HOME/awesome/rc.lua and bind new function to it. Here's example for Awesome 3.4: -- Battery require("battery")

batterywidget = widget({type = "textbox", name = "batterywidget", align = "right" })

bat_clo = battery.batclosure("BAT0") batterywidget.text = bat_clo battimer = timer({ timeout = 30 }) battimer:add_signal("timeout", function batterywidget.text = bat_clo end) battimer:start

Current original author's version is kept in BitBucket repo (see widgets.lua and widget_fun.lua files) and has evolved quite a bit since this article was written.