mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-23 23:53:44 +01:00
Add worldeditadditions.inspect function
This commit is contained in:
parent
35777b7ae5
commit
9bcf242443
@ -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")
|
||||
|
||||
|
37
worldeditadditions/utils/inspect.lua
Normal file
37
worldeditadditions/utils/inspect.lua
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user