-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathansify.lua
More file actions
665 lines (545 loc) · 15.3 KB
/
ansify.lua
File metadata and controls
665 lines (545 loc) · 15.3 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
---@diagnostic disable: missing-fields
local concat = table.concat
local ceil = math.ceil
local find = string.find
local floor = math.floor
local fmt = string.format
local gsub = string.gsub
local match = string.match
local rep = string.rep
local sub = string.sub
---@type ansify
local M = {
foregrounds = {},
backgrounds = {},
modifiers = {},
}
local ESC = "\27"
local function sgr(code)
return ESC .. "[" .. code .. "m"
end
local RESET = sgr(0)
local SGR_PATTERN = ESC .. "%[[0-9;]*m"
local escaped_chars = { [ESC] = "\\27", ["\n"] = "\\n", ["\t"] = "\\t" }
local color_names = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" }
local style_names = { "bold", "dim", "italic", "underline", "inverse", "strikethrough" }
local alignments = { left = true, right = true, center = true }
---@type table<(ansify.Style|ansify.styleName),ansify.Style>
local style_defs = {}
-- stylua: ignore
local style_codes = {
bold = 1,
dim = 2,
italic = 3,
underline = 4,
inverse = 7,
strikethrough = 9,
}
local function type_label(v)
return v == nil and "no value" or type(v)
end
local function assert_table(argn, v, fname)
if type(v) ~= "table" then
error(fmt("bad argument #%d to '%s' (table expected, got %s)", argn, fname, type_label(v)), 3)
end
return v
end
local function assert_string(argn, v, fname)
if type(v) ~= "string" then
error(fmt("bad argument #%d to '%s' (string expected, got %s)", argn, fname, type_label(v)), 3)
end
return v
end
local function assert_number(argn, v, fname)
if type(v) ~= "number" then
error(fmt("bad argument #%d to '%s' (number expected, got %s)", argn, fname, type_label(v)), 3)
end
return v
end
local function tbl_clear(t)
for name in pairs(t) do
t[name] = nil
end
end
local function register_style(style)
style_defs[style.name] = style
style_defs[style] = style
end
local function visible_len(text)
return #(gsub(text, SGR_PATTERN, ""))
end
---@type fun(mods:table<ansify.modifierName,true>,fg:integer?,bg:integer?):string
local function style_prefix(mods, fg, bg)
local codes = {}
for i = 1, #style_names do
local name = style_names[i]
if mods[name] then
codes[#codes + 1] = style_defs[name].code
end
end
if fg then
codes[#codes + 1] = fg
end
if bg then
codes[#codes + 1] = bg
end
return #codes == 0 and "" or (ESC .. "[" .. concat(codes, ";") .. "m")
end
-- luacheck: ignore
---@type fun(style_def:ansify.Style|ansify.styleName,mods:table<ansify.styleName,true>,fg:integer?,bg:integer?):(fg:integer?,bg:integer?)
local function apply_style(style_def, mods, fg, bg)
local style = style_defs[style_def]
if not style then
error(fmt("unknown style: %s", style_def), 3)
end
local category = style.category
if category == "modifier" then
mods[style.name] = true
elseif category == "foreground" then
fg = style.code
else
bg = style.code
end
return fg, bg
end
local function push(parts, s, prefix, emitted)
if s == "" then
return emitted
end
if prefix == "" then
if emitted ~= "" then
parts[#parts + 1] = RESET
emitted = ""
end
else
parts[#parts + 1] = prefix
emitted = prefix
end
parts[#parts + 1] = s
return emitted
end
local Style = setmetatable({
__tostring = function(self)
return self.value
end,
__concat = function(left, right)
return tostring(left) .. tostring(right)
end,
__call = function(self, text)
if text == nil then
return tostring(self)
end
assert_string(1, text, self.name)
return tostring(self) .. text .. RESET
end,
}, {
__call = function(style, name, category, code, value)
return setmetatable({
name = name,
category = category,
code = code,
value = value,
}, style)
end,
})
for i = 1, #color_names do
local name = color_names[i]
local code = 29 + i
local fg = Style(name, "foreground", code, sgr(code))
local bg = Style("on_" .. name, "background", code + 10, sgr(code + 10))
local bright_fg = Style("bright_" .. name, "foreground", code + 60, sgr(code + 60))
local bright_bg = Style("on_bright_" .. name, "background", code + 70, sgr(code + 70))
register_style(fg)
register_style(bg)
register_style(bright_fg)
register_style(bright_bg)
M.foregrounds[name] = fg
M.foregrounds["bright_" .. name] = bright_fg
M.backgrounds[name] = bg
M.backgrounds["bright_" .. name] = bright_bg
end
for i = 1, #style_names do
local name = style_names[i]
local style = Style(name, "modifier", style_codes[name], sgr(style_codes[name]))
M.modifiers[name] = style
register_style(style)
end
local function close_parts(parts, active_prefix)
if active_prefix ~= "" then
parts[#parts + 1] = RESET
end
return concat(parts)
end
local function close_word(words, parts, active_prefix)
if #parts == 0 then
return
end
words[#words + 1] = close_parts(parts, active_prefix)
end
local function open_active_prefix(parts, active_prefix)
if active_prefix ~= "" then
parts[#parts + 1] = active_prefix
end
end
local function update_active_prefix(active_prefix, code)
return code == RESET and "" or (active_prefix .. code)
end
local function each_chunk(text, on_chunk, on_code)
local idx = 1
while idx <= #text do
local start_i, end_i = find(text, SGR_PATTERN, idx)
if start_i == idx then
if not end_i then
break
end
local out = on_code(sub(text, idx, end_i))
if out ~= nil then
return out
end
idx = end_i + 1
else
local next_escape = start_i or (#text + 1)
local out = on_chunk(sub(text, idx, next_escape - 1))
if out ~= nil then
return out
end
idx = next_escape
end
end
end
local function close_line(parts, active_prefix)
return #parts == 0 and "" or close_parts(parts, active_prefix)
end
local function each_line(text, emit)
local active_prefix = ""
local parts = {}
local ended_with_newline = false
each_chunk(text, function(chunk)
local chunk_start = 1
while chunk_start <= #chunk do
local newline = find(chunk, "\n", chunk_start, true)
local piece = sub(chunk, chunk_start, newline and (newline - 1) or #chunk)
if piece ~= "" then
if #parts == 0 then
open_active_prefix(parts, active_prefix)
end
parts[#parts + 1] = piece
end
if not newline then
ended_with_newline = false
break
end
emit(close_line(parts, active_prefix))
parts = {}
chunk_start = newline + 1
ended_with_newline = true
end
end, function(code)
if #parts > 0 then
parts[#parts + 1] = code
end
active_prefix = update_active_prefix(active_prefix, code)
end)
if #parts > 0 or #text == 0 or ended_with_newline then
emit(close_line(parts, active_prefix))
end
end
local function slice_visible(text, start, stop)
local parts = {}
local visible = 0
local active_prefix = ""
local started = false
local sliced = each_chunk(text, function(chunk)
local chunk_stop = visible + #chunk
if chunk_stop >= start then
local chunk_start = visible + 1
local slice_start = start > chunk_start and ceil(start - visible) or 1
local slice_stop = stop < chunk_stop and floor(stop - visible) or #chunk
if slice_start <= slice_stop then
if not started then
started = true
open_active_prefix(parts, active_prefix)
end
parts[#parts + 1] = sub(chunk, slice_start, slice_stop)
end
visible = chunk_stop
if visible >= stop then
if started and active_prefix ~= "" then
parts[#parts + 1] = RESET
end
return concat(parts)
end
end
visible = chunk_stop
end, function(code)
if started then
parts[#parts + 1] = code
end
active_prefix = update_active_prefix(active_prefix, code)
end)
if sliced then
return sliced
end
if started and active_prefix ~= "" then
parts[#parts + 1] = RESET
end
return concat(parts)
end
local function slice_range(text, start, stop, visible_length)
local len = visible_length or visible_len(text)
if len == 0 or start > len then
return ""
end
if not stop or stop > len then
stop = len
end
return stop < start and "" or slice_visible(text, start, stop)
end
local function wrap_line(line, width)
local words = M.words(line)
if #words == 0 then
return { "" }
end
local lines = {}
local current_line = ""
local current_width = 0
for i = 1, #words do
local word = words[i]
local remaining = word
local remaining_width = visible_len(remaining)
while remaining_width > width do
if current_width > 0 then
lines[#lines + 1] = current_line
current_line = ""
current_width = 0
end
lines[#lines + 1] = slice_range(remaining, 1, width, remaining_width)
remaining = slice_range(remaining, width + 1, nil, remaining_width)
remaining_width = remaining_width - width
end
if current_width == 0 then
current_line = remaining
current_width = remaining_width
elseif current_width + 1 + remaining_width <= width then
current_line = current_line .. " " .. remaining
current_width = current_width + 1 + remaining_width
else
lines[#lines + 1] = current_line
current_line = remaining
current_width = remaining_width
end
end
if current_width > 0 then
lines[#lines + 1] = current_line
end
return lines
end
function M.strip(text)
assert_string(1, text, "strip")
return (gsub(text, SGR_PATTERN, ""))
end
function M.escape(v)
assert_string(1, v, "escape")
return (gsub(v, "[" .. ESC .. "\n\t]", escaped_chars))
end
function M.has_ansi(text)
assert_string(1, text, "has_ansi")
return find(text, SGR_PATTERN) ~= nil
end
function M.len(text)
assert_string(1, text, "len")
return visible_len(text)
end
function M.align(text, width, direction)
assert_string(1, text, "align")
assert_string(3, direction, "align")
assert_number(2, width, "align")
width = floor(width)
if not alignments[direction] then
error(fmt("bad argument #3 to '%s' (invalid alignment: %s)", "align", direction), 2)
end
local padding = width - visible_len(text)
if padding <= 0 then
return text
end
if direction == "right" then
return rep(" ", padding) .. text
end
if direction == "center" then
local lpad = floor(padding / 2)
return rep(" ", lpad) .. text .. rep(" ", padding - lpad)
end
return text .. rep(" ", padding)
end
function M.words(text)
assert_string(1, text, "words")
local words = {}
local active_prefix = ""
local parts = {}
each_chunk(text, function(chunk)
local idx = 1
while idx <= #chunk do
local start_i, end_i = find(chunk, "%S+", idx)
if not start_i then
close_word(words, parts, active_prefix)
parts = {}
break
end
if start_i > idx then
close_word(words, parts, active_prefix)
parts = {}
end
if #parts == 0 then
open_active_prefix(parts, active_prefix)
end
parts[#parts + 1] = sub(chunk, start_i, end_i)
idx = end_i + 1
end
end, function(code)
if #parts > 0 then
parts[#parts + 1] = code
end
active_prefix = update_active_prefix(active_prefix, code)
end)
close_word(words, parts, active_prefix)
return words
end
function M.lines(text)
assert_string(1, text, "lines")
local lines = {}
each_line(text, function(line)
if visible_len(line) > 0 then
lines[#lines + 1] = line
end
end)
return lines
end
function M.wrap(text, width)
assert_string(1, text, "wrap")
assert_number(2, width, "wrap")
width = floor(width)
width = width < 1 and 1 or width
local wrapped = {}
each_line(text, function(line)
local lines = wrap_line(line, width)
for i = 1, #lines do
wrapped[#wrapped + 1] = lines[i]
end
end)
return concat(wrapped, "\n")
end
function M.append(text, suffix)
assert_string(1, text, "append")
assert_string(2, suffix, "append")
local reset_start = #text + 1
while reset_start > #RESET do
local candidate = reset_start - #RESET
if sub(text, candidate, reset_start - 1) ~= RESET then
break
end
reset_start = candidate
end
if reset_start <= #text then
return sub(text, 1, reset_start - 1) .. suffix .. sub(text, reset_start)
end
return text .. suffix
end
function M.prepend(text, prefix)
assert_string(1, text, "prepend")
assert_string(2, prefix, "prepend")
local idx = 1
local prefix_end = 0
while idx <= #text do
local start, stop = find(text, SGR_PATTERN, idx)
if start ~= idx or not stop then
break
end
prefix_end = stop
idx = stop + 1
end
if prefix_end > 0 then
return sub(text, 1, prefix_end) .. prefix .. sub(text, prefix_end + 1)
end
return prefix .. text
end
function M.slice(text, start, stop)
assert_string(1, text, "slice")
start = assert_number(2, start == nil and 1 or start, "slice")
start = floor(start)
start = start < 1 and 1 or start
if stop ~= nil then
assert_number(3, stop, "slice")
stop = stop < 1 and 1 or stop
end
return slice_range(text, start, stop)
end
function M.truncate(text, w)
assert_string(1, text, "truncate")
assert_number(2, w, "truncate")
return visible_len(text) > w and slice_range(text, 1, w) or text
end
function M.format(text)
assert_string(1, text, "format")
local parts = {}
local mods = {} ---@type table<ansify.modifierName,true>
local fg, bg
local active_prefix = ""
local emitted_prefix = ""
local idx = 1
while idx <= #text do
local open = find(text, "{", idx, true)
if not open then
emitted_prefix = push(parts, sub(text, idx), active_prefix, emitted_prefix)
break
end
emitted_prefix = push(parts, sub(text, idx, open - 1), active_prefix, emitted_prefix)
if sub(text, open + 1, open + 1) == "{" then
local escaped_close = find(text, "}}", open + 2, true)
if not escaped_close then
return text
end
emitted_prefix = push(parts, "{" .. sub(text, open + 2, escaped_close - 1) .. "}", active_prefix, emitted_prefix)
idx = escaped_close + 2
else
local close = find(text, "}", open + 1, true)
if not close then
return text
end
local tag = match(sub(text, open + 1, close - 1), "^%s*(.-)%s*$")
if tag == "" then
return ""
end
if tag == "normal" then
tbl_clear(mods)
fg, bg = nil, nil
elseif not style_defs[tag] then
return ""
else
fg, bg = apply_style(tag, mods, fg, bg)
end
active_prefix = style_prefix(mods, fg, bg)
idx = close + 1
end
end
if emitted_prefix ~= "" then
parts[#parts + 1] = RESET
end
return concat(parts)
end
function M.style(style, text)
assert_table(1, style, "style")
assert_string(2, text, "style")
local mods = {} ---@type table<ansify.modifierName,true>
local fg, bg
for i = 1, #style do
local style_def = style[i]
if not style_defs[style_def] and type(style_def) ~= "string" then
error(fmt("invalid value (%s) at index %d in table for 'style'", type_label(style_def), i), 2)
end
fg, bg = apply_style(style_def, mods, fg, bg)
end
local prefix = style_prefix(mods, fg, bg)
return prefix == "" and text or (prefix .. text .. RESET)
end
M.modifiers.reset = Style("reset", "modifier", 0, RESET)
return M