aboutsummaryrefslogtreecommitdiff
path: root/contrib/batacpi.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-08-29 00:49:57 +0200
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-08-29 00:49:57 +0200
commit26b0395ba988948a7cc8b79d08faf6a57058886a (patch)
treeae475623e8b249aa8b8d29b57878737f09c165f5 /contrib/batacpi.lua
parent0d6333ed61a305c8a18fd4eb42ad33a353627bc9 (diff)
downloadvicious-legacy-26b0395ba988948a7cc8b79d08faf6a57058886a.tar.xz
contrib: imported contrib widgets
Diffstat (limited to 'contrib/batacpi.lua')
-rw-r--r--contrib/batacpi.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/contrib/batacpi.lua b/contrib/batacpi.lua
new file mode 100644
index 0000000..62156fd
--- /dev/null
+++ b/contrib/batacpi.lua
@@ -0,0 +1,51 @@
+---------------------------------------------------
+-- Licensed under the GNU General Public License v2
+-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
+---------------------------------------------------
+
+-- {{{ Grab environment
+local tonumber = tonumber
+local io = { popen = io.popen }
+local setmetatable = setmetatable
+local table = { insert = table.insert }
+local string = { match = string.match }
+-- }}}
+
+
+-- Batacpi: provides state, charge, and remaining time for all batteries using acpitool
+module("vicious.contrib.batacpi")
+
+
+-- {{{ Battery widget type
+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
+ if string.match(line, "^[%s]+Battery.*") then
+ -- Store state and charge information
+ table.insert(battery_info, (battery_state[string.match(line, "([%a]*),") or "unknown"]))
+ table.insert(battery_info, (tonumber(string.match(line, "([%d]?[%d]?[%d])%.")) or 0))
+ -- Store remaining time information
+ table.insert(battery_info, (string.match(line, "%%,%s(.*)") or "N/A"))
+ else
+ return {battery_state["unknown"], 0, "N/A"}
+ end
+ end
+ f:close()
+
+ return battery_info
+end
+-- }}}
+
+setmetatable(_M, { __call = function(_, ...) return worker(...) end })