aboutsummaryrefslogtreecommitdiff
path: root/os.lua
blob: eb0a2b15d4350c3c93f8b647e214f525ac6284c0 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
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.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 })