aboutsummaryrefslogtreecommitdiff
path: root/bat.lua
blob: e6caacf398e013670252de568eb9079136c17c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
----------------------------------------------------------
-- Licensed under the GNU General Public License version 2
--  * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
----------------------------------------------------------

-- {{{ Grab environment
local io = { popen = io.popen }
local table = { insert = table.insert }
-- }}}


-- Bat: provides state, charge, and remaining time for all batteries
module("vicious.bat")


-- {{{ Battery widget type
function worker(format)
    -- Initialise tables
    local battery_info  = {}
    local battery_state = {
        ["charged"]     = "+",
        ["charging"]    = "+",
        ["discharging"] = "-"
    }

    -- Get data from acpitool
    local f = io.popen("acpitool -b")

    -- Format data
    for line in f:lines() do
        -- Check if the battery is present
        if line:match("^[%s]+Battery.*") then
            -- Store state and charge information
            table.insert(battery_info, battery_state[line:match("([%a]*),")])
            table.insert(battery_info, line:match("([%d]?[%d]?[%d])%."))
            -- Store remaining time information if the battery supports it
            table.insert(battery_info, (line:match("%%,%s(.*)") or "/"))
        end
    end
    f:close()

    return battery_info
end
-- }}}