mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
refactorized level control
This commit is contained in:
parent
c25b42a610
commit
845a045962
40
inspect.lua
40
inspect.lua
@ -64,11 +64,11 @@ end
|
|||||||
local Inspector = {}
|
local Inspector = {}
|
||||||
|
|
||||||
function Inspector:new(v, depth)
|
function Inspector:new(v, depth)
|
||||||
local inspector = setmetatable( { buffer = {}, depth = depth }, {
|
local inspector = setmetatable( { buffer = {}, depth = depth, level = 0 }, {
|
||||||
__index = Inspector,
|
__index = Inspector,
|
||||||
__tostring = function(instance) return table.concat(instance.buffer) end
|
__tostring = function(instance) return table.concat(instance.buffer) end
|
||||||
} )
|
} )
|
||||||
return inspector:putValue(v, 0)
|
return inspector:putValue(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:puts(...)
|
function Inspector:puts(...)
|
||||||
@ -79,21 +79,30 @@ function Inspector:puts(...)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:tabify(level)
|
function Inspector:tabify()
|
||||||
self:puts("\n", string.rep(" ", level))
|
self:puts("\n", string.rep(" ", self.level))
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:putTable(t, level)
|
function Inspector:up()
|
||||||
if level >= self.depth then
|
self.level = self.level - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
function Inspector:down()
|
||||||
|
self.level = self.level + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
function Inspector:putTable(t)
|
||||||
|
if self.level >= self.depth then
|
||||||
self:puts('{...}')
|
self:puts('{...}')
|
||||||
else
|
else
|
||||||
local length, needsComma = #t, false
|
local length, needsComma = #t, false
|
||||||
self:puts('{')
|
self:puts('{')
|
||||||
|
self:down()
|
||||||
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:puts(' '):putValue(t[i], level + 1)
|
self:puts(' '):putValue(t[i])
|
||||||
end
|
end
|
||||||
|
|
||||||
local dictKeys = getDictionaryKeys(t)
|
local dictKeys = getDictionaryKeys(t)
|
||||||
@ -101,14 +110,15 @@ function Inspector:putTable(t, level)
|
|||||||
for _,k in ipairs(dictKeys) do
|
for _,k in ipairs(dictKeys) do
|
||||||
if needsComma then self:puts(',') end
|
if needsComma then self:puts(',') end
|
||||||
needsComma = true
|
needsComma = true
|
||||||
self:tabify(level+1)
|
self:tabify()
|
||||||
self:addKey(k, level + 1)
|
self:addKey(k)
|
||||||
self:puts(' = ')
|
self:puts(' = ')
|
||||||
self:putValue(t[k], level + 1)
|
self:putValue(t[k])
|
||||||
end
|
end
|
||||||
|
self:up()
|
||||||
|
|
||||||
if #dictKeys > 0 then
|
if #dictKeys > 0 then
|
||||||
self:tabify(level)
|
self:tabify()
|
||||||
elseif length > 0 then
|
elseif length > 0 then
|
||||||
self:puts(' ')
|
self:puts(' ')
|
||||||
end
|
end
|
||||||
@ -117,7 +127,7 @@ function Inspector:putTable(t, level)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:putValue(v, level)
|
function Inspector:putValue(v)
|
||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
@ -125,18 +135,18 @@ function Inspector:putValue(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:putTable(v, level)
|
self:putTable(v)
|
||||||
else
|
else
|
||||||
self:puts('<',tv,'>')
|
self:puts('<',tv,'>')
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:addKey(k, level)
|
function Inspector:addKey(k)
|
||||||
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( "[" ):putValue(k, level):puts("]")
|
return self:puts( "[" ):putValue(k):puts("]")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function inspect(t, depth)
|
local function inspect(t, depth)
|
||||||
|
Loading…
Reference in New Issue
Block a user