Skip to content

Commit

Permalink
Add support for ANSI 256 colors
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Jun 4, 2024
1 parent caaff4c commit b0eb600
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
31 changes: 26 additions & 5 deletions qtconsole/ansi_code_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ def _replace_special(self, match):
self.actions.append(ScrollAction('scroll', 'down', 'page', 1))
return ''

def _parse_ansi_color(self, color, intensity):
"""
Map an ANSI color code to color name or a RGB tuple.
Based on: https://gist.github.com/MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b
"""
parsed_color = None
if color < 8:
# Adjust for intensity, if possible.
if intensity > 0:
color += 8
parsed_color = self.color_map.get(color, None)
elif (color > 231):
s = int((color - 232) * 10 + 8)
parsed_color = (s, s, s)
else:
n = color - 16
b = n % 6
g = (n - b) / 6 % 6
r = (n - b - g * 6) / 36 % 6
r = int(r * 40 + 55) if r else 0
g = int(g * 40 + 55) if g else 0
b = int(b * 40 + 55) if b else 0
parsed_color = (r, g, b)
return parsed_color


class QtAnsiCodeProcessor(AnsiCodeProcessor):
""" Translates ANSI escape codes into QTextCharFormats.
Expand Down Expand Up @@ -323,12 +348,8 @@ def get_color(self, color, intensity=0):
""" Returns a QColor for a given color code or rgb list, or None if one
cannot be constructed.
"""

if isinstance(color, int):
# Adjust for intensity, if possible.
if color < 8 and intensity > 0:
color += 8
constructor = self.color_map.get(color, None)
constructor = self._parse_ansi_color(color, intensity)
elif isinstance(color, (tuple, list)):
constructor = color
else:
Expand Down
1 change: 0 additions & 1 deletion qtconsole/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ def set_syntax_style(self, syntax_style):
self.active_frontend._syntax_style_changed()
self.active_frontend._style_sheet_changed()
self.active_frontend.reset(clear=True)
self.active_frontend._execute("%colors linux", True)
selectColor = styles.get_colors(syntax_style)
self.active_frontend._execute("from IPython.core.ultratb import VerboseTB\n"+
"VerboseTB._tb_highlight = 'bg:%(select)s'\n"%selectColor+
Expand Down

0 comments on commit b0eb600

Please sign in to comment.