helpers.lua: restructure to support multiple operating systems
This commit is contained in:
parent
4820cb4d39
commit
3baade7b06
46
helpers.lua
46
helpers.lua
|
@ -12,13 +12,19 @@ local pairs = pairs
|
|||
local rawget = rawget
|
||||
local require = require
|
||||
local tonumber = tonumber
|
||||
local io = { open = io.open }
|
||||
local io = {
|
||||
open = io.open,
|
||||
popen = io.popen
|
||||
}
|
||||
local setmetatable = setmetatable
|
||||
local getmetatable = getmetatable
|
||||
local string = {
|
||||
upper = string.upper,
|
||||
lower = string.lower,
|
||||
format = string.format
|
||||
}
|
||||
local pcall = pcall
|
||||
local assert = assert
|
||||
-- }}}
|
||||
|
||||
|
||||
|
@ -32,11 +38,45 @@ local scroller = {}
|
|||
-- }}}
|
||||
|
||||
-- {{{ 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
|
||||
function helpers.wrequire(table, key)
|
||||
local module = rawget(table, key)
|
||||
return module or require(table._NAME .. "." .. key)
|
||||
local ret = rawget(table, key)
|
||||
|
||||
if ret then
|
||||
return ret
|
||||
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
|
||||
function helpers.pathtotable(dir)
|
||||
|
|
Loading…
Reference in New Issue