1local colormt = {}
2local ansicolors = {}
3
4local rspamd_util = require "rspamd_util"
5local isatty = rspamd_util.isatty()
6
7function colormt:__tostring()
8  return self.value
9end
10
11function colormt:__concat(other)
12  return tostring(self) .. tostring(other)
13end
14
15function colormt:__call(s)
16  return self .. s .. ansicolors.reset
17end
18
19colormt.__metatable = {}
20local 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
30end
31
32local 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
64for c, v in pairs(colors) do
65  ansicolors[c] = makecolor(v)
66end
67
68return ansicolors