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
|
--!A cross-platform build utility based on Lua
--
-- 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.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file colors.lua
--
-- define module
local colors = colors or {}
-- load modules
local tty -- lazy loading it
local emoji = require("base/emoji")
-- the color8 keys
--
-- from https://github.com/hoelzro/ansicolors
--
colors._keys8 =
{
-- attributes
reset = 0
, clear = 0
, default = 0
, bright = 1
, dim = 2
, underline = 4
, blink = 5
, reverse = 7
, hidden = 8
-- foreground
, black = 30
, red = 31
, green = 32
, yellow = 33
, blue = 34
, magenta = 35
, cyan = 36
, white = 37
-- background
, onblack = 40
, onred = 41
, ongreen = 42
, onyellow = 43
, onblue = 44
, onmagenta = 45
, oncyan = 46
, onwhite = 47
}
-- the color256 keys
--
-- from https://github.com/hoelzro/ansicolors
--
colors._keys256 =
{
-- attributes
reset = 0
, clear = 0
, default = 0
, bright = 1
, dim = 2
, underline = 4
, blink = 5
, reverse = 7
, hidden = 8
-- foreground
, black = "38;5;0"
, red = "38;5;1"
, green = "38;5;2"
, yellow = "38;5;3"
, blue = "38;5;4"
, magenta = "38;5;5"
, cyan = "38;5;6"
, white = "38;5;7"
-- background
, onblack = "48;5;0"
, onred = "48;5;1"
, ongreen = "48;5;2"
, onyellow = "48;5;3"
, onblue = "48;5;4"
, onmagenta = "48;5;5"
, oncyan = "48;5;6"
, onwhite = "48;5;7"
}
-- the 24bits color keys
--
-- from https://github.com/hoelzro/ansicolors
--
colors._keys24 =
{
-- attributes
reset = 0
, clear = 0
, default = 0
, bright = 1
, dim = 2
, underline = 4
, blink = 5
, reverse = 7
, hidden = 8
-- foreground
, black = "38;2;0;0;0"
, red = "38;2;255;0;0"
, green = "38;2;0;255;0"
, yellow = "38;2;255;255;0"
, blue = "38;2;0;0;255"
, magenta = "38;2;255;0;255"
, cyan = "38;2;0;255;255"
, white = "38;2;255;255;255"
-- background
, onblack = "48;2;0;0;0"
, onred = "48;2;255;0;0"
, ongreen = "48;2;0;255;0"
, onyellow = "48;2;255;255;0"
, onblue = "48;2;0;0;255"
, onmagenta = "48;2;255;0;255"
, oncyan = "48;2;0;255;255"
, onwhite = "48;2;255;255;255"
}
-- the escape string
colors._ESC = '\x1b[%sm'
-- make rainbow truecolor code by the index of characters
--
-- @param index the index of characters
-- @param seed the seed, 0-255, default: random
-- @param freq the frequency, default: 0.1
-- @param spread the spread, default: 3.0
--
--
function colors.rainbow24(index, seed, freq, spread)
-- init values
seed = seed
freq = freq or 0.1
spread = spread or 3.0
index = seed + index / spread
-- make colors
local red = math.sin(freq * index + 0) * 127 + 128
local green = math.sin(freq * index + 2 * math.pi / 3) * 127 + 128
local blue = math.sin(freq * index + 4 * math.pi / 3) * 127 + 128
-- make code
return string.format("%d;%d;%d", math.floor(red), math.floor(green), math.floor(blue))
end
-- make rainbow color256 code by the index of characters (16-256)
--
-- @param index the index of characters
-- @param seed the seed, 0-255, default: random
-- @param freq the frequency, default: 0.1
-- @param spread the spread, default: 3.0
--
--
function colors.rainbow256(index, seed, freq, spread)
-- init values
seed = seed
freq = freq or 0.1
spread = spread or 3.0
index = seed + index / spread
-- make color code
local code = math.floor((freq * index) % 240 + 18)
-- make code
return string.format("#%d", code)
end
-- translate colors from the string
--
-- @param str the string with colors
-- @param opt options
-- patch_reset: wrap str with `"${reset}"`?
-- ignore_unknown: ignore unknown codes like `"${unknown_code}"`?
-- plain: false
--
-- 8 colors:
--
-- "${red}hello"
-- "${onred}hello${clear} xmake"
-- "${bright red underline}hello"
-- "${dim red}hello"
-- "${blink red}hello"
-- "${reverse red}hello xmake"
--
-- 256 colors:
--
-- "${#255}hello"
-- "${on#255}hello${clear} xmake"
-- "${bright #255; underline}hello"
-- "${bright on#255 #10}hello${clear} xmake"
--
-- true colors:
--
-- "${255;0;0}hello"
-- "${on;255;0;0}hello${clear} xmake"
-- "${bright 255;0;0 underline}hello"
-- "${bright on;255;0;0 0;255;0}hello${clear} xmake"
--
-- emoji:
--
-- "${beer}hello${beer}world"
--
-- theme:
-- "${color.error}"
-- "${bright color.warning}"
--
-- text:
-- "${hello xmake}"
-- "${hello xmake $beer}"
--
-- @param str the string with color/emoji markup
-- @param opt the options, e.g. {patch_reset = true, ignore = false}
-- @return the translated string with ANSI escape codes
--
function colors.translate(str, opt)
-- check string
if not str then
return nil
end
opt = opt or {}
-- get theme
local theme = colors.theme()
-- patch reset
if opt.patch_reset ~= false then
str = "${reset}" .. str .. "${reset}"
end
-- translate color blocks, e.g. ${red}, ${color.xxx}, ${emoji}
tty = tty or require("base/tty")
local has_color8 = tty.has_color8()
local has_color256 = tty.has_color256()
local has_color24 = tty.has_color24()
local has_emoji = tty.has_emoji()
str = str:gsub("(%${(.-)})", function(_, word)
-- not supported? ignore it
local nocolors = false
if not has_color8 and not has_color256 and not has_color24 then
nocolors = true
end
-- is plain theme? no colors and no emoji
local noemoji = not has_emoji
if opt.plain or (theme and theme:name() == "plain") then
nocolors = true
noemoji = true
end
-- get keys
local keys = has_color256 and colors._keys256 or colors._keys8
if has_color24 then
keys = colors._keys24
end
-- split words
local blocks_raw = word:split(' ', {plain = true})
-- translate theme color first, e.g ${color.error}
local blocks = {}
for _, block in ipairs(blocks_raw) do
if theme then
local theme_block = theme:get(block)
if theme_block then
for _, theme_block_sub in ipairs(theme_block:split(' ', {plain = true})) do
table.insert(blocks, theme_block_sub)
end
else
table.insert(blocks, block)
end
elseif block:startswith("color.") or block:startswith("text.") then
local default_theme = {["color.error"] = "red", ["color.warning"] = "yellow", ["text.error"] = "error", ["text.warning"] = "warning"}
local theme_block = default_theme[block]
if theme_block then
table.insert(blocks, theme_block)
else
table.insert(blocks, block)
end
else
table.insert(blocks, block)
end
end
-- make color buffer
local text_buffer = {}
local color_buffer = {}
for _, block in ipairs(blocks) do
-- get the color code
local code = keys[block]
if not code then
if has_color24 and block:find(";", 1, true) then
if block:startswith("on;") then
code = block:gsub("on;", "48;2;")
else
code = "38;2;" .. block
end
elseif has_color256 and block:find("#", 1, true) then
if block:startswith("on#") then
code = block:gsub("on#", "48;5;")
else
code = block:gsub("#", "38;5;")
end
elseif block:startswith("$") then
-- plain text, do not translate emoji
table.insert(text_buffer, block:sub(2))
elseif not noemoji then
-- get emoji code
local emoji_code = emoji.translate(block)
if emoji_code then
table.insert(text_buffer, emoji_code)
elseif not opt.ignore_unknown then
-- unknown code, regard as plain text
table.insert(text_buffer, block)
end
elseif not opt.ignore_unknown then
table.insert(text_buffer, block)
end
end
-- save this code
table.insert(color_buffer, code)
end
-- make result
local result = ""
if #color_buffer > 0 and not nocolors then
result = result .. colors._ESC:format(table.concat(color_buffer, ";"))
end
if #text_buffer > 0 then
result = result .. table.concat(text_buffer, " ")
end
return result
end)
return str
end
-- ignore all colors, strip color markup from string
--
-- @param str the string with color markup
-- @return the plain string without colors
--
function colors.ignore(str)
if str then
-- strip "${red}" and "${theme color}"
str = colors.translate(str, {plain = true})
-- strip color code, e.g. for clang/gcc color diagnostics output
return (str:gsub("\x1b%[.-m", ""))
end
end
-- get theme
function colors.theme()
return colors._THEME
end
-- set theme
function colors.theme_set(theme)
colors._THEME = theme
end
-- return module
return colors
|