1 ##########################################################################
3 # Copyright 2008-2009 VMware, Inc.
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 ##########################################################################/
33 def __init__(self, stream):
37 self.stream.write(text)
42 def function(self, name):
45 def variable(self, name):
48 def literal(self, value):
51 def address(self, addr):
55 class AnsiFormatter(Formatter):
56 '''Formatter for plain-text files which outputs ANSI escape codes. See
57 http://en.wikipedia.org/wiki/ANSI_escape_code for more information
58 concerning ANSI escape codes.
70 def _escape(self, code):
71 self.text(self._csi + code)
73 def function(self, name):
74 self._escape(self._bold)
75 Formatter.function(self, name)
76 self._escape(self._normal)
78 def variable(self, name):
79 self._escape(self._italic)
80 Formatter.variable(self, name)
81 self._escape(self._normal)
83 def literal(self, value):
84 self._escape(self._blue)
85 Formatter.literal(self, value)
86 self._escape(self._normal)
88 def address(self, value):
89 self._escape(self._green)
90 Formatter.address(self, value)
91 self._escape(self._normal)
94 class WindowsConsoleFormatter(Formatter):
95 '''Formatter for the Windows Console. See
96 http://code.activestate.com/recipes/496901/ for more information.
99 STD_INPUT_HANDLE = -10
100 STD_OUTPUT_HANDLE = -11
101 STD_ERROR_HANDLE = -12
103 FOREGROUND_BLUE = 0x01
104 FOREGROUND_GREEN = 0x02
105 FOREGROUND_RED = 0x04
106 FOREGROUND_INTENSITY = 0x08
107 BACKGROUND_BLUE = 0x10
108 BACKGROUND_GREEN = 0x20
109 BACKGROUND_RED = 0x40
110 BACKGROUND_INTENSITY = 0x80
112 _normal = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
113 _bold = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
114 _italic = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
115 _red = FOREGROUND_RED | FOREGROUND_INTENSITY
116 _green = FOREGROUND_GREEN | FOREGROUND_INTENSITY
117 _blue = FOREGROUND_BLUE | FOREGROUND_INTENSITY
119 def __init__(self, stream):
120 Formatter.__init__(self, stream)
122 if stream is sys.stdin:
123 nStdHandle = self.STD_INPUT_HANDLE
124 elif stream is sys.stdout:
125 nStdHandle = self.STD_OUTPUT_HANDLE
126 elif stream is sys.stderr:
127 nStdHandle = self.STD_ERROR_HANDLE
133 self.handle = ctypes.windll.kernel32.GetStdHandle(nStdHandle)
137 def _attribute(self, attr):
140 ctypes.windll.kernel32.SetConsoleTextAttribute(self.handle, attr)
142 def function(self, name):
143 self._attribute(self._bold)
144 Formatter.function(self, name)
145 self._attribute(self._normal)
147 def variable(self, name):
148 self._attribute(self._italic)
149 Formatter.variable(self, name)
150 self._attribute(self._normal)
152 def literal(self, value):
153 self._attribute(self._blue)
154 Formatter.literal(self, value)
155 self._attribute(self._normal)
157 def address(self, value):
158 self._attribute(self._green)
159 Formatter.address(self, value)
160 self._attribute(self._normal)
163 def DefaultFormatter(stream):
164 if sys.platform in ('linux2', 'cygwin'):
165 return AnsiFormatter(stream)
166 elif sys.platform in ('win32',):
167 return WindowsConsoleFormatter(stream)
169 return Formatter(stream)