Repository navigation
Expand file tree
/
Copy pathtabular.lua
More file actions
308 lines (253 loc) · 7.38 KB
/
Copy pathtabular.lua
File metadata and controls
308 lines (253 loc) · 7.38 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local utf8 = _tl_compat and _tl_compat.utf8 or utf8
local tabular = {}
local ansicolors_ok, ansicolors = pcall(require, "ansicolors")
local draw = {
NW = "/",
NE = "\\",
SW = "\\",
SE = "/",
N = "+",
S = "+",
E = "+",
W = "+",
V = "|",
H = "-",
X = "+",
}
local colors = {
ansicolors_ok and ansicolors.noReset("%{cyan}") or "",
ansicolors_ok and ansicolors.noReset("%{white}") or "",
}
local function strlen(s)
s = s:gsub("\27[^m]*m", "")
return #s
end
local strsub = string.sub
if (os.getenv("LANG") or ""):upper():match("UTF%-?8") then
draw = {
NW = "┌",
NE = "┐",
SW = "└",
SE = "┘",
N = "┬",
S = "┴",
E = "┤",
W = "├",
V = "│",
H = "─",
X = "┼",
}
strlen = function(s)
s = s:gsub("\27[^m]*m", "")
return utf8.len(s) or #s
end
strsub = function(s, i, j)
local uj = utf8.offset(s, j + 1)
if uj then
uj = uj - 1
end
return s:sub(utf8.offset(s, i), uj)
end
end
local show
local show_as_columns
local function output_line(out, line)
table.insert(out, line)
out.width = math.max(out.width or 0, strlen(line))
end
local function escape_chars(c)
return "\\" .. string.byte(c)
end
local function show_as_list(t, color, seen, ids, skip_array)
local tt = {}
local width = 0
local keys = {}
for k, v in pairs(t) do
if not skip_array or type(k) ~= "number" then
table.insert(tt, { k, v })
keys[k] = tostring(k)
width = math.max(width, strlen(keys[k]))
end
end
table.sort(tt, function(a, b)
if type(a[1]) == "number" and type(b[1]) == "number" then
return a[1] < b[1]
else
return tostring(a[1]) < tostring(b[1])
end
end)
for i = 1, #tt do
local k = keys[tt[i][1]]
tt[i][1] = k .. " " .. ("."):rep(width - strlen(k)) .. ":"
end
return show_as_columns(tt, color, seen, ids, nil, true)
end
local function show_primitive(t)
local out = {}
local s = tostring(t)
if utf8.len(s) then
s = s:gsub("[\n\t]", {
["\n"] = "\\n",
["\t"] = "\\t",
})
else
s = s:gsub("[%z-\31\127-\255]", escape_chars)
end
if strlen(s) > 80 then
for i = 1, strlen(s), 80 do
output_line(out, strsub(s, i, i + 79))
end
else
output_line(out, s)
end
return out
end
show_as_columns = function(t, bgcolor, seen, ids, column_order, skip_header)
local columns = {}
local row_heights = {}
local column_names
local column_set
if column_order then
column_names = column_order
column_set = {}
for _, cname in ipairs(column_names) do
column_set[cname] = true
end
end
for i, row in ipairs(t) do
if type(row) == "table" then
for k, v in pairs(row) do
local sk = tostring(k)
if (not column_set) or column_set[sk] then
if not columns[sk] then
columns[sk] = {}
columns[sk].width = strlen(sk)
end
local sv = show(v, bgcolor and colors[(i % #colors) + 1], seen, ids)
columns[sk][i] = sv
columns[sk].width = math.max(columns[sk].width, sv.width)
row_heights[i] = math.max(row_heights[i] or 0, #sv)
end
end
end
end
if not column_order then
column_names = {}
column_set = {}
for name, _row in pairs(columns) do
if not column_set[name] then
table.insert(column_names, name)
column_set[name] = true
end
end
table.sort(column_names)
end
local function output_cell(line, cname, text, color)
local w = columns[cname].width
text = text or ""
if color then
table.insert(line, color)
elseif bgcolor then
table.insert(line, bgcolor)
end
table.insert(line, text .. (" "):rep((w - strlen(text))))
if color then
table.insert(line, bgcolor)
end
table.insert(line, draw.V)
end
local out = {}
local border_top = {}
local border_bot = {}
for i, cname in ipairs(column_names) do
local w = columns[cname].width
table.insert(border_top, draw.H:rep(w))
table.insert(border_bot, draw.H:rep(w))
if i < #column_names then
table.insert(border_top, draw.N)
table.insert(border_bot, draw.S)
end
end
table.insert(border_top, 1, draw.NW)
table.insert(border_bot, 1, draw.SW)
table.insert(border_top, draw.NE)
table.insert(border_bot, draw.SE)
output_line(out, table.concat(border_top))
if not skip_header then
local line = { draw.V }
local sep = { draw.V }
for _, cname in ipairs(column_names) do
output_cell(line, cname, cname)
output_cell(sep, cname, draw.H:rep(strlen(cname)))
end
output_line(out, table.concat(line))
output_line(out, table.concat(sep))
end
for i = 1, #t do
for h = 1, row_heights[i] or 1 do
local line = { draw.V }
for _, cname in ipairs(column_names) do
local row = columns[cname][i]
output_cell(line, cname, row and row[math.floor(h)] or "", bgcolor and colors[(i % #colors) + 1])
end
output_line(out, table.concat(line))
end
end
output_line(out, table.concat(border_bot))
local mt = t
for k, _v in pairs(mt) do
if type(k) ~= "number" then
local out2 = show_as_list(mt, bgcolor, seen, ids, true)
for _, line in ipairs(out2) do
output_line(out, line)
end
break
end
end
return out
end
show = function(t, color, seen, ids, column_order)
if type(t) == "table" and seen[t] then
local msg = "<see " .. ids[t] .. ">"
return { msg, width = strlen(msg) }
end
seen[t] = true
if type(t) == "table" then
local tt = t
if #(tt) > 0 and type(tt[1]) == "table" then
return show_as_columns(tt, color, seen, ids, column_order)
else
return show_as_list(tt, color, seen, ids)
end
else
return show_primitive(t)
end
end
local function detect_cycles(t, n, seen)
n = n or 0
seen = seen or {}
if type(t) == "table" then
if seen[t] then
return seen, n
end
n = n + 1
seen[t] = n
for _k, v in pairs(t) do
seen, n = detect_cycles(v, n, seen)
end
end
return seen, n
end
function tabular.show(t, column_order, color)
local ids = detect_cycles(t)
return table.concat(show(t, color and colors and ansicolors_ok and ansicolors.noReset("%{reset}"), {}, ids, column_order), "\n")
end
if arg and arg[0]:match("tabular%..*$") then
print(tabular.show(_G, nil, true))
os.exit(0)
end
return setmetatable(tabular, {
__call = function(_, ...)
return tabular.show(...)
end,
})