color.py 3.5 KB

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