##// END OF EJS Templates
The IPythonWidget now tries to be smart about choosing colors to use for ANSI color codes.
epatters -
Show More
@@ -31,6 +31,10 b' class AnsiCodeProcessor(object):'
31 31 """ Translates ANSI escape codes into readable attributes.
32 32 """
33 33
34 # Whether to increase intensity or set boldness for SGR code 1.
35 # (Different terminals handle this in different ways.)
36 bold_text_enabled = False
37
34 38 # Protected class variables.
35 39 _ansi_commands = 'ABCDEFGHJKSTfmnsu'
36 40 _ansi_pattern = re.compile('\x01?\x1b\[(.*?)([%s])\x02?' % _ansi_commands)
@@ -111,8 +115,10 b' class AnsiCodeProcessor(object):'
111 115 if code == 0:
112 116 self.reset_sgr()
113 117 elif code == 1:
114 self.intensity = 1
115 self.bold = True
118 if self.bold_text_enabled:
119 self.bold = True
120 else:
121 self.intensity = 1
116 122 elif code == 2:
117 123 self.intensity = 0
118 124 elif code == 3:
@@ -141,15 +147,19 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
141 147 """
142 148
143 149 # A map from color codes to RGB colors.
144 ansi_colors = (# Normal, Bright/Light ANSI color code
150 default_map = (# Normal, Bright/Light ANSI color code
145 151 ('black', 'grey'), # 0: black
146 152 ('darkred', 'red'), # 1: red
147 ('darkgreen', 'green'), # 2: green
148 ('gold', 'yellow'), # 3: yellow
149 ('darkblue', 'blue'), # 4: blue
153 ('darkgreen', 'lime'), # 2: green
154 ('brown', 'yellow'), # 3: yellow
155 ('darkblue', 'deepskyblue'), # 4: blue
150 156 ('darkviolet', 'magenta'), # 5: magenta
151 157 ('steelblue', 'cyan'), # 6: cyan
152 158 ('grey', 'white')) # 7: white
159
160 def __init__(self):
161 super(QtAnsiCodeProcessor, self).__init__()
162 self.color_map = self.default_map
153 163
154 164 def get_format(self):
155 165 """ Returns a QTextCharFormat that encodes the current style attributes.
@@ -158,12 +168,12 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
158 168
159 169 # Set foreground color
160 170 if self.foreground_color is not None:
161 color = self.ansi_colors[self.foreground_color][self.intensity]
171 color = self.color_map[self.foreground_color][self.intensity]
162 172 format.setForeground(QtGui.QColor(color))
163 173
164 174 # Set background color
165 175 if self.background_color is not None:
166 color = self.ansi_colors[self.background_color][self.intensity]
176 color = self.color_map[self.background_color][self.intensity]
167 177 format.setBackground(QtGui.QColor(color))
168 178
169 179 # Set font weight/style options
@@ -175,3 +185,19 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
175 185 format.setFontUnderline(self.underline)
176 186
177 187 return format
188
189 def set_background_color(self, color):
190 """ Given a background color (a QColor), attempt to set a color map
191 that will be aesthetically pleasing.
192 """
193 if color.value() < 127:
194 # Colors appropriate for a terminal with a dark background.
195 self.color_map = self.default_map
196
197 else:
198 # Colors appropriate for a terminal with a light background. For
199 # now, only use non-bright colors...
200 self.color_map = [ (pair[0], pair[0]) for pair in self.default_map ]
201
202 # ...and replace white with black.
203 self.color_map[7] = ('black', 'black')
@@ -309,6 +309,9 b' class IPythonWidget(FrontendWidget):'
309 309 else:
310 310 self._highlighter.set_style(syntax_style)
311 311
312 bg_color = self._control.palette().background().color()
313 self._ansi_processor.set_background_color(bg_color)
314
312 315 #---------------------------------------------------------------------------
313 316 # 'IPythonWidget' protected interface
314 317 #---------------------------------------------------------------------------
@@ -80,10 +80,6 b' def main():'
80 80 widget.setWindowTitle('Python' if args.pure else 'IPython')
81 81 widget.show()
82 82
83 # FIXME: This is a hack: set colors to lightbg by default in qt terminal
84 # unconditionally, regardless of user settings in config files.
85 widget.execute("%colors lightbg", hidden=True)
86
87 83 # Start the application main loop.
88 84 app.exec_()
89 85
General Comments 0
You need to be logged in to leave comments. Login now