Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconsole.lua
More file actions
Latest commit
267 lines (250 loc) · 6.25 KB
/
Copy pathconsole.lua
File metadata and controls
267 lines (250 loc) · 6.25 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
--[[
ConsoleUtils by PixelToast, public domain
> console.box("puts a box\naround some text")
┌────────────────┐
│puts a box │
│around some text│
└────────────────┘
> console.cat(console.box("cats\nsome\ntext")," with more text")
┌────┐
│cats│
│some│ with more text
│text│
└────┘
> console.table({{"first","column"},{"second\ncolumn"},{"foo","potato walrus","bar"}})
┌──────┬─────────────┬───┐
│first │column │ │
├──────┼─────────────┼───┤
│second│ │ │
│column│ │ │
├──────┼─────────────┼───┤
│foo │potato walrus│bar│
└──────┴─────────────┴───┘
> console.serialize({key="value",table={list={1,2,"three"},d="alphabetical order",c=1,b=2,a=3}})
┌─────────────────────────────────────────┐
│"key" = "value" │
│ ┌─────────────────────────────┐│
│ │"a" = 3 ││
│ │"b" = 2 ││
│ │"c" = 1 ││
│ │"d" = "alphabetical order"││
│"table" = │ ┌───────┐ ││
│ │ │1 │ ││
│ │"list" = │2 │ ││
│ │ │"three"│ ││
│ │ └───────┘ ││
│ └─────────────────────────────┘│
└─────────────────────────────────────────┘
]]
localconsole={}
localbox={
ud="│",
lr="─",
ur="└",
rd="┌",
ld="┐",
lu="┘",
lrd="┬",
udr="├",
x="┼",
lud="┤",
lur="┴",
e="[]"
}
--[[local box={
ud="|",
lr="-",
ur="\\",
rd="/",
ld="\\",
lu="/",
lrd="-",
udr="|",
x="+",
lud="|",
lur="-",
e="[]"
}]]
localunicode=require("unicode")
localfunctiontxtbl(s,mx)
mx=mxor0
ifs=="" andmx>0then
s=""
end
localo={}
forcins:gmatch("[^\r\n]+") do
table.insert(o,c)
end
forl1=1,#odo
mx=math.max(mx,unicode.len(o[l1]))
end
forl1=1,#odo
o[l1]=o[l1]..(""):rep(mx-unicode.len(o[l1]))
end
returno,mx
end
functionconsole.box(s)
localt,l=txtbl(s)
forl1=1,#tdo
t[l1]=box.ud..t[l1]..box.ud
end
table.insert(t,1,box.rd..(box.lr:rep(l))..box.ld)
table.insert(t,box.ur..(box.lr:rep(l))..box.lu)
returntable.concat(t,"\n")
end
functionconsole.cat(a,b,al,bl)
localat,al=txtbl(a,al)
localbt,bl=txtbl(b,bl)
if#at==#btthen
forl1=1,#btdo
bt[l1]=at[l1]..bt[l1]
end
returntable.concat(bt,"\n")
end
ifal==0then
returntable.concat(bt,"\n")
elseifbl==0then
returntable.concat(at,"\n")
end
localml=math.max(#at,#bt)
forl1=1,math.floor((#bt-#at)/2) do
table.insert(at,1,(""):rep(al))
end
forl1=#at+1,mldo
table.insert(at,(""):rep(al))
end
forl1=1,math.floor((#at-#bt)/2) do
table.insert(bt,1,(""):rep(bl))
end
forl1=#bt+1,mldo
table.insert(bt,(""):rep(bl))
end
forl1=1,mldo
at[l1]=at[l1]..bt[l1]
end
returntable.concat(at,"\n")
end
functionconsole.table(t)
localncols=0
localnrows=0
fork,vinpairs(t) do
nrows=math.max(nrows,k)
forn,linpairs(v) do
ncols=math.max(ncols,n)
end
end
localwcols={}
localhrows={}
forl1=1,nrowsdo
forl2=1,ncolsdo
locald,mx=txtbl(t[l1][l2] or"")
wcols[l2]=math.max(wcols[l2] or0,mx)
hrows[l1]=math.max(hrows[l1] or0,#d)
end
end
localsp={}
forl1=1,ncolsdo
table.insert(sp,(box.lr):rep(wcols[l1]))
end
localocols={box.rd..table.concat(sp,box.lrd)..box.ld}
forl1=1,nrowsdo
localorow={}
forl2=1,ncolsdo
table.insert(orow,table.concat(txtbl(t[l1][l2] or"",wcols[l2]),"\n"))
table.insert(orow,(box.ud.."\n"):rep(hrows[l1]))
end
localo=(box.ud.."\n"):rep(hrows[l1])
forl2=1,#orowdo
o=console.cat(o,orow[l2])
end
table.insert(ocols,o)
table.insert(ocols,l1==nrowsand (box.ur..table.concat(sp,box.lur)..box.lu) or (box.udr..table.concat(sp,box.x)..box.lud))
end
returntable.concat(ocols,"\n")
end
local_serialize
function_serialize(t,r)
r=ror {}
ifr[t] then
returntostring(t)
end
localtpe=type(t)
iftpe=="table" then
ifgetmetatable(v) andgetmetatable(v).__tostringthen
returntostring(v)
end
localerr,res=pcall(function()
localok={}
localov={}
localu={}
localn=1
r[t]=true
whilet[n]~=nildo
u[n]=true
table.insert(ok,"")
table.insert(ov,console.cat("",_serialize(t[n],r)))
n=n+1
end
localoi={}
fork,vinpairs(t) do
ifnotu[k] then
table.insert(oi,{k,v})
end
end
if#oi==0then
forl1=1,#ovdo
ov[l1]=ov[l1]:sub(4)
end
end
table.sort(oi,function(a,b)
returntostring(a[1])<tostring(b[1])
end)
fork,vinipairs(oi) do
table.insert(ok,_serialize(v[1],r))
table.insert(ov,console.cat(" = ",_serialize(v[2],r)))
end
if#ok==0then
returnbox.e
end
local_
localkl=0
fork,vinpairs(ok) do
ifv~="" then
_,kl=txtbl(v,kl)
end
end
ifkl==0then
returnconsole.box(table.concat(ov,"\n"))
end
localvl=0
fork,vinpairs(ov) do
_,vl=txtbl(v,vl)
end
localo=""
forl1=1,#okdo
o=o..console.cat(ok[l1],ov[l1],kl,vl).."\n"
end
r[t]=nil
returnconsole.box(o)
end)
returnerrandresortostring(t)
elseiftpe=="number" then
ift~=tthen
return"nan"
elseift==math.hugethen
return"inf"
elseift==-math.hugethen
return"-inf"
end
returntostring(t)
elseiftpe=="string" then
localo=string.format("%q",t):gsub("\\\n","\\n"):gsub("%z","\\z")
returno
else
returntostring(t)
end
end
functionconsole.serialize(t)
return_serialize(t)
end
returnconsole