color.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import sys
  17. import pager
  18. COLORS = {None :-1,
  19. 'normal' :-1,
  20. 'black' : 0,
  21. 'red' : 1,
  22. 'green' : 2,
  23. 'yellow' : 3,
  24. 'blue' : 4,
  25. 'magenta': 5,
  26. 'cyan' : 6,
  27. 'white' : 7}
  28. ATTRS = {None :-1,
  29. 'bold' : 1,
  30. 'dim' : 2,
  31. 'ul' : 4,
  32. 'blink' : 5,
  33. 'reverse': 7}
  34. RESET = "\033[m" # pylint: disable=W1401
  35. # backslash is not anomalous
  36. def is_color(s):
  37. return s in COLORS
  38. def is_attr(s):
  39. return s in ATTRS
  40. def _Color(fg = None, bg = None, attr = None):
  41. fg = COLORS[fg]
  42. bg = COLORS[bg]
  43. attr = ATTRS[attr]
  44. if attr >= 0 or fg >= 0 or bg >= 0:
  45. need_sep = False
  46. code = "\033[" #pylint: disable=W1401
  47. if attr >= 0:
  48. code += chr(ord('0') + attr)
  49. need_sep = True
  50. if fg >= 0:
  51. if need_sep:
  52. code += ';'
  53. need_sep = True
  54. if fg < 8:
  55. code += '3%c' % (ord('0') + fg)
  56. else:
  57. code += '38;5;%d' % fg
  58. if bg >= 0:
  59. if need_sep:
  60. code += ';'
  61. need_sep = True
  62. if bg < 8:
  63. code += '4%c' % (ord('0') + bg)
  64. else:
  65. code += '48;5;%d' % bg
  66. code += 'm'
  67. else:
  68. code = ''
  69. return code
  70. DEFAULT = None
  71. def SetDefaultColoring(state):
  72. """Set coloring behavior to |state|.
  73. This is useful for overriding config options via the command line.
  74. """
  75. if state is None:
  76. # Leave it alone -- return quick!
  77. return
  78. global DEFAULT
  79. state = state.lower()
  80. if state in ('auto',):
  81. DEFAULT = state
  82. elif state in ('always', 'yes', 'true', True):
  83. DEFAULT = 'always'
  84. elif state in ('never', 'no', 'false', False):
  85. DEFAULT = 'never'
  86. class Coloring(object):
  87. def __init__(self, config, section_type):
  88. self._section = 'color.%s' % section_type
  89. self._config = config
  90. self._out = sys.stdout
  91. on = DEFAULT
  92. if on is None:
  93. on = self._config.GetString(self._section)
  94. if on is None:
  95. on = self._config.GetString('color.ui')
  96. if on == 'auto':
  97. if pager.active or os.isatty(1):
  98. self._on = True
  99. else:
  100. self._on = False
  101. elif on in ('true', 'always'):
  102. self._on = True
  103. else:
  104. self._on = False
  105. def redirect(self, out):
  106. self._out = out
  107. @property
  108. def is_on(self):
  109. return self._on
  110. def write(self, fmt, *args):
  111. self._out.write(fmt % args)
  112. def flush(self):
  113. self._out.flush()
  114. def nl(self):
  115. self._out.write('\n')
  116. def printer(self, opt=None, fg=None, bg=None, attr=None):
  117. s = self
  118. c = self.colorer(opt, fg, bg, attr)
  119. def f(fmt, *args):
  120. s._out.write(c(fmt, *args))
  121. return f
  122. def nofmt_printer(self, opt=None, fg=None, bg=None, attr=None):
  123. s = self
  124. c = self.nofmt_colorer(opt, fg, bg, attr)
  125. def f(fmt):
  126. s._out.write(c(fmt))
  127. return f
  128. def colorer(self, opt=None, fg=None, bg=None, attr=None):
  129. if self._on:
  130. c = self._parse(opt, fg, bg, attr)
  131. def f(fmt, *args):
  132. output = fmt % args
  133. return ''.join([c, output, RESET])
  134. return f
  135. else:
  136. def f(fmt, *args):
  137. return fmt % args
  138. return f
  139. def nofmt_colorer(self, opt=None, fg=None, bg=None, attr=None):
  140. if self._on:
  141. c = self._parse(opt, fg, bg, attr)
  142. def f(fmt):
  143. return ''.join([c, fmt, RESET])
  144. return f
  145. else:
  146. def f(fmt):
  147. return fmt
  148. return f
  149. def _parse(self, opt, fg, bg, attr):
  150. if not opt:
  151. return _Color(fg, bg, attr)
  152. v = self._config.GetString('%s.%s' % (self._section, opt))
  153. if v is None:
  154. return _Color(fg, bg, attr)
  155. v = v.strip().lower()
  156. if v == "reset":
  157. return RESET
  158. elif v == '':
  159. return _Color(fg, bg, attr)
  160. have_fg = False
  161. for a in v.split(' '):
  162. if is_color(a):
  163. if have_fg:
  164. bg = a
  165. else:
  166. fg = a
  167. elif is_attr(a):
  168. attr = a
  169. return _Color(fg, bg, attr)