mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
I didnt commit the right stuff on previous commit. fixing that
This commit is contained in:
parent
71bf1a87a0
commit
4572d02f72
33
inspect.lua
33
inspect.lua
@ -63,12 +63,12 @@ end
|
|||||||
|
|
||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
|
|
||||||
function Inspector:new(v)
|
function Inspector:new(v, depth)
|
||||||
local inspector = setmetatable( { buffer = {} }, {
|
local inspector = setmetatable( { buffer = {}, depth = depth }, {
|
||||||
__index = Inspector,
|
__index = Inspector,
|
||||||
__tostring = function(instance) return table.concat(instance.buffer) end
|
__tostring = function(instance) return table.concat(instance.buffer) end
|
||||||
} )
|
} )
|
||||||
return inspector:addValue(v, 0)
|
return inspector:putValue(v, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:puts(...)
|
function Inspector:puts(...)
|
||||||
@ -84,13 +84,16 @@ function Inspector:tabify(level)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:addTable(t, level)
|
function Inspector:putTable(t, level)
|
||||||
|
if level >= self.depth then
|
||||||
|
self:puts('{...}')
|
||||||
|
else
|
||||||
local length, needsComma = #t, false
|
local length, needsComma = #t, false
|
||||||
self:puts('{')
|
self:puts('{')
|
||||||
for i=1, length do
|
for i=1, length do
|
||||||
if needsComma then self:puts(',') end
|
if needsComma then self:puts(',') end
|
||||||
needsComma = true
|
needsComma = true
|
||||||
self:addValue(t[i], level + 1)
|
self:puts(' '):putValue(t[i], level + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local dictKeys = getDictionaryKeys(t)
|
local dictKeys = getDictionaryKeys(t)
|
||||||
@ -101,15 +104,20 @@ function Inspector:addTable(t, level)
|
|||||||
self:tabify(level+1)
|
self:tabify(level+1)
|
||||||
self:addKey(k, level + 1)
|
self:addKey(k, level + 1)
|
||||||
self:puts(' = ')
|
self:puts(' = ')
|
||||||
self:addValue(t[k], level + 1)
|
self:putValue(t[k], level + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
if #dictKeys > 0 then self:tabify(level) end
|
if #dictKeys > 0 then
|
||||||
|
self:tabify(level)
|
||||||
|
elseif length > 0 then
|
||||||
|
self:puts(' ')
|
||||||
|
end
|
||||||
self:puts('}')
|
self:puts('}')
|
||||||
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:addValue(v, level)
|
function Inspector:putValue(v, level)
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
@ -117,7 +125,7 @@ function Inspector:addValue(v, level)
|
|||||||
elseif tv == 'number' or tv == 'boolean' then
|
elseif tv == 'number' or tv == 'boolean' then
|
||||||
self:puts(tostring(v))
|
self:puts(tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' then
|
||||||
self:addTable(v, level)
|
self:putTable(v, level)
|
||||||
else
|
else
|
||||||
self:puts('<',tv,'>')
|
self:puts('<',tv,'>')
|
||||||
end
|
end
|
||||||
@ -128,11 +136,12 @@ function Inspector:addKey(k, level)
|
|||||||
if type(k) == "string" and isIdentifier(k) then
|
if type(k) == "string" and isIdentifier(k) then
|
||||||
return self:puts(k)
|
return self:puts(k)
|
||||||
end
|
end
|
||||||
return self:puts( "[" ):addValue(k, level):puts("]")
|
return self:puts( "[" ):putValue(k, level):puts("]")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function inspect(t)
|
local function inspect(t, depth)
|
||||||
return tostring(Inspector:new(t))
|
depth = depth or 4
|
||||||
|
return tostring(Inspector:new(t, depth))
|
||||||
end
|
end
|
||||||
|
|
||||||
return inspect
|
return inspect
|
||||||
|
@ -100,6 +100,12 @@ context( 'inspect', function()
|
|||||||
b = {...}
|
b = {...}
|
||||||
}
|
}
|
||||||
}]])
|
}]])
|
||||||
|
|
||||||
|
assert_equal(inspect(level4, 1), [[{ 1, 2, 3,
|
||||||
|
a = {...}
|
||||||
|
}]])
|
||||||
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user