2021-06-03 00:57:46 +00:00
|
|
|
--- Returns the key value pairs in a table as a single string
|
|
|
|
-- @param tbl table input table
|
|
|
|
-- @param sep string key value seperator
|
|
|
|
-- @param new_line string key value pair delimiter
|
2021-10-10 21:39:50 +00:00
|
|
|
-- @param max_depth number max recursion depth (optional)
|
2021-06-03 00:57:46 +00:00
|
|
|
-- @return string concatenated table pairs
|
2021-10-10 21:39:50 +00:00
|
|
|
local function table_tostring(tbl, sep, new_line, max_depth)
|
2021-07-01 05:11:34 +00:00
|
|
|
if type(sep) ~= "string" then sep = ": " end
|
|
|
|
if type(new_line) ~= "string" then new_line = ", " end
|
2021-10-10 21:39:50 +00:00
|
|
|
if type(max_depth) == "number" then max_depth = {depth=0,max=max_depth}
|
|
|
|
elseif type(max_depth) ~= "table" then max_depth = {depth=0,max=5} end
|
2021-07-01 05:11:34 +00:00
|
|
|
local ret = {}
|
|
|
|
if type(tbl) ~= "table" then return "Error: input not table!" end
|
|
|
|
for key,value in pairs(tbl) do
|
2021-10-10 21:39:50 +00:00
|
|
|
if type(value) == "table" and max_depth.depth < max_depth.max then
|
|
|
|
table.insert(ret,tostring(key) .. sep ..
|
|
|
|
"{" .. table_tostring(value,sep,new_line,{max_depth.depth+1,max_depth.max}) .. "}")
|
|
|
|
else
|
|
|
|
table.insert(ret,tostring(key) .. sep .. tostring(value))
|
|
|
|
end
|
2021-07-01 05:11:34 +00:00
|
|
|
end
|
2021-10-10 21:39:50 +00:00
|
|
|
return table.concat(ret,new_line)
|
2021-06-03 00:57:46 +00:00
|
|
|
end
|
2021-06-27 23:56:29 +00:00
|
|
|
|
2021-10-10 21:39:50 +00:00
|
|
|
-- Test:
|
|
|
|
-- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1))
|
|
|
|
|
2021-06-27 23:56:29 +00:00
|
|
|
return table_tostring
|