vicious/batat.lua

51 lines
1.5 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2009, Adrian C. <anrxc.sysphere.org>
---------------------------------------------------
-- {{{ Grab environment
local io = { popen = io.popen }
local setmetatable = setmetatable
local table = { insert = table.insert }
2009-10-05 00:10:47 +02:00
local string = { match = string.match }
-- }}}
-- Batat: provides state, charge, and remaining time for all batteries using acpitool
module("vicious.batat")
-- {{{ Battery widget type
2009-08-07 17:41:10 +02:00
local function worker(format)
local battery_info = {}
local battery_state = {
["full"] = "",
["unknown"] = "",
["charged"] = "",
["charging"] = "+",
["discharging"] = "-"
}
-- Get data from acpitool
local f = io.popen("acpitool -b")
for line in f:lines() do
-- Check if the battery is present
2009-10-05 00:10:47 +02:00
if string.match(line, "^[%s]+Battery.*") then
-- Store state and charge information
2009-10-05 00:10:47 +02:00
table.insert(battery_info, (battery_state[string.match(line, "([%a]*),")] or "/"))
table.insert(battery_info, (string.match(line, "([%d]?[%d]?[%d])%.") or "/"))
-- Store remaining time information
2009-10-05 00:10:47 +02:00
table.insert(battery_info, (string.match(line, "%%,%s(.*)") or "/"))
else
2009-07-30 20:47:02 +02:00
return {"/", "/", "/"}
end
end
f:close()
return battery_info
end
-- }}}
setmetatable(_M, { __call = function(_, ...) return worker(...) end })