Skip to content

Instantly share code, notes, and snippets.

@TobleMiner
Last active December 8, 2017 21:36
Show Gist options
  • Save TobleMiner/e197665e5098e8538c0aa7c60c723ca9 to your computer and use it in GitHub Desktop.
Save TobleMiner/e197665e5098e8538c0aa7c60c723ca9 to your computer and use it in GitHub Desktop.
Lua vardump with cycle detection
_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