From 3af1bbd373afc5a5e34ab581cc51cbf0c85b9e88 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sat, 1 Oct 2016 15:20:34 +0200 Subject: [PATCH] inspect library change --- libs/inspect.lua | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/libs/inspect.lua b/libs/inspect.lua index f2392f9..8777877 100644 --- a/libs/inspect.lua +++ b/libs/inspect.lua @@ -10,34 +10,46 @@ local function format_value(v) return type(v) == 'string' and '"' .. v .. '"' or tostring(v) end -local function print_table(t, max_depth, depth) +local max_depth + +local function print_pair(k, v, depth) local padding = strrep(' ', depth * 4) + if depth == max_depth then + print(padding .. '...') + return + end + print(padding .. format_key(k) .. ' = ' .. format_value(v)) + if type(v) == 'table' then + print(padding .. '{') + print_table(v, depth + 1) + print(padding .. '}') + end +end + +local function print_table(t, depth) + for i = 1, getn(t) do + print_pair(i, t[i], depth) + end for k, v in t do - if depth == max_depth then - print(padding .. '...') - return - end - print(padding .. format_key(k) .. ' = ' .. format_value(v)) - if type(v) == 'table' then - print(padding .. '{') - print_table(v, max_depth, depth + 1) - print(padding .. '}') + if type(k) ~= 'number' or k < 1 or k > getn(t) then + print_pair(k, v, depth) end end end -local depth - function inspect(_, ...) + local n = arg.n arg.n = nil - print_table(arg, depth or 2, 0) - depth = nil + table.setn(arg, n) + max_depth = max_depth or 2 + print_table(arg, 0) + max_depth = nil return unpack(arg) end local function setting(v) if type(v) == 'number' then - depth = v + max_depth = v elseif type(v) == 'function' then print('#' .. v()) else