-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansicolors2.lua
133 lines (62 loc) · 1.92 KB
/
ansicolors2.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
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 assert = _tl_compat and _tl_compat.assert or assert; local os = _tl_compat and _tl_compat.os or os; local package = _tl_compat and _tl_compat.package or package; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table
local function isWindows()
return type(package) == 'table' and type(package.config) == 'string' and package.config:sub(1, 1) == '\\'
end
local supported = not isWindows()
if isWindows() then supported = os.getenv("ANSICON") end
local keys = {
["reset"] = 0,
["bright"] = 1,
["dim"] = 2,
["underline"] = 4,
["blink"] = 5,
["reverse"] = 7,
["hidden"] = 8,
["black"] = 30,
["red"] = 31,
["green"] = 32,
["yellow"] = 33,
["blue"] = 34,
["magenta"] = 35,
["cyan"] = 36,
["white"] = 37,
["blackbg"] = 40,
["redbg"] = 41,
["greenbg"] = 42,
["yellowbg"] = 43,
["bluebg"] = 44,
["magentabg"] = 45,
["cyanbg"] = 46,
["whitebg"] = 47,
}
local escapeString = string.char(27) .. '[%dm'
local function escapeNumber(num)
return escapeString:format(num)
end
local function escapeKeys(str)
if not supported then return "" end
local buffer = {}
local num
for word in str:gmatch("%w+") do
num = keys[word]
assert(num, "Unknown key: " .. word)
table.insert(buffer, escapeNumber(num))
end
return table.concat(buffer)
end
local function replaceCodes(str)
str = string.gsub(str, "(%%{(.-)})",
function(_, s)
return escapeKeys(s)
end)
return str
end
local function ansicolors(str)
str = tostring(str or '')
return replaceCodes('%{reset}' .. str .. '%{reset}')
end
local R = {}
local export = {
ansicolors = ansicolors,
}
return export