Closured Battery Widget

From awesome
Jump to: navigation, search

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

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).

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")

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()

Original is kept in Google Code repo and might evolve quite a bit since this article was written (or stay the very same, of course).

Personal tools