diff --git a/helpers.lua b/helpers.lua index 885b5bb..a90ab77 100644 --- a/helpers.lua +++ b/helpers.lua @@ -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 -- }}} diff --git a/widgets/bat_freebsd.lua b/widgets/bat_freebsd.lua index 56bb8e7..7fd3ec1 100644 --- a/widgets/bat_freebsd.lua +++ b/widgets/bat_freebsd.lua @@ -1,9 +1,3 @@ ---------------------------------------------------- --- Licensed under the GNU General Public License v2 --- * (c) 2010, Adrian C. --- * (c) 2013, NormalRa ---------------------------------------------------- - -- {{{ Grab environment local tonumber = tonumber local setmetatable = setmetatable diff --git a/widgets/bat.lua b/widgets/bat_linux.lua similarity index 96% rename from widgets/bat.lua rename to widgets/bat_linux.lua index 1f491d9..66f5b17 100644 --- a/widgets/bat.lua +++ b/widgets/bat_linux.lua @@ -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 }) diff --git a/widgets/init.lua b/widgets/init.lua index 5d3b246..92aed11 100644 --- a/widgets/init.lua +++ b/widgets/init.lua @@ -1,4 +1,3 @@ - local wrequire = require("vicious.helpers").wrequire widgets = { _NAME = "vicious.widgets" }