Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. -------------------------------------------
  2. -- Lua pretty printer
  3. -------------------------------------------
  4. local displayvalue=
  5. function (s)
  6. if not s or type(s)=='function' or type(s)=='userdata' then
  7. s=tostring(s)
  8. elseif type(s)~='number' then
  9. s=string.gsub(string.format('%q',s),'^"([^"\']*)"$',"'%1'")
  10. end
  11. return s
  12. end
  13.  
  14. local askeystr=
  15. function (u,s)
  16. if type(u)=='string' and string.find(u,'^[%w_]+$') then return s..u end
  17. return '['..displayvalue(u)..']'
  18. end
  19.  
  20. local horizvec=
  21. function (x,n)
  22. local o,e='',''
  23. for i=1,table.getn(x) do
  24. if type(x[i])=='table' then return end
  25. o=o..e..displayvalue(x[i])
  26. if string.len(o)>n then return end
  27. e=','
  28. end
  29. return '('..o..')'
  30. end
  31.  
  32. local horizmap=
  33. function (x,n)
  34. local o,e='',''
  35. for k,v in pairs(x) do
  36. if type(v)=='table' then return end
  37. o=o..e..askeystr(k,'')..'='..displayvalue(v)
  38. if string.len(o)>n then return end
  39. e=','
  40. end
  41. return '{'..o..'}'
  42. end
  43.  
  44. function pretty(p,x,h,q)
  45. if not p then p,x='globals',globals() end
  46. if type(x)=='table' then
  47. if not h then h={} end
  48. if h[x] then
  49. x=h[x]
  50. else
  51. if not q then q=p end
  52. h[x]=q
  53. local s={}
  54. for k,v in pairs(x) do table.insert(s,k) end
  55. if table.getn(s)>0 then
  56. local n=75-string.len(p)
  57. local f=table.getn(s)==table.getn(x) and horizvec(x,n)
  58. if not f then f=horizmap(x,n) end
  59. if not f then
  60. table.sort(s,function (a,b)
  61. --if tag(a)~=tag(b) then a,b=tag(b),tag(a) end
  62. if type(a)~=type(b) then a,b=type(b),type(a) end
  63. return a<b
  64. end)
  65. for i=1,table.getn(s) do
  66. if s[i] then
  67. local u=askeystr(s[i],'.')
  68. pretty(p..u,x[s[i]],h,q..u)
  69. p=string.rep(' ',string.len(p))
  70. end
  71. end
  72. return
  73. end
  74. x=f
  75. else
  76. x='{}'
  77. end
  78. end
  79. else
  80. x=displayvalue(x)
  81. end
  82. print(p..' = '..x)
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement