1 ##########################################################################
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2009 VMware, Inc.
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ##########################################################################/
32 class PlainHighlighter:
44 def __init__(self, stream):
47 def write(self, text):
48 self.stream.write(text)
56 def color(self, color):
66 class AnsiHighlighter(PlainHighlighter):
67 '''Highlighter for plain-text files which outputs ANSI escape codes. See
68 http://en.wikipedia.org/wiki/ANSI_escape_code for more information
69 concerning ANSI escape codes.
87 def __init__(self, stream):
88 PlainHighlighter.__init__(self, stream)
90 def _escape(self, code):
91 self.stream.write(self._csi + code)
94 self._escape(self._normal)
96 def color(self, color):
97 self._escape(str(30 + color) + 'm')
100 self._escape(self._bold)
103 self._escape(self._italic)
106 class WindowsConsoleHighlighter(PlainHighlighter):
107 '''Highlighter for the Windows Console. See
108 http://code.activestate.com/recipes/496901/ for more information.
111 INVALID_HANDLE_VALUE = -1
112 STD_INPUT_HANDLE = -10
113 STD_OUTPUT_HANDLE = -11
114 STD_ERROR_HANDLE = -12
116 FOREGROUND_BLUE = 0x01
117 FOREGROUND_GREEN = 0x02
118 FOREGROUND_RED = 0x04
119 FOREGROUND_INTENSITY = 0x08
120 BACKGROUND_BLUE = 0x10
121 BACKGROUND_GREEN = 0x20
122 BACKGROUND_RED = 0x40
123 BACKGROUND_INTENSITY = 0x80
125 COMMON_LVB_LEADING_BYTE = 0x0100
126 COMMON_LVB_TRAILING_BYTE = 0x0200
127 COMMON_LVB_GRID_HORIZONTAL = 0x0400
128 COMMON_LVB_GRID_LVERTICAL = 0x0800
129 COMMON_LVB_GRID_RVERTICAL = 0x1000
130 COMMON_LVB_REVERSE_VIDEO = 0x4000
131 COMMON_LVB_UNDERSCORE = 0x8000
133 _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
134 _bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
135 _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
139 green = FOREGROUND_GREEN
140 blue = FOREGROUND_BLUE
141 white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
143 def __init__(self, stream):
144 PlainHighlighter.__init__(self, stream)
146 if stream is sys.stdin:
147 nStdHandle = self.STD_INPUT_HANDLE
148 elif stream is sys.stdout:
149 nStdHandle = self.STD_OUTPUT_HANDLE
150 elif stream is sys.stderr:
151 nStdHandle = self.STD_ERROR_HANDLE
155 if nStdHandle is not None:
157 self._handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)
159 self._handle = self.INVALID_HANDLE_VALUE
161 self._attribute = self.white
163 def _setAttribute(self, attr):
164 if self._handle != self.INVALID_HANDLE_VALUE:
166 ctypes.windll.kernel32.SetConsoleTextAttribute(self._handle, attr)
167 self._attribute = attr
170 self._setAttribute(self._normal)
172 def color(self, color):
173 intensity = self._attribute & self.FOREGROUND_INTENSITY
174 self._setAttribute(color | intensity)
177 self._setAttribute(self._attribute | self.FOREGROUND_INTENSITY)
183 def Highlighter(stream = sys.stdout, force = False):
184 if force or stream.isatty():
185 if platform.system() == 'Windows':
186 return WindowsConsoleHighlighter(stream)
188 return AnsiHighlighter(stream)
190 return PlainHighlighter(stream)