other minor refactors

This commit is contained in:
kikito 2012-10-29 21:05:31 +01:00
parent 282800b478
commit fa1ef6683e

View File

@ -113,24 +113,26 @@ end
function Inspector:puts(...) function Inspector:puts(...)
local args = {...} local args = {...}
local len = #self.buffer
for i=1, #args do for i=1, #args do
table.insert(self.buffer, tostring(args[i])) len = len + 1
self.buffer[len] = tostring(args[i])
end end
return self return self
end end
function Inspector:putComma(comma) function Inspector:commaControl(comma)
if comma then self:puts(',') end if comma then self:puts(',') end
return true return true
end end
function Inspector:putTable(t) function Inspector:putTable(t)
if self:alreadySeen(t) then if self:alreadySeen(t) then
self:puts('<table ', self:getOrCreateCounter(t), '>') self:puts('<table ', self:getCounter(t), '>')
elseif self.level >= self.depth then elseif self.level >= self.depth then
self:puts('{...}') self:puts('{...}')
else else
self:puts('<',self:getOrCreateCounter(t),'>{') self:puts('<',self:getCounter(t),'>{')
self:down() self:down()
local length = #t local length = #t
@ -144,19 +146,19 @@ function Inspector:putTable(t)
local comma = false local comma = false
for i=1, length do for i=1, length do
comma = self:putComma(comma) comma = self:commaControl(comma)
self:puts(' '):putValue(t[i]) self:puts(' '):putValue(t[i])
end end
local dictKeys = getDictionaryKeys(t) local dictKeys = getDictionaryKeys(t)
for _,k in ipairs(dictKeys) do for _,k in ipairs(dictKeys) do
comma = self:putComma(comma) comma = self:commaControl(comma)
self:tabify():putKey(k):puts(' = '):putValue(t[k]) self:tabify():putKey(k):puts(' = '):putValue(t[k])
end end
if mt then if mt then
comma = self:putComma(comma) comma = self:commaControl(comma)
self:tabify():puts('<metatable> = '):putValue(mt) self:tabify():puts('<metatable> = '):putValue(mt)
end end
self:up() self:up()
@ -172,11 +174,10 @@ function Inspector:putTable(t)
end end
function Inspector:alreadySeen(v) function Inspector:alreadySeen(v)
local tv = type(v) return self.pools[type(v)][v] ~= nil
return self.pools[tv][v] ~= nil
end end
function Inspector:getOrCreateCounter(v) function Inspector:getCounter(v)
local tv = type(v) local tv = type(v)
local current = self.pools[tv][v] local current = self.pools[tv][v]
if not current then if not current then
@ -197,7 +198,7 @@ function Inspector:putValue(v)
elseif tv == 'table' then elseif tv == 'table' then
self:putTable(v) self:putTable(v)
else else
self:puts('<',tv,' ',self:getOrCreateCounter(v),'>') self:puts('<',tv,' ',self:getCounter(v),'>')
end end
return self return self
end end