Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

color.lua 4.7 KiB

2 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. -- color.lua
  2. --------------------------------------------------------------------------------
  3. -- A super-simple way to make colored text output in Lua.
  4. -- To use, simply print out things from this module, then print out some text.
  5. --
  6. -- Example:
  7. -- print(color.bg.green .. color.fg.RED .. "This is bright red on green")
  8. -- print(color.invert .. "This is inverted..." .. color.reset .. " And this isn't.")
  9. -- print(color.fg(0xDE) .. color.bg(0xEE) .. "You can use xterm-256 colors too!" .. color.reset)
  10. -- print("And also " .. color.bold .. "BOLD" .. color.normal .. " if you want.")
  11. -- print(color.bold .. color.fg.BLUE .. color.bg.blue .. "Miss your " .. color.fg.RED .. "C-64" .. color.fg.BLUE .. "?" .. color.reset)
  12. --
  13. -- You can see all these examples in action by calling color.test()
  14. --
  15. -- Can't pick a good color scheme? Look at a handy chart:
  16. -- print(color.chart())
  17. --
  18. -- If you want to add anything to this, check out the Wikipedia page on ANSI control codes:
  19. -- http://en.wikipedia.org/wiki/ANSI_escape_code
  20. --------------------------------------------------------------------------------
  21. -- Copyright (C) 2012 Ross Andrews
  22. --
  23. -- This program is free software: you can redistribute it and/or modify
  24. -- it under the terms of the GNU Lesser General Public License as published by
  25. -- the Free Software Foundation, either version 3 of the License, or
  26. -- (at your option) any later version.
  27. --
  28. -- This program is distributed in the hope that it will be useful,
  29. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. -- GNU General Public License for more details.
  32. --
  33. -- You should have received a copy of the GNU Lesser General Public License
  34. -- along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  35. --------------------------------------------------------------------------------
  36. -- A note about licensing:
  37. --
  38. -- The LGPL isn't really intended to be used with non-compiled libraries. The way
  39. -- I interpret derivative works of this library is this: if you don't modify this
  40. -- file, and the program it's embedded in doesn't modify the Lua table it defines,
  41. -- then you can distribute it with a program under any license. If you do either
  42. -- of those things, then you've created a derivative work of this library and you
  43. -- have to release the modifications you made under this same license.
  44. local color = { _NAME = "color" }
  45. local _M = color
  46. local esc = string.char(27, 91)
  47. local names = {'black', 'red', 'green', 'yellow', 'blue', 'pink', 'cyan', 'white'}
  48. local hi_names = {'BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'PINK', 'CYAN', 'WHITE'}
  49. color.fg, color.bg = {}, {}
  50. for i, name in ipairs(names) do
  51. color.fg[name] = esc .. tostring(30+i-1) .. 'm'
  52. _M[name] = color.fg[name]
  53. color.bg[name] = esc .. tostring(40+i-1) .. 'm'
  54. end
  55. for i, name in ipairs(hi_names) do
  56. color.fg[name] = esc .. tostring(90+i-1) .. 'm'
  57. _M[name] = color.fg[name]
  58. color.bg[name] = esc .. tostring(100+i-1) .. 'm'
  59. end
  60. local function fg256(_,n)
  61. return esc .. "38;5;" .. n .. 'm'
  62. end
  63. local function bg256(_,n)
  64. return esc .. "48;5;" .. n .. 'm'
  65. end
  66. setmetatable(color.fg, {__call = fg256})
  67. setmetatable(color.bg, {__call = bg256})
  68. color.reset = esc .. '0m'
  69. color.clear = esc .. '2J'
  70. color.bold = esc .. '1m'
  71. color.faint = esc .. '2m'
  72. color.normal = esc .. '22m'
  73. color.invert = esc .. '7m'
  74. color.underline = esc .. '4m'
  75. color.hide = esc .. '?25l'
  76. color.show = esc .. '?25h'
  77. function color.move(x, y)
  78. return esc .. y .. ';' .. x .. 'H'
  79. end
  80. color.home = color.move(1, 1)
  81. --------------------------------------------------
  82. function color.chart(ch,col)
  83. local cols = '0123456789abcdef'
  84. ch = ch or ' '
  85. col = col or color.fg.black
  86. local str = color.reset .. color.bg.WHITE .. col
  87. for y = 0, 15 do
  88. for x = 0, 15 do
  89. local lbl = cols:sub(x+1, x+1)
  90. if x == 0 then lbl = cols:sub(y+1, y+1) end
  91. str = str .. color.bg.black .. color.fg.WHITE .. lbl
  92. str = str .. color.bg(x+y*16) .. col .. ch
  93. end
  94. str = str .. color.reset .. "\n"
  95. end
  96. return str .. color.reset
  97. end
  98. function color.test()
  99. print(color.reset .. color.bg.green .. color.fg.RED .. "This is bright red on green" .. color.reset)
  100. print(color.invert .. "This is inverted..." .. color.reset .. " And this isn't.")
  101. print(color.fg(0xDE) .. color.bg(0xEE) .. "You can use xterm-256 colors too!" .. color.reset)
  102. print("And also " .. color.bold .. "BOLD" .. color.normal .. " if you want.")
  103. print(color.bold .. color.fg.BLUE .. color.bg.blue .. "Miss your " .. color.fg.RED .. "C-64" .. color.fg.BLUE .. "?" .. color.reset)
  104. print("Try printing " .. color.underline .. _M._NAME .. ".chart()" .. color.reset)
  105. end
  106. return color