hide os specific code from user

This commit is contained in:
mutlusun 2016-12-21 19:21:25 +01:00
parent 82f9e8970d
commit 6f5a6825a1
4 changed files with 40 additions and 11 deletions

View File

@ -22,6 +22,8 @@ local string = {
upper = string.upper,
format = string.format
}
local error = error
local pcall = pcall
-- }}}
@ -35,10 +37,44 @@ local scroller = {}
-- }}}
-- {{{ Helper functions
-- {{{ Determine operating system
function helpers.getos()
local f = io.popen("uname -s")
local uname = f:read("*line")
f:close()
return uname:lower()
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 not ret then
os = helpers.getos()
if os == "linux" then
os = { "linux", "all" }
elseif os == "freebsd" then
os = { "freebsd", "bsd", "all" }
else
error("Vicious: platform not supported.")
end
for _, i in pairs(os) do -- there is a break in loop
local status, value =
pcall(function()
return require(table._NAME .. "." .. key .. "_" .. i)
end)
if status then
ret = value
break
end
end
end
return ret
end
-- }}}

View File

@ -1,9 +1,3 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
-- * (c) 2013, NormalRa <normalrawr@gmail.com>
---------------------------------------------------
-- {{{ Grab environment
local tonumber = tonumber
local setmetatable = setmetatable

View File

@ -18,7 +18,7 @@ local math = {
-- Bat: provides state, charge, remaining time, and wear for a requested battery
-- vicious.widgets.bat
local bat = {}
local bat_linux = {}
-- {{{ Battery widget type
@ -91,4 +91,4 @@ local function worker(format, warg)
end
-- }}}
return setmetatable(bat, { __call = function(_, ...) return worker(...) end })
return setmetatable(bat_linux, { __call = function(_, ...) return worker(...) end })

View File

@ -1,4 +1,3 @@
local wrequire = require("vicious.helpers").wrequire
widgets = { _NAME = "vicious.widgets" }