diff options
author | Adrian C. (anrxc) <anrxc@sysphere.org> | 2010-03-14 01:55:33 +0100 |
---|---|---|
committer | Adrian C. (anrxc) <anrxc@sysphere.org> | 2010-03-14 01:55:33 +0100 |
commit | 237470c8f45190b213e3a173ce6ae1a74b3e11fe (patch) | |
tree | 7f53c8144761947d4bde20715bcad34f4be0d6c0 /widgets/os.lua | |
parent | 9a82d4113a8271b7dfc7506f2b07379e3ede89a8 (diff) | |
download | vicious-legacy-237470c8f45190b213e3a173ce6ae1a74b3e11fe.tar.xz |
API: transform widgets namespace table to a directory
Diffstat (limited to 'widgets/os.lua')
-rw-r--r-- | widgets/os.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/widgets/os.lua b/widgets/os.lua new file mode 100644 index 0000000..8b1d11e --- /dev/null +++ b/widgets/os.lua @@ -0,0 +1,58 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2010, Adrian C. <anrxc@sysphere.org> +--------------------------------------------------- + +-- {{{ Grab environment +local pairs = pairs +local io = { popen = io.popen } +local os = { getenv = os.getenv } +local setmetatable = setmetatable +local helpers = require("vicious.helpers") +local string = { + gsub = string.gsub, + match = string.match +} +-- }}} + + +-- OS: provides operating system information +module("vicious.widgets.os") + + +-- {{{ Operating system widget type +local function worker(format) + local system = { + ["ostype"] = "N/A", + ["hostname"] = "N/A", + ["osrelease"] = "N/A", + ["username"] = "N/A" + } + + -- Linux manual page: uname(2) + local kernel = helpers.pathtotable("/proc/sys/kernel") + for k, v in pairs(system) do + if kernel[k] then + system[k] = string.gsub(kernel[k], "[%s]*$", "") + end + end + + -- BSD manual page: uname(1) + if system["ostype"] == "N/A" then + local f = io.popen("uname -snr") + local uname = f:read("*line") + f:close() + + system["ostype"], system["hostname"], system["osrelease"] = + string.match(uname, "([%w]+)[%s]([%w%p]+)[%s]([%w%p]+)") + end + + -- Get user from the environment + system["username"] = os.getenv("USER") + + return {system["ostype"], system["osrelease"], + system["username"], system["hostname"]} +end +-- }}} + +setmetatable(_M, { __call = function(_, ...) return worker(...) end }) |