library refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-10-12 12:58:46 +02:00
parent a26665148d
commit 2e19a90d2d
2 changed files with 31 additions and 29 deletions
+15 -13
View File
@@ -5,11 +5,11 @@ function print(msg)
end
function format_key(k)
return type(k) == 'string' and k or '[' .. tostring(k) .. ']'
return type(k) == 'string' and k or '['..tostring(k)..']'
end
function format_value(v)
return type(v) == 'string' and '"' .. v .. '"' or tostring(v)
return type(v) == 'string' and '"'..v..'"' or tostring(v)
end
function print_table(t, depth)
@@ -27,15 +27,17 @@ local max_depth
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))
print(padding..format_key(k)..' = '..format_value(v))
if type(v) == 'table' then
print(padding .. '{')
print_table(v, depth + 1)
print(padding .. '}')
if next(v) then
print(padding..'{')
if depth == max_depth then
print(padding..' ...')
else
print_table(v, depth + 1)
end
print(padding .. '}')
end
end
end
@@ -53,13 +55,13 @@ local function setting(v)
if type(v) == 'number' then
max_depth = v
elseif type(v) == 'function' then
print('#' .. v())
print('#'..v())
else
print('#' .. v)
print('#'..v)
end
end
local mt = { __metatable=false, __call=inspect, __div=inspect }
local mt = {__metatable=false, __call=inspect, __div=inspect}
function mt:__index(key)
setting(key)