2 ##########################################################################
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 ##########################################################################/
34 def __init__(self, stream):
38 self.stream.write(text)
43 def function(self, name):
46 def variable(self, name):
49 def literal(self, value):
52 def address(self, addr):
56 class AnsiFormatter(Formatter):
57 '''Formatter for plain-text files which outputs ANSI escape codes. See
58 http://en.wikipedia.org/wiki/ANSI_escape_code for more information
59 concerning ANSI escape codes.
71 def _escape(self, code):
72 self.text(self._csi + code)
74 def function(self, name):
75 self._escape(self._bold)
76 Formatter.function(self, name)
77 self._escape(self._normal)
79 def variable(self, name):
80 self._escape(self._italic)
81 Formatter.variable(self, name)
82 self._escape(self._normal)
84 def literal(self, value):
85 self._escape(self._blue)
86 Formatter.literal(self, value)
87 self._escape(self._normal)
89 def address(self, value):
90 self._escape(self._green)
91 Formatter.address(self, value)
92 self._escape(self._normal)
95 class WindowsConsoleFormatter(Formatter):
96 '''Formatter for the Windows Console. See
97 http://code.activestate.com/recipes/496901/ for more information.
100 STD_INPUT_HANDLE = -10
101 STD_OUTPUT_HANDLE = -11
102 STD_ERROR_HANDLE = -12
104 FOREGROUND_BLUE = 0x01
105 FOREGROUND_GREEN = 0x02
106 FOREGROUND_RED = 0x04
107 FOREGROUND_INTENSITY = 0x08
108 BACKGROUND_BLUE = 0x10
109 BACKGROUND_GREEN = 0x20
110 BACKGROUND_RED = 0x40
111 BACKGROUND_INTENSITY = 0x80
113 _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
114 _bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
115 _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
116 _red = FOREGROUND_RED | FOREGROUND_INTENSITY
117 _green = FOREGROUND_GREEN | FOREGROUND_INTENSITY
118 _blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY
120 def __init__(self, stream):
121 Formatter.__init__(self, stream)
123 if stream is sys.stdin:
124 nStdHandle = self.STD_INPUT_HANDLE
125 elif stream is sys.stdout:
126 nStdHandle = self.STD_OUTPUT_HANDLE
127 elif stream is sys.stderr:
128 nStdHandle = self.STD_ERROR_HANDLE
134 self.handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)
138 def _attribute(self, attr):
141 ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, attr)
143 def function(self, name):
144 self._attribute(self._bold)
145 Formatter.function(self, name)
146 self._attribute(self._normal)
148 def variable(self, name):
149 self._attribute(self._italic)
150 Formatter.variable(self, name)
151 self._attribute(self._normal)
153 def literal(self, value):
154 self._attribute(self._blue)
155 Formatter.literal(self, value)
156 self._attribute(self._normal)
158 def address(self, value):
159 self._attribute(self._green)
160 Formatter.address(self, value)
161 self._attribute(self._normal)
164 def DefaultFormatter(stream):
165 if sys.platform in ('linux2', 'cygwin'):
166 return AnsiFormatter(stream)
167 elif sys.platform in ('win32',):
168 return WindowsConsoleFormatter(stream)
170 return Formatter(stream)