##// 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 """ Translates ANSI escape codes into readable attributes.
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 # Protected class variables.
38 # Protected class variables.
35 _ansi_commands = 'ABCDEFGHJKSTfmnsu'
39 _ansi_commands = 'ABCDEFGHJKSTfmnsu'
36 _ansi_pattern = re.compile('\x01?\x1b\[(.*?)([%s])\x02?' % _ansi_commands)
40 _ansi_pattern = re.compile('\x01?\x1b\[(.*?)([%s])\x02?' % _ansi_commands)
@@ -111,8 +115,10 b' class AnsiCodeProcessor(object):'
111 if code == 0:
115 if code == 0:
112 self.reset_sgr()
116 self.reset_sgr()
113 elif code == 1:
117 elif code == 1:
114 self.intensity = 1
118 if self.bold_text_enabled:
115 self.bold = True
119 self.bold = True
120 else:
121 self.intensity = 1
116 elif code == 2:
122 elif code == 2:
117 self.intensity = 0
123 self.intensity = 0
118 elif code == 3:
124 elif code == 3:
@@ -141,16 +147,20 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
141 """
147 """
142
148
143 # A map from color codes to RGB colors.
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 ('black', 'grey'), # 0: black
151 ('black', 'grey'), # 0: black
146 ('darkred', 'red'), # 1: red
152 ('darkred', 'red'), # 1: red
147 ('darkgreen', 'green'), # 2: green
153 ('darkgreen', 'lime'), # 2: green
148 ('gold', 'yellow'), # 3: yellow
154 ('brown', 'yellow'), # 3: yellow
149 ('darkblue', 'blue'), # 4: blue
155 ('darkblue', 'deepskyblue'), # 4: blue
150 ('darkviolet', 'magenta'), # 5: magenta
156 ('darkviolet', 'magenta'), # 5: magenta
151 ('steelblue', 'cyan'), # 6: cyan
157 ('steelblue', 'cyan'), # 6: cyan
152 ('grey', 'white')) # 7: white
158 ('grey', 'white')) # 7: white
153
159
160 def __init__(self):
161 super(QtAnsiCodeProcessor, self).__init__()
162 self.color_map = self.default_map
163
154 def get_format(self):
164 def get_format(self):
155 """ Returns a QTextCharFormat that encodes the current style attributes.
165 """ Returns a QTextCharFormat that encodes the current style attributes.
156 """
166 """
@@ -158,12 +168,12 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
158
168
159 # Set foreground color
169 # Set foreground color
160 if self.foreground_color is not None:
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 format.setForeground(QtGui.QColor(color))
172 format.setForeground(QtGui.QColor(color))
163
173
164 # Set background color
174 # Set background color
165 if self.background_color is not None:
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 format.setBackground(QtGui.QColor(color))
177 format.setBackground(QtGui.QColor(color))
168
178
169 # Set font weight/style options
179 # Set font weight/style options
@@ -175,3 +185,19 b' class QtAnsiCodeProcessor(AnsiCodeProcessor):'
175 format.setFontUnderline(self.underline)
185 format.setFontUnderline(self.underline)
176
186
177 return format
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 else:
309 else:
310 self._highlighter.set_style(syntax_style)
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 # 'IPythonWidget' protected interface
316 # 'IPythonWidget' protected interface
314 #---------------------------------------------------------------------------
317 #---------------------------------------------------------------------------
@@ -80,10 +80,6 b' def main():'
80 widget.setWindowTitle('Python' if args.pure else 'IPython')
80 widget.setWindowTitle('Python' if args.pure else 'IPython')
81 widget.show()
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 # Start the application main loop.
83 # Start the application main loop.
88 app.exec_()
84 app.exec_()
89
85
General Comments 0
You need to be logged in to leave comments. Login now