color.py 4.3 KB

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