Last active
December 8, 2017 21:36
-
-
Save TobleMiner/e197665e5098e8538c0aa7c60c723ca9 to your computer and use it in GitHub Desktop.
Lua vardump with cycle detection
This file contains hidden or 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
_G['vardump'] = function(var, longform, prefix, refs) | |
prefix = prefix or '' | |
refs = refs or {} | |
if type(var) == 'table' then | |
local cyclic = not not refs[var] | |
refs[var] = true | |
print(prefix .. tostring(var) .. (cyclic and ' (cyclic)' or '')) | |
if not cyclic then | |
prefix = prefix .. ' ' | |
for k,v in pairs(var) do | |
print(prefix .. tostring(k) .. ': ' .. '(' .. type(v) .. ')') | |
vardump(v, longform, prefix .. '| ', refs) | |
end | |
refs[var] = not longform | |
end | |
else | |
local str = tostring(var) | |
str = string.gsub(str, '[^%a%s%d%p%%%^%$%(%)%[%]%*%+%-%?_~|{}\\]', '?') | |
print(prefix .. (type(var) == 'string' and '"' .. str .. '"' or str)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment