Created
November 3, 2012 18:53
-
-
Save vrinek/4008262 to your computer and use it in GitHub Desktop.
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
-- Prints the variable (even a table) in detail. It is very useful for tables | |
-- where print() is more or less useless. | |
function inspect(variable, indentation) | |
local inspected = "" -- this will be printed at the end | |
local inner_indentation = indentation and indentation + 1 or 0 -- for nested tables | |
local tabs = "" -- also for nested tables | |
for i=1,inner_indentation do | |
tabs = tabs .. "\t" | |
end | |
if type(variable) == "table" then | |
local itabs = tabs .. "\t" -- inner tabs | |
inspected = inspected .. "{\n" | |
for k, v in pairs(variable) do | |
local value = inspect(v, inner_indentation) | |
local key = k | |
-- number keys are iterated before string ones (as of Lua v5.1) | |
if type(key) == "number" then | |
inspected = inspected .. itabs .. value .. ",\n" | |
elseif type(key) == "string" then | |
inspected = inspected .. itabs .. key .. " = " .. value .. ",\n" | |
else | |
print("Unrecognized key type:", type(key)) | |
end | |
end | |
inspected = inspected .. tabs .. "}" | |
elseif type(variable) == "string" then | |
inspected = inspected .. '"' .. variable .. '"' | |
elseif type(variable) == "number" then | |
inspected = inspected .. variable | |
elseif type(variable) == "boolean" then | |
if variable then | |
inspected = inspected .. "true" | |
else | |
inspected = inspected .. "false" | |
end | |
elseif type(variable) == "nil" then | |
inspected = inspected .. "nil" | |
else | |
print("Unrecognized variable type:", type(variable)) | |
end | |
if inner_indentation == 0 then -- outer table or no table | |
print(inspected) | |
else -- inside a table | |
return inspected | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment