ansicolors.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- ansicolors.lua v1.0.2 (2012-08)
  2. -- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
  3. -- Copyright (c) 2011 Enrique García Cota <enrique.garcia.cota@gmail.com>
  4. --
  5. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  6. -- of this software and associated documentation files (the "Software"), to deal
  7. -- in the Software without restriction, including without limitation the rights
  8. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. -- copies of the Software, and to permit persons to whom the Software is
  10. -- furnished to do so, subject to the following conditions:
  11. --
  12. -- The above copyright notice and this permission notice shall be included in
  13. -- all copies or substantial portions of the Software.
  14. --
  15. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. -- THE SOFTWARE.
  22. -- support detection
  23. local function isWindows()
  24. return type(package) == 'table' and type(package.config) == 'string' and package.config:sub(1,1) == '\\'
  25. end
  26. local supported = not isWindows()
  27. if isWindows() then supported = os.getenv("ANSICON") end
  28. local keys = {
  29. -- reset
  30. reset = 0,
  31. -- misc
  32. bright = 1,
  33. dim = 2,
  34. underline = 4,
  35. blink = 5,
  36. reverse = 7,
  37. hidden = 8,
  38. -- foreground colors
  39. black = 30,
  40. red = 31,
  41. green = 32,
  42. yellow = 33,
  43. blue = 34,
  44. magenta = 35,
  45. cyan = 36,
  46. white = 37,
  47. lred = 91,
  48. -- background colors
  49. blackbg = 40,
  50. redbg = 41,
  51. greenbg = 42,
  52. yellowbg = 43,
  53. bluebg = 44,
  54. magentabg = 45,
  55. cyanbg = 46,
  56. whitebg = 47
  57. }
  58. local escapeString = string.char(27) .. '[%dm'
  59. local function escapeNumber(number)
  60. return escapeString:format(number)
  61. end
  62. local function escapeKeys(str)
  63. if not supported then return "" end
  64. local buffer = {}
  65. local number
  66. for word in str:gmatch("%w+") do
  67. number = keys[word]
  68. assert(number, "Unknown key: " .. word)
  69. table.insert(buffer, escapeNumber(number) )
  70. end
  71. return table.concat(buffer)
  72. end
  73. local function replaceCodes(str)
  74. str = string.gsub(str,"(%%{(.-)})", function(_, str) return escapeKeys(str) end )
  75. return str
  76. end
  77. -- public
  78. local function ansicolors( str )
  79. str = tostring(str or '')
  80. return replaceCodes('%{reset}' .. str .. '%{reset}')
  81. end
  82. return setmetatable({noReset = replaceCodes}, {__call = function (_, str) return ansicolors (str) end})