color.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class Coloring(object):
  71. def __init__(self, config, section_type):
  72. self._section = 'color.%s' % section_type
  73. self._config = config
  74. self._out = sys.stdout
  75. on = self._config.GetString(self._section)
  76. if on is None:
  77. on = self._config.GetString('color.ui')
  78. if on == 'auto':
  79. if pager.active or os.isatty(1):
  80. self._on = True
  81. else:
  82. self._on = False
  83. elif on in ('true', 'always'):
  84. self._on = True
  85. else:
  86. self._on = False
  87. def redirect(self, out):
  88. self._out = out
  89. @property
  90. def is_on(self):
  91. return self._on
  92. def write(self, fmt, *args):
  93. self._out.write(fmt % args)
  94. def flush(self):
  95. self._out.flush()
  96. def nl(self):
  97. self._out.write('\n')
  98. def printer(self, opt=None, fg=None, bg=None, attr=None):
  99. s = self
  100. c = self.colorer(opt, fg, bg, attr)
  101. def f(fmt, *args):
  102. s._out.write(c(fmt, *args))
  103. return f
  104. def colorer(self, opt=None, fg=None, bg=None, attr=None):
  105. if self._on:
  106. c = self._parse(opt, fg, bg, attr)
  107. def f(fmt, *args):
  108. output = fmt % args
  109. return ''.join([c, output, RESET])
  110. return f
  111. else:
  112. def f(fmt, *args):
  113. return fmt % args
  114. return f
  115. def _parse(self, opt, fg, bg, attr):
  116. if not opt:
  117. return _Color(fg, bg, attr)
  118. v = self._config.GetString('%s.%s' % (self._section, opt))
  119. if v is None:
  120. return _Color(fg, bg, attr)
  121. v = v.strip().lower()
  122. if v == "reset":
  123. return RESET
  124. elif v == '':
  125. return _Color(fg, bg, attr)
  126. have_fg = False
  127. for a in v.split(' '):
  128. if is_color(a):
  129. if have_fg:
  130. bg = a
  131. else:
  132. fg = a
  133. elif is_attr(a):
  134. attr = a
  135. return _Color(fg, bg, attr)