forked from Fundament-Software/Alicorn0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty-printer.lua
294 lines (266 loc) · 6.33 KB
/
pretty-printer.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---@class PrettyPrint
local PrettyPrint = {}
local PrettyPrint_mt = { __index = PrettyPrint }
local prettyprintable = require "./pretty-printable-trait"
local kind_field = "kind"
local hidden_fields = {
[kind_field] = true,
capture = function(capture)
if capture.bindings and capture.bindings.len then
-- FIXME: we can't print all the bindings for a capture currently because we
-- capture everything in scope and that's way too verbose
-- if that gets fixed to only capture used bindings we can print more
-- local ret = {}
-- for i = 1, capture.bindings:len() do
-- ret[i] = capture.bindings:get(i)
-- end
-- return ret
return "runtime context with len=" .. tostring(capture.bindings:len())
end
return capture
end,
}
---@return PrettyPrint
function PrettyPrint.new()
return setmetatable({}, PrettyPrint_mt)
end
---@param unknown any
---@param ... any
function PrettyPrint:any(unknown, ...)
local ty = type(unknown)
if ty == "string" then
self[#self + 1] = string.format("%q", unknown)
elseif ty == "table" then
if self.depth and self.depth > 50 then
self[#self + 1] = "DEPTH LIMIT EXCEEDED"
return
end
local mt = getmetatable(unknown)
local via_trait = mt and prettyprintable[mt]
if via_trait and via_trait.print then
via_trait.print(unknown, self, ...)
elseif mt and mt.__tostring then
self[#self + 1] = tostring(unknown)
else
self:table(unknown)
end
else
self[#self + 1] = tostring(unknown)
end
end
function PrettyPrint:_prefix()
if self.prefix then
self[#self + 1] = self.prefix
end
end
function PrettyPrint:_indent()
if not self.prefix then
self.prefix = " "
else
self.prefix = self.prefix .. " "
end
end
function PrettyPrint:_enter()
self.depth = (self.depth or 0) + 1
end
function PrettyPrint:_exit()
self.depth = self.depth - 1
end
function PrettyPrint:_dedent()
if self.prefix and #self.prefix > 1 then
self.prefix = string.sub(self.prefix, 1, #self.prefix - 1)
else
self.prefix = nil
end
end
local colors = {
"\27[38;5;226m",
"\27[38;5;255m",
"\27[38;5;135m",
"\27[38;2;40;40;40m",
}
function PrettyPrint:_color()
return colors[1 + (((self.depth or 0) + #colors - 1) % #colors)]
end
function PrettyPrint:_resetcolor()
return "\27[0m"
end
---@param array any[]
function PrettyPrint:array(array, ...)
self:_enter()
self[#self + 1] = self:_color()
self[#self + 1] = "["
self[#self + 1] = self:_resetcolor()
for i, v in ipairs(array) do
if i > 1 then
self[#self + 1] = ", "
end
self:any(v, ...)
end
self[#self + 1] = self:_color()
self[#self + 1] = "]"
self[#self + 1] = self:_resetcolor()
self:_exit()
end
---@param fields table
function PrettyPrint:table(fields, ...)
-- i considered keeping track of a path of tables
-- but it turned really horrible
-- just grep its address until you find the original
self[#self + 1] = "<"
self[#self + 1] = tostring(fields)
self[#self + 1] = ">"
self.table_tracker = self.table_tracker or {}
if self.table_tracker[fields] then
return
end
self.table_tracker[fields] = true
self:_enter()
local count = 0
local num = 0
local nums = {}
for k, v in pairs(fields) do
if k == "kind" then
self[#self + 1] = " "
self[#self + 1] = fields.kind
elseif hidden_fields[k] then
-- nothing
elseif type(k) == "number" then
num = num + 1
nums[k] = true
else
count = count + 1
end
end
local seq = false
if count == 0 and #nums == num then
seq = true
end
if seq then
self[#self + 1] = self:_color()
self[#self + 1] = "["
self[#self + 1] = self:_resetcolor()
for i, v in ipairs(fields) do
if i > 1 then
self[#self + 1] = ", "
end
self:any(v, ...)
end
self[#self + 1] = self:_color()
self[#self + 1] = "]"
self[#self + 1] = self:_resetcolor()
-- elseif num == 0 and count == 1 then
-- self[#self + 1] = self:_color()
-- self[#self + 1] = "{"
-- self[#self + 1] = self:_resetcolor()
-- for k, v in pairs(fields) do
-- if not hidden_fields[k] then
-- self:any(v, ...)
-- end
-- end
-- self[#self + 1] = self:_color()
-- self[#self + 1] = "}"
-- self[#self + 1] = self:_resetcolor()
else
self[#self + 1] = self:_color()
self[#self + 1] = " {\n"
self[#self + 1] = self:_resetcolor()
self:_indent()
for k, v in pairs(fields) do
if not hidden_fields[k] then
self:_prefix()
self[#self + 1] = k
self[#self + 1] = self:_color()
self[#self + 1] = " = "
self[#self + 1] = self:_resetcolor()
self:any(v, ...)
self[#self + 1] = ",\n"
end
end
self:_dedent()
self:_prefix()
self[#self + 1] = self:_color()
self[#self + 1] = "}"
self[#self + 1] = self:_resetcolor()
end
self:_exit()
end
---@param kind string
---@param fields table
function PrettyPrint:record(kind, fields, ...)
local startLen = #self
self:_enter()
self[#self + 1] = self:_color()
if kind then
self[#self + 1] = kind
end
if #fields <= 1 then
--self[#self + 1] = self:_color()
local k, v = table.unpack(fields[1])
if hidden_fields[k] then
v = hidden_fields[k](v)
end
self[#self + 1] = "("
self[#self + 1] = self:_resetcolor()
self:any(v, ...)
self[#self + 1] = self:_color()
self[#self + 1] = ")"
else
--self[#self + 1] = self:_color()
self[#self + 1] = " {\n"
self[#self + 1] = self:_resetcolor()
self:_indent()
for _, pair in ipairs(fields) do
local k, v = table.unpack(pair)
if hidden_fields[k] then
v = hidden_fields[k](v)
end
self:_prefix()
self[#self + 1] = k
self[#self + 1] = self:_color()
self[#self + 1] = " = "
self[#self + 1] = self:_resetcolor()
self:any(v, ...)
self[#self + 1] = ",\n"
end
self[#self + 1] = self:_color()
-- if the record is big mark what's ending
if (#self - startLen) > 50 then
self:_prefix()
self[#self + 1] = "--end "
self[#self + 1] = kind
self[#self + 1] = "\n"
end
self:_dedent()
self:_prefix()
self[#self + 1] = "}"
end
self[#self + 1] = self:_resetcolor()
self:_exit()
end
---@param name string
function PrettyPrint:unit(name)
self[#self + 1] = name
end
function PrettyPrint_mt:__tostring()
return table.concat(self, "")
end
local function s(...)
local res = {}
for i, v in ipairs { ... } do
local pp = PrettyPrint:new()
pp:any(v)
res[i] = tostring(pp)
end
return table.concat(res, " ")
end
local function p(...)
print(s(...))
end
_G["p"] = p
return {
PrettyPrint = PrettyPrint,
prettyprintable = prettyprintable,
s = s,
p = p,
}