diff --git a/worldeditadditions/init.lua b/worldeditadditions/init.lua index 7c4154a..a5b2713 100644 --- a/worldeditadditions/init.lua +++ b/worldeditadditions/init.lua @@ -14,6 +14,7 @@ wea.Mesh, wea.Face = dofile(wea.modpath.."/utils/mesh.lua") wea.Queue = dofile(wea.modpath.."/utils/queue.lua") wea.LRU = dofile(wea.modpath.."/utils/lru.lua") +wea.inspect = dofile(wea.modpath.."/utils/inspect.lua") wea.bit = dofile(wea.modpath.."/utils/bit.lua") diff --git a/worldeditadditions/utils/inspect.lua b/worldeditadditions/utils/inspect.lua new file mode 100644 index 0000000..3515962 --- /dev/null +++ b/worldeditadditions/utils/inspect.lua @@ -0,0 +1,37 @@ +--- Serialises an arbitrary value to a string. +-- Note that although the resulting table *looks* like valid Lua, it isn't. +-- @param item any Input item to serialise. +-- @param sep string key value seperator +-- @param new_line string key value pair delimiter +-- @return string concatenated table pairs +local function inspect(item, maxdepth) + if not maxdepth then maxdepth = 3 end + if type(item) ~= "table" then + if type(item) == "string" then return "\""..item.."\"" end + return tostring(item) + end + if maxdepth < 1 then return "[truncated]" end + + local result = { "{\n" } + for key,value in pairs(item) do + local value_text = inspect(value, maxdepth - 1) + :gsub("\n", "\n\t") + table.insert(result, "\t"..tostring(key).." = ".."("..type(value)..") "..value_text.."\n") + end + table.insert(result, "}") + return table.concat(result,"") +end + +local test = { + a = { x = 5, y = 7, z = -6 }, + http = { + port = 80, + protocol = "http" + }, + mode = "do_stuff", + apple = false, + deepa = { deepb = { deepc = { yay = "Happy Birthday!" } }} +} +print(inspect(test, 10)) + +return inspect