blob: b5c68efa6999e5a8caa9c3cf85b838b84378b875 [file] [log] [blame]
José Fonseca28382fc2014-05-27 23:23:47 +01001#########################################################################
José Fonsecab96ab8e2011-09-06 10:22:56 +01002#
José Fonseca01908962012-03-16 09:56:09 +00003# Copyright 2011-2012 Jose Fonseca
José Fonsecab96ab8e2011-09-06 10:22:56 +01004# Copyright 2008-2009 VMware, Inc.
5# All Rights Reserved.
6#
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:
13#
14# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
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
23# THE SOFTWARE.
24#
25##########################################################################/
26
27
José Fonsecab96ab8e2011-09-06 10:22:56 +010028import platform
José Fonseca01908962012-03-16 09:56:09 +000029import subprocess
30import sys
José Fonsecab96ab8e2011-09-06 10:22:56 +010031
32
33class PlainHighlighter:
José Fonseca28382fc2014-05-27 23:23:47 +010034 '''Plain highlighter.'''
José Fonsecab96ab8e2011-09-06 10:22:56 +010035
36 black = None
37 red = None
38 green = None
39 yellow = None
40 blue = None
41 magenta = None
42 cyan = None
43 white = None
44
José Fonseca01908962012-03-16 09:56:09 +000045 def __init__(self, stream = sys.stdout):
José Fonsecab96ab8e2011-09-06 10:22:56 +010046 self.stream = stream
47
48 def write(self, text):
49 self.stream.write(text)
50
51 def flush(self):
52 self.stream.flush()
53
54 def normal(self):
55 pass
56
57 def color(self, color):
58 pass
59
José Fonsecaa20eec22012-03-16 15:40:49 +000060 def bold(self, enable = True):
61 pass
62
63 def strike(self):
José Fonsecab96ab8e2011-09-06 10:22:56 +010064 pass
65
66 def italic(self):
67 pass
68
69
70class AnsiHighlighter(PlainHighlighter):
71 '''Highlighter for plain-text files which outputs ANSI escape codes. See
72 http://en.wikipedia.org/wiki/ANSI_escape_code for more information
73 concerning ANSI escape codes.
74 '''
75
76 _csi = '\33['
77
78 _normal = '0m'
José Fonsecab96ab8e2011-09-06 10:22:56 +010079 _italic = '3m'
80
81 black = 0
82 red = 1
83 green = 2
84 yellow = 3
85 blue = 4
86 magenta = 5
87 cyan = 6
88 white = 7
89
José Fonseca01908962012-03-16 09:56:09 +000090 def __init__(self, stream = sys.stdout):
José Fonsecab96ab8e2011-09-06 10:22:56 +010091 PlainHighlighter.__init__(self, stream)
José Fonsecab96ab8e2011-09-06 10:22:56 +010092
93 def _escape(self, code):
José Fonsecab412d242012-03-15 23:43:52 +000094 self.stream.write(self._csi + code)
José Fonsecab96ab8e2011-09-06 10:22:56 +010095
96 def normal(self):
97 self._escape(self._normal)
98
99 def color(self, color):
100 self._escape(str(30 + color) + 'm')
101
José Fonseca587ca792012-03-16 07:09:04 +0000102 def bold(self, enable = True):
103 if enable:
104 self._escape('1m')
105 else:
Jose Fonseca2edc9992020-02-14 16:30:44 +0000106 self._escape('22m')
José Fonsecab96ab8e2011-09-06 10:22:56 +0100107
José Fonsecaa20eec22012-03-16 15:40:49 +0000108 def strike(self):
109 self._escape('9m')
110
José Fonsecab96ab8e2011-09-06 10:22:56 +0100111 def italic(self):
112 self._escape(self._italic)
113
114
115class WindowsConsoleHighlighter(PlainHighlighter):
116 '''Highlighter for the Windows Console. See
117 http://code.activestate.com/recipes/496901/ for more information.
118 '''
119
120 INVALID_HANDLE_VALUE = -1
121 STD_INPUT_HANDLE = -10
122 STD_OUTPUT_HANDLE = -11
123 STD_ERROR_HANDLE = -12
124
125 FOREGROUND_BLUE = 0x01
126 FOREGROUND_GREEN = 0x02
127 FOREGROUND_RED = 0x04
128 FOREGROUND_INTENSITY = 0x08
129 BACKGROUND_BLUE = 0x10
130 BACKGROUND_GREEN = 0x20
131 BACKGROUND_RED = 0x40
132 BACKGROUND_INTENSITY = 0x80
133
134 COMMON_LVB_LEADING_BYTE = 0x0100
135 COMMON_LVB_TRAILING_BYTE = 0x0200
136 COMMON_LVB_GRID_HORIZONTAL = 0x0400
137 COMMON_LVB_GRID_LVERTICAL = 0x0800
138 COMMON_LVB_GRID_RVERTICAL = 0x1000
139 COMMON_LVB_REVERSE_VIDEO = 0x4000
140 COMMON_LVB_UNDERSCORE = 0x8000
141
142 _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
José Fonsecab96ab8e2011-09-06 10:22:56 +0100143 _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
144
145 black = 0
146 red = FOREGROUND_RED
147 green = FOREGROUND_GREEN
148 blue = FOREGROUND_BLUE
149 white = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
150
José Fonseca01908962012-03-16 09:56:09 +0000151 def __init__(self, stream = sys.stdout):
José Fonsecab96ab8e2011-09-06 10:22:56 +0100152 PlainHighlighter.__init__(self, stream)
153
154 if stream is sys.stdin:
155 nStdHandle = self.STD_INPUT_HANDLE
156 elif stream is sys.stdout:
157 nStdHandle = self.STD_OUTPUT_HANDLE
158 elif stream is sys.stderr:
159 nStdHandle = self.STD_ERROR_HANDLE
160 else:
161 nStdHandle = None
162
163 if nStdHandle is not None:
164 import ctypes
165 self._handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)
166 else:
José Fonseca4d73f852012-02-09 14:04:17 +0000167 self._handle = self.INVALID_HANDLE_VALUE
José Fonsecab96ab8e2011-09-06 10:22:56 +0100168
169 self._attribute = self.white
170
171 def _setAttribute(self, attr):
José Fonseca4d73f852012-02-09 14:04:17 +0000172 if self._handle != self.INVALID_HANDLE_VALUE:
José Fonsecab96ab8e2011-09-06 10:22:56 +0100173 import ctypes
174 ctypes.windll.kernel32.SetConsoleTextAttribute(self._handle, attr)
175 self._attribute = attr
176
177 def normal(self):
178 self._setAttribute(self._normal)
179
180 def color(self, color):
José Fonseca4d73f852012-02-09 14:04:17 +0000181 intensity = self._attribute & self.FOREGROUND_INTENSITY
José Fonsecab96ab8e2011-09-06 10:22:56 +0100182 self._setAttribute(color | intensity)
183
José Fonsecaa20eec22012-03-16 15:40:49 +0000184 def bold(self, enable = True):
José Fonseca01908962012-03-16 09:56:09 +0000185 if enable:
186 attribute = self._attribute | self.FOREGROUND_INTENSITY
187 else:
188 attribute = self._attribute & ~self.FOREGROUND_INTENSITY
189 self._setAttribute(attribute)
José Fonsecab96ab8e2011-09-06 10:22:56 +0100190
191 def italic(self):
192 pass
193
194
José Fonseca01908962012-03-16 09:56:09 +0000195if platform.system() == 'Windows':
196 ColorHighlighter = WindowsConsoleHighlighter
197else:
198 ColorHighlighter = AnsiHighlighter
199
200
201def AutoHighlighter(stream = sys.stdout):
202 if stream.isatty():
203 return ColorHighlighter(stream)
José Fonsecab96ab8e2011-09-06 10:22:56 +0100204 else:
José Fonsecab412d242012-03-15 23:43:52 +0000205 return PlainHighlighter(stream)
José Fonsecab96ab8e2011-09-06 10:22:56 +0100206
207
José Fonseca01908962012-03-16 09:56:09 +0000208class _LessHighlighter(AnsiHighlighter):
209
210 def __init__(self, less):
211 AnsiHighlighter.__init__(self, less.stdin)
212 self.less = less
213
214 def __del__(self):
215 self.less.stdin.close()
216 self.less.wait()
217
218
219def LessHighlighter():
220 if sys.stdout.isatty():
221 try:
222 less = subprocess.Popen(
223 args = ['less', '-FRXn'],
Jose Fonseca247e1fa2019-04-28 14:14:44 +0100224 stdin = subprocess.PIPE,
225 universal_newlines = True
José Fonseca01908962012-03-16 09:56:09 +0000226 )
227 except OSError:
228 return ColorHighlighter()
229 else:
230 return _LessHighlighter(less)
231 return PlainHighlighter(sys.stdout)
232