color.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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"
  35. def is_color(s):
  36. return s in COLORS
  37. def is_attr(s):
  38. return s in ATTRS
  39. def _Color(fg = None, bg = None, attr = None):
  40. fg = COLORS[fg]
  41. bg = COLORS[bg]
  42. attr = ATTRS[attr]
  43. if attr >= 0 or fg >= 0 or bg >= 0:
  44. need_sep = False
  45. code = "\033["
  46. if attr >= 0:
  47. code += chr(ord('0') + attr)
  48. need_sep = True
  49. if fg >= 0:
  50. if need_sep:
  51. code += ';'
  52. need_sep = True
  53. if fg < 8:
  54. code += '3%c' % (ord('0') + fg)
  55. else:
  56. code += '38;5;%d' % fg
  57. if bg >= 0:
  58. if need_sep:
  59. code += ';'
  60. need_sep = True
  61. if bg < 8:
  62. code += '4%c' % (ord('0') + bg)
  63. else:
  64. code += '48;5;%d' % bg
  65. code += 'm'
  66. else:
  67. code = ''
  68. return code
  69. class Coloring(object):
  70. def __init__(self, config, section_type):
  71. self._section = 'color.%s' % section_type
  72. self._config = config
  73. self._out = sys.stdout
  74. on = self._config.GetString(self._section)
  75. if on is None:
  76. on = self._config.GetString('color.ui')
  77. if on == 'auto':
  78. if pager.active or os.isatty(1):
  79. self._on = True
  80. else:
  81. self._on = False
  82. elif on in ('true', 'always'):
  83. self._on = True
  84. else:
  85. self._on = False
  86. def redirect(self, out):
  87. self._out = out
  88. @property
  89. def is_on(self):
  90. return self._on
  91. def write(self, fmt, *args):
  92. self._out.write(fmt % args)
  93. def flush(self):
  94. self._out.flush()
  95. def nl(self):
  96. self._out.write('\n')
  97. def printer(self, opt=None, fg=None, bg=None, attr=None):
  98. s = self
  99. c = self.colorer(opt, fg, bg, attr)
  100. def f(fmt, *args):
  101. s._out.write(c(fmt, *args))
  102. return f
  103. def colorer(self, opt=None, fg=None, bg=None, attr=None):
  104. if self._on:
  105. c = self._parse(opt, fg, bg, attr)
  106. def f(fmt, *args):
  107. output = fmt % args
  108. return ''.join([c, output, RESET])
  109. return f
  110. else:
  111. def f(fmt, *args):
  112. return fmt % args
  113. return f
  114. def _parse(self, opt, fg, bg, attr):
  115. if not opt:
  116. return _Color(fg, bg, attr)
  117. v = self._config.GetString('%s.%s' % (self._section, opt))
  118. if v is None:
  119. return _Color(fg, bg, attr)
  120. v = v.strip().lower()
  121. if v == "reset":
  122. return RESET
  123. elif v == '':
  124. return _Color(fg, bg, attr)
  125. have_fg = False
  126. for a in v.split(' '):
  127. if is_color(a):
  128. if have_fg:
  129. bg = a
  130. else:
  131. fg = a
  132. elif is_attr(a):
  133. attr = a
  134. return _Color(fg, bg, attr)