[beautiful] Beautiful initialization handle path or table

Allow beautiful to be initialized either with a path to the theme file or directly with a table
This commit is contained in:
Alexis BRENON 2015-01-20 15:37:35 +01:00
parent 3bccb0ab73
commit 0c06d37899
1 changed files with 17 additions and 9 deletions

View File

@ -72,16 +72,24 @@ function beautiful.get_font_height(name)
end
--- Init function, should be runned at the beginning of configuration file.
-- @param path The theme file path.
function beautiful.init(path)
if path then
-- @param config The theme to load. It can be either the path to the theme file
-- (returning a table) or directly the table containing all the theme values
function beautiful.init(config)
if config then
local success
-- try and grab user's $HOME directory and expand '~'
local homedir = os.getenv("HOME")
path = path:gsub("^~/", homedir .. "/")
success, theme = pcall(function() return dofile(path) end)
-- If config is the path to the theme file,
-- run this file,
-- else if it is the theme table, save it
if type(config) == 'string' then
-- Expand the '~' $HOME shortcut
config = config:gsub("^~/", homedir .. "/")
success, theme = pcall(function() return dofile(config) end)
elseif type(config) == 'table' then
success = true
theme = config
end
if not success then
return print("E: beautiful: error loading theme file " .. theme)
@ -95,10 +103,10 @@ function beautiful.init(path)
if theme.font then set_font(theme.font) end
else
return print("E: beautiful: error loading theme file " .. path)
return print("E: beautiful: error loading theme file " .. config)
end
else
return print("E: beautiful: error loading theme: no path specified")
return print("E: beautiful: error loading theme: no theme specified")
end
end