forked from manoelcampos/xml2lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.lua
86 lines (80 loc) · 2.19 KB
/
pretty.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
--- Lua pretty printer from <a href="http://mini.net/cgi-bin/lua/44.html">http://mini.net/cgi-bin/lua/44.html</a><br/>
-- This was extracted from utility code in "util.lua".<br/>
-- 23/02/2001 [email protected]<br/>
-- Pretty displays a value, properly dealing with tables and cycles
local displayvalue=
function (s)
if not s or type(s)=='function' or type(s)=='userdata' then
s=tostring(s)
elseif type(s)~='number' then
s=string.gsub(string.format('%q',s),'^"([^"\']*)"$',"'%1'")
end
return s
end
local askeystr=
function (u,s)
if type(u)=='string' and string.find(u,'^[%w_]+$') then return s..u end
return '['..displayvalue(u)..']'
end
local horizvec=
function (x,n)
local o,e='',''
for i=1,table.getn(x) do
if type(x[i])=='table' then return end
o=o..e..displayvalue(x[i])
if string.len(o)>n then return end
e=','
end
return '('..o..')'
end
local horizmap=
function (x,n)
local o,e='',''
for k,v in pairs(x) do
if type(v)=='table' then return end
o=o..e..askeystr(k,'')..'='..displayvalue(v)
if string.len(o)>n then return end
e=','
end
return '{'..o..'}'
end
function pretty(p,x,h,q)
if not p then p,x='globals',globals() end
if type(x)=='table' then
if not h then h={} end
if h[x] then
x=h[x]
else
if not q then q=p end
h[x]=q
local s={}
for k,v in pairs(x) do table.insert(s,k) end
if table.getn(s)>0 then
local n=75-string.len(p)
local f=table.getn(s)==table.getn(x) and horizvec(x,n)
if not f then f=horizmap(x,n) end
if not f then
table.sort(s,function (a,b)
--if tag(a)~=tag(b) then a,b=tag(b),tag(a) end
if type(a)~=type(b) then a,b=type(b),type(a) end
return a<b
end)
for i=1,table.getn(s) do
if s[i] then
local u=askeystr(s[i],'.')
pretty(p..u,x[s[i]],h,q..u)
p=string.rep(' ',string.len(p))
end
end
return
end
x=f
else
x='{}'
end
end
else
x=displayvalue(x)
end
print(p..' = '..x)
end