Advertisement
Maxstripe

consoleutils.lua

Jun 23rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. --[[
  2. ConsoleUtils by PixelToast, public domain
  3.  
  4. > console.box("puts a box\naround some text")
  5. ┌────────────────┐
  6. │puts a box │
  7. │around some text│
  8. └────────────────┘
  9. > console.cat(console.box("cats\nsome\ntext")," with more text")
  10. ┌────┐
  11. │cats│
  12. │some│ with more text
  13. │text│
  14. └────┘
  15. > console.table({{"first","column"},{"second\ncolumn"},{"foo","potato walrus","bar"}})
  16. ┌──────┬─────────────┬───┐
  17. │first │column │ │
  18. ├──────┼─────────────┼───┤
  19. │second│ │ │
  20. │column│ │ │
  21. ├──────┼─────────────┼───┤
  22. │foo │potato walrus│bar│
  23. └──────┴─────────────┴───┘
  24. > console.serialize({key="value",table={list={1,2,"three"},d="alphabetical order",c=1,b=2,a=3}})
  25. ┌─────────────────────────────────────────┐
  26. │"key" = "value" │
  27. │ ┌─────────────────────────────┐│
  28. │ │"a" = 3 ││
  29. │ │"b" = 2 ││
  30. │ │"c" = 1 ││
  31. │ │"d" = "alphabetical order"││
  32. │"table" = │ ┌───────┐ ││
  33. │ │ │1 │ ││
  34. │ │"list" = │2 │ ││
  35. │ │ │"three"│ ││
  36. │ │ └───────┘ ││
  37. │ └─────────────────────────────┘│
  38. └─────────────────────────────────────────┘
  39. ]]
  40.  
  41. local console={}
  42.  
  43. local box={
  44. ud="│",
  45. lr="─",
  46. ur="└",
  47. rd="┌",
  48. ld="┐",
  49. lu="┘",
  50. lrd="┬",
  51. udr="├",
  52. x="┼",
  53. lud="┤",
  54. lur="┴",
  55. e="[]"
  56. }
  57.  
  58. --[[local box={
  59. ud="|",
  60. lr="-",
  61. ur="\\",
  62. rd="/",
  63. ld="\\",
  64. lu="/",
  65. lrd="-",
  66. udr="|",
  67. x="+",
  68. lud="|",
  69. lur="-",
  70. e="[]"
  71. }]]
  72.  
  73. local unicode=require("unicode")
  74.  
  75. local function txtbl(s,mx)
  76. mx=mx or 0
  77. if s=="" and mx>0 then
  78. s=" "
  79. end
  80. local o={}
  81. for c in s:gmatch("[^\r\n]+") do
  82. table.insert(o,c)
  83. end
  84. for l1=1,#o do
  85. mx=math.max(mx,unicode.len(o[l1]))
  86. end
  87. for l1=1,#o do
  88. o[l1]=o[l1]..(" "):rep(mx-unicode.len(o[l1]))
  89. end
  90. return o,mx
  91. end
  92.  
  93. function console.box(s)
  94. local t,l=txtbl(s)
  95. for l1=1,#t do
  96. t[l1]=box.ud..t[l1]..box.ud
  97. end
  98. table.insert(t,1,box.rd..(box.lr:rep(l))..box.ld)
  99. table.insert(t,box.ur..(box.lr:rep(l))..box.lu)
  100. return table.concat(t,"\n")
  101. end
  102.  
  103. function console.cat(a,b,al,bl)
  104. local at,al=txtbl(a,al)
  105. local bt,bl=txtbl(b,bl)
  106. if #at==#bt then
  107. for l1=1,#bt do
  108. bt[l1]=at[l1]..bt[l1]
  109. end
  110. return table.concat(bt,"\n")
  111. end
  112. if al==0 then
  113. return table.concat(bt,"\n")
  114. elseif bl==0 then
  115. return table.concat(at,"\n")
  116. end
  117. local ml=math.max(#at,#bt)
  118. for l1=1,math.floor((#bt-#at)/2) do
  119. table.insert(at,1,(" "):rep(al))
  120. end
  121. for l1=#at+1,ml do
  122. table.insert(at,(" "):rep(al))
  123. end
  124. for l1=1,math.floor((#at-#bt)/2) do
  125. table.insert(bt,1,(" "):rep(bl))
  126. end
  127. for l1=#bt+1,ml do
  128. table.insert(bt,(" "):rep(bl))
  129. end
  130. for l1=1,ml do
  131. at[l1]=at[l1]..bt[l1]
  132. end
  133. return table.concat(at,"\n")
  134. end
  135.  
  136. function console.table(t)
  137. local ncols=0
  138. local nrows=0
  139. for k,v in pairs(t) do
  140. nrows=math.max(nrows,k)
  141. for n,l in pairs(v) do
  142. ncols=math.max(ncols,n)
  143. end
  144. end
  145. local wcols={}
  146. local hrows={}
  147. for l1=1,nrows do
  148. for l2=1,ncols do
  149. local d,mx=txtbl(t[l1][l2] or "")
  150. wcols[l2]=math.max(wcols[l2] or 0,mx)
  151. hrows[l1]=math.max(hrows[l1] or 0,#d)
  152. end
  153. end
  154. local sp={}
  155. for l1=1,ncols do
  156. table.insert(sp,(box.lr):rep(wcols[l1]))
  157. end
  158. local ocols={box.rd..table.concat(sp,box.lrd)..box.ld}
  159. for l1=1,nrows do
  160. local orow={}
  161. for l2=1,ncols do
  162. table.insert(orow,table.concat(txtbl(t[l1][l2] or "",wcols[l2]),"\n"))
  163. table.insert(orow,(box.ud.."\n"):rep(hrows[l1]))
  164. end
  165. local o=(box.ud.."\n"):rep(hrows[l1])
  166. for l2=1,#orow do
  167. o=console.cat(o,orow[l2])
  168. end
  169. table.insert(ocols,o)
  170. table.insert(ocols,l1==nrows and (box.ur..table.concat(sp,box.lur)..box.lu) or (box.udr..table.concat(sp,box.x)..box.lud))
  171. end
  172. return table.concat(ocols,"\n")
  173. end
  174.  
  175. local _serialize
  176. function _serialize(t,r)
  177. r=r or {}
  178. if r[t] then
  179. return tostring(t)
  180. end
  181. local tpe=type(t)
  182. if tpe=="table" then
  183. if getmetatable(v) and getmetatable(v).__tostring then
  184. return tostring(v)
  185. end
  186. local err,res=pcall(function()
  187. local ok={}
  188. local ov={}
  189. local u={}
  190. local n=1
  191. r[t]=true
  192. while t[n]~=nil do
  193. u[n]=true
  194. table.insert(ok," ")
  195. table.insert(ov,console.cat(" ",_serialize(t[n],r)))
  196. n=n+1
  197. end
  198. local oi={}
  199. for k,v in pairs(t) do
  200. if not u[k] then
  201. table.insert(oi,{k,v})
  202. end
  203. end
  204. if #oi==0 then
  205. for l1=1,#ov do
  206. ov[l1]=ov[l1]:sub(4)
  207. end
  208. end
  209. table.sort(oi,function(a,b)
  210. return tostring(a[1])<tostring(b[1])
  211. end)
  212. for k,v in ipairs(oi) do
  213. table.insert(ok,_serialize(v[1],r))
  214. table.insert(ov,console.cat(" = ",_serialize(v[2],r)))
  215. end
  216. if #ok==0 then
  217. return box.e
  218. end
  219. local _
  220. local kl=0
  221. for k,v in pairs(ok) do
  222. if v~=" " then
  223. _,kl=txtbl(v,kl)
  224. end
  225. end
  226. if kl==0 then
  227. return console.box(table.concat(ov,"\n"))
  228. end
  229. local vl=0
  230. for k,v in pairs(ov) do
  231. _,vl=txtbl(v,vl)
  232. end
  233. local o=""
  234. for l1=1,#ok do
  235. o=o..console.cat(ok[l1],ov[l1],kl,vl).."\n"
  236. end
  237. r[t]=nil
  238. return console.box(o)
  239. end)
  240. return err and res or tostring(t)
  241. elseif tpe=="number" then
  242. if t~=t then
  243. return "nan"
  244. elseif t==math.huge then
  245. return "inf"
  246. elseif t==-math.huge then
  247. return "-inf"
  248. end
  249. return tostring(t)
  250. elseif tpe=="string" then
  251. local o=string.format("%q",t):gsub("\\\n","\\n"):gsub("%z","\\z")
  252. return o
  253. else
  254. return tostring(t)
  255. end
  256. end
  257.  
  258. function console.serialize(t)
  259. return _serialize(t)
  260. end
  261.  
  262. return console
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement