diff --git a/analyze_structure.lua b/analyze_structure.lua index 4172e4e..0d7f8b4 100644 --- a/analyze_structure.lua +++ b/analyze_structure.lua @@ -211,9 +211,11 @@ end -------------------------------------------------------------------------------- -local function analyze_structure (root) +local function analyze_structure (root, max_depth) if type(root) ~= 'table' then return {} end assert(type(root) == 'table') + local max_depth = max_depth or math.huge + assert(type(max_depth) == 'number') local info, visited, next_mark, depth = { root = root }, {}, 1, { [root] = 0 } local queue = { root, bottom = 1, top = 2 } @@ -227,7 +229,7 @@ local function analyze_structure (root) info[node].marker, next_mark = next_mark, next_mark + 1 end -- Get table info & visit children. - if visited[node] < 2 then + if visited[node] < 2 and depth[node] < max_depth then for k, v in pairs(node) do if type(k) == 'table' then queue[queue.top], queue.top, depth[k] = k, queue.top + 1, math.min(depth[k] or math.huge, depth[node] + 1) end if type(v) == 'table' then queue[queue.top], queue.top, depth[v] = v, queue.top + 1, math.min(depth[v] or math.huge, depth[node] + 1) end diff --git a/function.lua b/function.lua index 92b8f1f..ee0960a 100644 --- a/function.lua +++ b/function.lua @@ -237,7 +237,6 @@ return function (value, depth, l, format_value) l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) } -- Cleanup and finish - print(not l.options.more_function_info, depth ~= 0) if not l.options.more_function_info or depth ~= 0 then l[#l+1] = (function_body:sub(1,1) == '\n') and '' or ' ' l[#l+1] = function_body diff --git a/pretty.lua b/pretty.lua index f935e4c..f9e0464 100644 --- a/pretty.lua +++ b/pretty.lua @@ -508,7 +508,7 @@ local function pretty_format (value, options) local l = StringBuilder() l.visited = { next_mark = 1 } l.options = options - l.info = analyze_structure(value) + l.info = analyze_structure(value, options.max_depth) -- Format value. format_value(value, 0, l) diff --git a/test/test_analyze_structure.lua b/test/test_analyze_structure.lua index 7e027a2..95f2ee8 100644 --- a/test/test_analyze_structure.lua +++ b/test/test_analyze_structure.lua @@ -125,6 +125,29 @@ SUITE:addTest('Not Tabular, due to varying lengths', function () assert_equal(table_info.is_tabular, false) end) +-------------------------------------------------------------------------------- +-- Stops at max_depth + +SUITE:addTest('Has info for root, and children', function () + local input = { { 1 }, { 2, 3 }, { 4, 5, 6 } } + local table_info = analyze_structure(input, 1) + + assert(table_info[input]) + assert(table_info[input[1]]) + assert(table_info[input[2]]) + assert(table_info[input[3]]) +end) + +SUITE:addTest('Has info for root, but not children', function () + local input = { { 1 }, { 2, 3 }, { 4, 5, 6 } } + local table_info = analyze_structure(input, 0) + + assert(table_info[input]) + assert(not table_info[input[1]]) + assert(not table_info[input[2]]) + assert(not table_info[input[3]]) +end) + -------------------------------------------------------------------------------- -- Corner cases.