| 1 |
local colormt = {} |
| 2 |
local ansicolors = {} |
| 3 |
|
| 4 |
local rspamd_util = require "rspamd_util" |
| 5 |
local isatty = rspamd_util.isatty() |
| 6 |
|
| 7 |
function colormt:__tostring() |
| 8 |
return self.value |
| 9 |
end |
| 10 |
|
| 11 |
function colormt:__concat(other) |
| 12 |
return tostring(self) .. tostring(other) |
| 13 |
end |
| 14 |
|
| 15 |
function colormt:__call(s) |
| 16 |
return self .. s .. ansicolors.reset |
| 17 |
end |
| 18 |
|
| 19 |
colormt.__metatable = {} |
| 20 |
local function makecolor(value) |
| 21 |
if isatty then |
| 22 |
return setmetatable({ |
| 23 |
value = string.char(27) .. '[' .. tostring(value) .. 'm' |
| 24 |
}, colormt) |
| 25 |
else |
| 26 |
return setmetatable({ |
| 27 |
value = '' |
| 28 |
}, colormt) |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
local colors = { |
| 33 |
-- attributes |
| 34 |
reset = 0, |
| 35 |
clear = 0, |
| 36 |
bright = 1, |
| 37 |
dim = 2, |
| 38 |
underscore = 4, |
| 39 |
blink = 5, |
| 40 |
reverse = 7, |
| 41 |
hidden = 8, |
| 42 |
|
| 43 |
-- foreground |
| 44 |
black = 30, |
| 45 |
red = 31, |
| 46 |
green = 32, |
| 47 |
yellow = 33, |
| 48 |
blue = 34, |
| 49 |
magenta = 35, |
| 50 |
cyan = 36, |
| 51 |
white = 37, |
| 52 |
|
| 53 |
-- background |
| 54 |
onblack = 40, |
| 55 |
onred = 41, |
| 56 |
ongreen = 42, |
| 57 |
onyellow = 43, |
| 58 |
onblue = 44, |
| 59 |
onmagenta = 45, |
| 60 |
oncyan = 46, |
| 61 |
onwhite = 47, |
| 62 |
} |
| 63 |
|
| 64 |
for c, v in pairs(colors) do |
| 65 |
ansicolors[c] = makecolor(v) |
| 66 |
end |
| 67 |
|
| 68 |
return ansicolors |