mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
small refactor
This commit is contained in:
parent
54ef1b0711
commit
282800b478
27
inspect.lua
27
inspect.lua
@ -27,7 +27,7 @@ local function unescape(str)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function isIdentifier(str)
|
local function isIdentifier(str)
|
||||||
return string.match( str, "^[_%a][_%a%d]*$" )
|
return type(str) == 'string' and str:match( "^[_%a][_%a%d]*$" )
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isArrayKey(k, length)
|
local function isArrayKey(k, length)
|
||||||
@ -91,21 +91,13 @@ function Inspector:new(v, depth)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable( inspector, {
|
setmetatable( inspector, {
|
||||||
__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)
|
return inspector:putValue(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:puts(...)
|
|
||||||
local args = {...}
|
|
||||||
for i=1, #args do
|
|
||||||
table.insert(self.buffer, tostring(args[i]))
|
|
||||||
end
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
function Inspector:tabify()
|
function Inspector:tabify()
|
||||||
self:puts("\n", string.rep(" ", self.level))
|
self:puts("\n", string.rep(" ", self.level))
|
||||||
return self
|
return self
|
||||||
@ -119,6 +111,14 @@ function Inspector:down()
|
|||||||
self.level = self.level + 1
|
self.level = self.level + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Inspector:puts(...)
|
||||||
|
local args = {...}
|
||||||
|
for i=1, #args do
|
||||||
|
table.insert(self.buffer, tostring(args[i]))
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
function Inspector:putComma(comma)
|
function Inspector:putComma(comma)
|
||||||
if comma then self:puts(',') end
|
if comma then self:puts(',') end
|
||||||
return true
|
return true
|
||||||
@ -160,7 +160,7 @@ function Inspector:putTable(t)
|
|||||||
self:tabify():puts('<metatable> = '):putValue(mt)
|
self:tabify():puts('<metatable> = '):putValue(mt)
|
||||||
end
|
end
|
||||||
self:up()
|
self:up()
|
||||||
|
|
||||||
if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
|
if #dictKeys > 0 or mt then -- dictionary table. Justify closing }
|
||||||
self:tabify()
|
self:tabify()
|
||||||
elseif length > 0 then -- array tables have one extra space before closing }
|
elseif length > 0 then -- array tables have one extra space before closing }
|
||||||
@ -202,10 +202,9 @@ function Inspector:putValue(v)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function Inspector:putKey(k)
|
function Inspector:putKey(k)
|
||||||
if type(k) == "string" and isIdentifier(k) then
|
if isIdentifier(k) then return self:puts(k) end
|
||||||
return self:puts(k)
|
|
||||||
end
|
|
||||||
return self:puts( "[" ):putValue(k):puts("]")
|
return self:puts( "[" ):putValue(k):puts("]")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user