Last active
December 14, 2015 06:59
-
-
Save fur-q/5047000 to your computer and use it in GitHub Desktop.
the best lua config loader idst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hostname = "localhost" | |
reconnect = true | |
attempts = 10 | |
ignore = { "user1", "user2" } | |
channels = {} | |
for i = 1, 4 do | |
channels[i] = "#channel" .. i | |
end | |
include "config2.cfg" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function include(name, env) | |
local cfg, err = assert(loadfile(name, "t", env)) | |
if setfenv then | |
setfenv(cfg, env) | |
end | |
cfg() | |
return env | |
end | |
local function load(name, env) | |
env = setmetatable(env or {}, | |
{ __index = { include = function(f) return include(f, env) end } } | |
) | |
local ok, env = xpcall(function() | |
debug.sethook(function() error("timed out") end, "c", 1e5) | |
include(name, env) | |
debug.sethook() | |
end, debug.traceback) | |
return ok and env or nil, ok or env | |
end | |
return { load = load } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local config = require "config" | |
local cfg = config.load("config.cfg") | |
print(cfg.hostname, cfg.attempts, cfg.channels[1]) | |
--> localhost 10 #channel1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment