From 191e99f0b55c3bd25ef29143aba11255ae15c2ff Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Sun, 10 Oct 2021 14:39:50 -0700 Subject: [PATCH] refactored table_tosting --- .../utils/tables/table_tostring.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/worldeditadditions/utils/tables/table_tostring.lua b/worldeditadditions/utils/tables/table_tostring.lua index 4a13586..da6778e 100644 --- a/worldeditadditions/utils/tables/table_tostring.lua +++ b/worldeditadditions/utils/tables/table_tostring.lua @@ -2,16 +2,27 @@ -- @param tbl table input table -- @param sep string key value seperator -- @param new_line string key value pair delimiter +-- @param max_depth number max recursion depth (optional) -- @return string concatenated table pairs -local function table_tostring(tbl, sep, new_line) +local function table_tostring(tbl, sep, new_line, max_depth) if type(sep) ~= "string" then sep = ": " end if type(new_line) ~= "string" then new_line = ", " end + 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 local ret = {} if type(tbl) ~= "table" then return "Error: input not table!" end for key,value in pairs(tbl) do - table.insert(ret,tostring(key) .. sep .. tostring(value) .. new_line) + 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 end - return table.concat(ret,"") + return table.concat(ret,new_line) end +-- Test: +-- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1)) + return table_tostring