termcolor.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # coding: utf-8
  2. # Copyright (c) 2008-2011 Volvox Development Team
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Author: Konstantin Lepa <konstantin.lepa@gmail.com>
  23. """ANSI Color formatting for output in terminal."""
  24. from __future__ import print_function
  25. import os
  26. __ALL__ = [ 'colored', 'cprint' ]
  27. VERSION = (1, 1, 0)
  28. ATTRIBUTES = dict(
  29. list(zip([
  30. 'bold',
  31. 'dark',
  32. '',
  33. 'underline',
  34. 'blink',
  35. '',
  36. 'reverse',
  37. 'concealed'
  38. ],
  39. list(range(1, 9))
  40. ))
  41. )
  42. del ATTRIBUTES['']
  43. HIGHLIGHTS = dict(
  44. list(zip([
  45. 'on_grey',
  46. 'on_red',
  47. 'on_green',
  48. 'on_yellow',
  49. 'on_blue',
  50. 'on_magenta',
  51. 'on_cyan',
  52. 'on_white'
  53. ],
  54. list(range(40, 48))
  55. ))
  56. )
  57. COLORS = dict(
  58. list(zip([
  59. 'grey',
  60. 'red',
  61. 'green',
  62. 'yellow',
  63. 'blue',
  64. 'magenta',
  65. 'cyan',
  66. 'white',
  67. ],
  68. list(range(30, 38))
  69. ))
  70. )
  71. RESET = '\033[0m'
  72. def colored(text, color=None, on_color=None, attrs=None):
  73. """Colorize text.
  74. Available text colors:
  75. red, green, yellow, blue, magenta, cyan, white.
  76. Available text highlights:
  77. on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
  78. Available attributes:
  79. bold, dark, underline, blink, reverse, concealed.
  80. Example:
  81. colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
  82. colored('Hello, World!', 'green')
  83. """
  84. if os.getenv('ANSI_COLORS_DISABLED') is None:
  85. fmt_str = '\033[%dm%s'
  86. if color is not None:
  87. text = fmt_str % (COLORS[color], text)
  88. if on_color is not None:
  89. text = fmt_str % (HIGHLIGHTS[on_color], text)
  90. if attrs is not None:
  91. for attr in attrs:
  92. text = fmt_str % (ATTRIBUTES[attr], text)
  93. text += RESET
  94. return text
  95. def cprint(text, color=None, on_color=None, attrs=None, **kwargs):
  96. """Print colorize text.
  97. It accepts arguments of print function.
  98. """
  99. print((colored(text, color, on_color, attrs)), **kwargs)
  100. if __name__ == '__main__':
  101. print('Current terminal type: %s' % os.getenv('TERM'))
  102. print('Test basic colors:')
  103. cprint('Grey color', 'grey')
  104. cprint('Red color', 'red')
  105. cprint('Green color', 'green')
  106. cprint('Yellow color', 'yellow')
  107. cprint('Blue color', 'blue')
  108. cprint('Magenta color', 'magenta')
  109. cprint('Cyan color', 'cyan')
  110. cprint('White color', 'white')
  111. print(('-' * 78))
  112. print('Test highlights:')
  113. cprint('On grey color', on_color='on_grey')
  114. cprint('On red color', on_color='on_red')
  115. cprint('On green color', on_color='on_green')
  116. cprint('On yellow color', on_color='on_yellow')
  117. cprint('On blue color', on_color='on_blue')
  118. cprint('On magenta color', on_color='on_magenta')
  119. cprint('On cyan color', on_color='on_cyan')
  120. cprint('On white color', color='grey', on_color='on_white')
  121. print('-' * 78)
  122. print('Test attributes:')
  123. cprint('Bold grey color', 'grey', attrs=['bold'])
  124. cprint('Dark red color', 'red', attrs=['dark'])
  125. cprint('Underline green color', 'green', attrs=['underline'])
  126. cprint('Blink yellow color', 'yellow', attrs=['blink'])
  127. cprint('Reversed blue color', 'blue', attrs=['reverse'])
  128. cprint('Concealed Magenta color', 'magenta', attrs=['concealed'])
  129. cprint('Bold underline reverse cyan color', 'cyan',
  130. attrs=['bold', 'underline', 'reverse'])
  131. cprint('Dark blink concealed white color', 'white',
  132. attrs=['dark', 'blink', 'concealed'])
  133. print(('-' * 78))
  134. print('Test mixing:')
  135. cprint('Underline red on grey color', 'red', 'on_grey',
  136. ['underline'])
  137. cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])