-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpretty-print-ffi.lua
364 lines (331 loc) · 9.02 KB
/
pretty-print-ffi.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
--[[
Copyright 2014-2015 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
--[[lit-meta
name = "luvit/pretty-print"
version = "2.0.1"
homepage = "https://github.com/luvit/luvit/blob/master/deps/pretty-print.lua"
description = "A lua value pretty printer and colorizer for terminals."
tags = {"colors", "tty"}
license = "Apache 2"
author = { name = "Tim Caswell" }
]]
local loop = require 'uv-ffi'
local getenv = require('os').getenv
local prettyPrint, dump, strip, color, colorize, loadColors
local theme = {}
local useColors = false
local defaultTheme
local stdout, stdin, stderr, width
local quote, quote2, dquote, dquote2, obracket, cbracket, obrace, cbrace, comma, equals, controls
local themes = {
-- nice color theme using 16 ansi colors
[16] = {
property = '0;37', -- white
sep = '1;30', -- bright-black
braces = '1;30', -- bright-black
['nil'] = '1;30', -- bright-black
boolean = '0;33', -- yellow
number = '1;33', -- bright-yellow
string = '0;32', -- green
quotes = '1;32', -- bright-green
escape = '1;32', -- bright-green
['function'] = '0;35', -- purple
thread = '1;35', -- bright-purple
table = '1;34', -- bright blue
userdata = '1;36', -- bright cyan
cdata = '0;36', -- cyan
err = '1;31', -- bright red
success = '1;33;42', -- bright-yellow on green
failure = '1;33;41', -- bright-yellow on red
highlight = '1;36;44' -- bright-cyan on blue
},
-- nice color theme using ansi 256-mode colors
[256] = {
property = '38;5;253',
braces = '38;5;247',
sep = '38;5;240',
['nil'] = '38;5;244',
boolean = '38;5;220', -- yellow-orange
number = '38;5;202', -- orange
string = '38;5;34', -- darker green
quotes = '38;5;40', -- green
escape = '38;5;46', -- bright green
['function'] = '38;5;129', -- purple
thread = '38;5;199', -- pink
table = '38;5;27', -- blue
userdata = '38;5;39', -- blue2
cdata = '38;5;69', -- teal
err = '38;5;196', -- bright red
success = '38;5;120;48;5;22', -- bright green on dark green
failure = '38;5;215;48;5;52', -- bright red on dark red
highlight = '38;5;45;48;5;236' -- bright teal on dark grey
}
}
local special = {
[7] = 'a',
[8] = 'b',
[9] = 't',
[10] = 'n',
[11] = 'v',
[12] = 'f',
[13] = 'r'
}
function strip(str)
return string.gsub(str, '\027%[[^m]*m', '')
end
function loadColors(index)
if index == nil then
index = defaultTheme
end
-- Remove the old theme
for key in pairs(theme) do
theme[key] = nil
end
if index then
local new = themes[index]
if not new then
error('Invalid theme index: ' .. tostring(index))
end
-- Add the new theme
for key in pairs(new) do
theme[key] = new[key]
end
useColors = true
else
useColors = false
end
quote = colorize('quotes', "'", 'string')
quote2 = colorize('quotes', "'")
dquote = colorize('quotes', '"', 'string')
dquote2 = colorize('quotes', '"')
obrace = colorize('braces', '{ ')
cbrace = colorize('braces', '}')
obracket = colorize('property', '[')
cbracket = colorize('property', ']')
comma = colorize('sep', ', ')
equals = colorize('sep', ' = ')
controls = {}
for i = 0, 31 do
local c = special[i]
if not c then
if i < 10 then
c = '00' .. tostring(i)
else
c = '0' .. tostring(i)
end
end
controls[i] = colorize('escape', '\\' .. c, 'string')
end
controls[92] = colorize('escape', '\\\\', 'string')
controls[34] = colorize('escape', '\\"', 'string')
controls[39] = colorize('escape', "\\'", 'string')
for i = 128, 255 do
local c
if i < 100 then
c = '0' .. tostring(i)
else
c = tostring(i)
end
controls[i] = colorize('escape', '\\' .. c, 'string')
end
end
function color(colorName)
return '\27[' .. (theme[colorName] or '0') .. 'm'
end
function colorize(colorName, string, resetName)
return useColors and (color(colorName) .. tostring(string) .. color(resetName)) or
tostring(string)
end
local function stringEscape(c)
return controls[string.byte(c, 1)]
end
function dump(value, recurse, nocolor)
local seen = {}
local output = {}
local offset = 0
local stack = {}
local function recalcOffset(index)
for i = index + 1, #output do
local m = string.match(output[i], '\n([^\n]*)$')
if m then
offset = #(strip(m))
else
offset = offset + #(strip(output[i]))
end
end
end
local function write(text, length)
if not length then
length = #(strip(text))
end
-- Create room for data by opening parent blocks
-- Start at the root and go down.
local i = 1
while offset + length > width and stack[i] do
local entry = stack[i]
if not entry.opened then
entry.opened = true
table.insert(output, entry.index + 1, '\n' .. string.rep(' ', i))
-- Recalculate the offset
recalcOffset(entry.index)
-- Bump the index of all deeper entries
for j = i + 1, #stack do
stack[j].index = stack[j].index + 1
end
end
i = i + 1
end
output[#output + 1] = text
offset = offset + length
if offset > width then
return dump(stack)
end
end
local function indent()
stack[#stack + 1] = {
index = #output,
opened = false
}
end
local function unindent()
stack[#stack] = nil
end
local function process(localValue)
local typ = type(localValue)
if typ == 'string' then
if string.find(localValue, "'") and not string.find(localValue, '"') then
write(dquote)
write(string.gsub(localValue, '[%c\\\128-\255]', stringEscape))
write(dquote2)
else
write(quote)
write(string.gsub(localValue, "[%c\\'\128-\255]", stringEscape))
write(quote2)
end
elseif typ == 'table' and not seen[localValue] then
if not recurse then
seen[localValue] = true
end
write(obrace)
local i = 1
-- Count the number of keys so we know when to stop adding commas
local total = 0
for _ in pairs(localValue) do
total = total + 1
end
local nextIndex = 1
for k, v in pairs(localValue) do
indent()
if k == nextIndex then
-- if the key matches the last numerical index + 1
-- This is how lists print without keys
nextIndex = k + 1
process(v)
else
if type(k) == 'string' and string.find(k, '^[%a_][%a%d_]*$') then
write(colorize('property', k))
write(equals)
else
write(obracket)
process(k)
write(cbracket)
write(equals)
end
if type(v) == 'table' then
process(v)
else
indent()
process(v)
unindent()
end
end
if i < total then
write(comma)
else
write(' ')
end
i = i + 1
unindent()
end
write(cbrace)
else
write(colorize(typ, tostring(localValue)))
end
end
process(value)
local s = table.concat(output)
return nocolor and strip(s) or s
end
-- Print replacement that goes through libuv. This is useful on windows
-- to use libuv's code to translate ansi escape codes to windows API calls.
local oldPrint = _G.print
local function print(...)
local n = select('#', ...)
local arguments = {...}
for i = 1, n do
arguments[i] = tostring(arguments[i])
end
stdout:write(table.concat(arguments, '\t') .. '\n')
end
function prettyPrint(...)
local n = select('#', ...)
local arguments = {...}
for i = 1, n do
arguments[i] = dump(arguments[i])
end
print(unpack(arguments))
end
if loop.guessHandle(0) == 'tty' then
stdin = loop:newTty(0)
else
stdin = loop:newPipe(false)
stdin:open(0)
end
if loop.guessHandle(1) == 'tty' then
stdout = loop:newTty(1)
width = stdout:getWinsize()
if width == 0 then
width = 80
end
-- auto-detect when 16 color mode should be used
local term = getenv('TERM')
if term and (term == 'xterm' or term:find '-256color$') then
defaultTheme = 256
else
defaultTheme = 16
end
else
stdout = loop:newPipe(false)
stdout:open(1)
width = 80
end
loadColors()
if loop.guessHandle(2) == 'tty' then
stderr = loop:newTty(2)
else
stderr = loop:newPipe(false)
stderr:open(2)
end
return {
loadColors = loadColors,
theme = theme,
print = print,
prettyPrint = prettyPrint,
dump = dump,
color = color,
colorize = colorize,
stdin = stdin,
stdout = stdout,
stderr = stderr,
strip = strip
}