Add worldeditadditions.inspect function

This commit is contained in:
Starbeamrainbowlabs 2021-08-05 15:55:30 +01:00
parent 35777b7ae5
commit 9bcf242443
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
2 changed files with 38 additions and 0 deletions

@ -14,6 +14,7 @@ wea.Mesh, wea.Face = dofile(wea.modpath.."/utils/mesh.lua")
wea.Queue = dofile(wea.modpath.."/utils/queue.lua") wea.Queue = dofile(wea.modpath.."/utils/queue.lua")
wea.LRU = dofile(wea.modpath.."/utils/lru.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") wea.bit = dofile(wea.modpath.."/utils/bit.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