helpers.lua: restructure to support multiple operating systems

This commit is contained in:
mutlusun 2017-01-25 17:46:54 +01:00 committed by Jörg Thalheim
parent 4820cb4d39
commit 3baade7b06
No known key found for this signature in database
GPG Key ID: CA4106B8D7CC79FA
1 changed files with 43 additions and 3 deletions

View File

@ -12,13 +12,19 @@ local pairs = pairs
local rawget = rawget local rawget = rawget
local require = require local require = require
local tonumber = tonumber local tonumber = tonumber
local io = { open = io.open } local io = {
open = io.open,
popen = io.popen
}
local setmetatable = setmetatable local setmetatable = setmetatable
local getmetatable = getmetatable local getmetatable = getmetatable
local string = { local string = {
upper = string.upper, upper = string.upper,
lower = string.lower,
format = string.format format = string.format
} }
local pcall = pcall
local assert = assert
-- }}} -- }}}
@ -32,12 +38,46 @@ local scroller = {}
-- }}} -- }}}
-- {{{ Helper functions -- {{{ Helper functions
-- {{{ Determine operating system
function helpers.getos()
local f = io.popen("uname -s")
local uname = f:read("*line")
f:close()
return string.lower(uname)
end
-- }}}
-- {{{ Loader of vicious modules -- {{{ Loader of vicious modules
function helpers.wrequire(table, key) function helpers.wrequire(table, key)
local module = rawget(table, key) local ret = rawget(table, key)
return module or require(table._NAME .. "." .. key)
if ret then
return ret
end end
local ostable = {
linux = { "linux", "all" },
freebsd = { "freebsd", "bsd", "all" }
}
local os = ostable[helpers.getos()]
assert(os, "Vicious: platform not supported.")
for i = 1, #os do
local status, value = pcall(require, table._NAME .. "." .. key .. "_" .. os[i])
if status then
ret = value
break
end
end
assert(ret, "Vicious: widget " .. table._NAME .. "." .. key .. " not available for current platform.")
return ret
end
-- }}}
-- {{{ Expose path as a Lua table -- {{{ Expose path as a Lua table
function helpers.pathtotable(dir) function helpers.pathtotable(dir)
return setmetatable({ _path = dir }, return setmetatable({ _path = dir },