26 lines
5.5 KiB
Lua
26 lines
5.5 KiB
Lua
-- inspect 3.1.2 | /inspect.lua | https://github.com/kikito/inspect.lua | License: MIT License | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
|
|
local a={_VERSION='inspect.lua 3.1.0',_URL='http://github.com/kikito/inspect.lua',_DESCRIPTION='human-readable representations of tables',_LICENSE=[[
|
|
MIT LICENSE
|
|
|
|
Copyright (c) 2013 Enrique García Cota
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
copy of this software and associated documentation files (the
|
|
"Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
]]}local tostring=tostring;a.KEY=setmetatable({},{__tostring=function()return'inspect.KEY'end})a.METATABLE=setmetatable({},{__tostring=function()return'inspect.METATABLE'end})local function b(c)return next,c,nil end;local function d(e)if e:match('"')and not e:match("'")then return"'"..e.."'"end;return'"'..e:gsub('"','\\"')..'"'end;local f={["\a"]="\\a",["\b"]="\\b",["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t",["\v"]="\\v",["\127"]="\\127"}local g={["\127"]="\127"}for h=0,31 do local i=string.char(h)if not f[i]then f[i]="\\"..h;g[i]=string.format("\\%03d",h)end end;local function j(e)return e:gsub("\\","\\\\"):gsub("(%c)%f[0-9]",g):gsub("%c",f)end;local function k(e)return type(e)=='string'and e:match("^[_%a][_%a%d]*$")end;local function l(m,n)return type(m)=='number'and 1<=m and m<=n and math.floor(m)==m end;local o={['number']=1,['boolean']=2,['string']=3,['table']=4,['function']=5,['userdata']=6,['thread']=7}local function p(q,r)local s,t=type(q),type(r)if s==t and(s=='string'or s=='number')then return q<r end;local u,v=o[s],o[t]if u and v then return o[s]<o[t]elseif u then return true elseif v then return false end;return s<t end;local function w(c)local x=1;local y=rawget(c,x)while y~=nil do x=x+1;y=rawget(c,x)end;return x-1 end;local function z(c)local A,B={},0;local n=w(c)for m,C in b(c)do if not l(m,n)then B=B+1;A[B]=m end end;table.sort(A,p)return A,B,n end;local function D(c,E)E=E or{}if type(c)=='table'then if not E[c]then E[c]=1;for m,y in b(c)do D(m,E)D(y,E)end;D(getmetatable(c),E)else E[c]=E[c]+1 end end;return E end;local F=function(G)local H,x={},#G;for h=1,x do H[h]=G[h]end;return H,x end;local function I(J,...)local A={...}local K,x=F(J)for h=1,#A do K[x+h]=A[h]end;return K end;local function L(M,N,J,O)if N==nil then return nil end;if O[N]then return O[N]end;local P=M(N,J)if type(P)=='table'then local Q={}O[N]=Q;local R;for m,y in b(P)do R=L(M,m,I(J,m,a.KEY),O)if R~=nil then Q[R]=L(M,y,I(J,R),O)end end;local S=L(M,getmetatable(P),I(J,a.METATABLE),O)if type(S)~='table'then S=nil end;setmetatable(Q,S)P=Q end;return P end;local T={}local U={__index=T}function T:puts(...)local V={...}local W=self.buffer;local x=#W;for h=1,#V do x=x+1;W[x]=V[h]end end;function T:down(X)self.level=self.level+1;X()self.level=self.level-1 end;function T:tabify()self:puts(self.newline,string.rep(self.indent,self.level))end;function T:alreadyVisited(y)return self.ids[y]~=nil end;function T:getId(y)local Y=self.ids[y]if not Y then local Z=type(y)Y=(self.maxIds[Z]or 0)+1;self.maxIds[Z]=Y;self.ids[y]=Y end;return tostring(Y)end;function T:putKey(m)if k(m)then return self:puts(m)end;self:puts("[")self:putValue(m)self:puts("]")end;function T:putTable(c)if c==a.KEY or c==a.METATABLE then self:puts(tostring(c))elseif self:alreadyVisited(c)then self:puts('<table ',self:getId(c),'>')elseif self.level>=self.depth then self:puts('{...}')else if self.tableAppearances[c]>1 then self:puts('<',self:getId(c),'>')end;local _,a0,n=z(c)local S=getmetatable(c)self:puts('{')self:down(function()local a1=0;for h=1,n do if a1>0 then self:puts(',')end;self:puts(' ')self:putValue(c[h])a1=a1+1 end;for h=1,a0 do local m=_[h]if a1>0 then self:puts(',')end;self:tabify()self:putKey(m)self:puts(' = ')self:putValue(c[m])a1=a1+1 end;if type(S)=='table'then if a1>0 then self:puts(',')end;self:tabify()self:puts('<metatable> = ')self:putValue(S)end end)if a0>0 or type(S)=='table'then self:tabify()elseif n>0 then self:puts(' ')end;self:puts('}')end end;function T:putValue(y)local Z=type(y)if Z=='string'then self:puts(d(j(y)))elseif Z=='number'or Z=='boolean'or Z=='nil'or Z=='cdata'or Z=='ctype'then self:puts(tostring(y))elseif Z=='table'then self:putTable(y)else self:puts('<',Z,' ',self:getId(y),'>')end end;function a.inspect(a2,a3)a3=a3 or{}local a4=a3.depth or math.huge;local a5=a3.newline or'\n'local a6=a3.indent or' 'local M=a3.process;if M then a2=L(M,a2,{},{})end;local a7=setmetatable({depth=a4,level=0,buffer={},ids={},maxIds={},newline=a5,indent=a6,tableAppearances=D(a2)},U)a7:putValue(a2)return table.concat(a7.buffer)end;setmetatable(a,{__call=function(C,...)return a.inspect(...)end})return a
|