inspect library change

This commit is contained in:
Manuel Simon Hirsig
2016-10-01 15:20:34 +02:00
parent 14e429221c
commit 3af1bbd373
+27 -15
View File
@@ -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