You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansicolors.lua 1.1KB

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