From caaff4c610e9ae0589429738fb3ed6cbf42ac1c3 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Mon, 27 May 2024 22:58:48 -0500 Subject: [PATCH 01/13] Use of "hack" verboseTB --- qtconsole/mainwindow.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index b5b0476a..ac086001 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -801,6 +801,7 @@ def set_syntax_style(self, syntax_style): colors='nocolor' elif styles.dark_style(syntax_style): colors='linux' + else: colors='lightbg' self.active_frontend.syntax_style = syntax_style @@ -810,6 +811,11 @@ def set_syntax_style(self, syntax_style): 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+ + f"VerboseTB._tb_highlight_style = '{syntax_style}'", True) + def close_active_frontend(self): self.close_tab(self.active_frontend) From b0eb6003c27c4cead2c469bd0c76fb8cb52b8b68 Mon Sep 17 00:00:00 2001 From: dalthviz <16781833+dalthviz@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:29:51 -0500 Subject: [PATCH 02/13] Add support for ANSI 256 colors --- qtconsole/ansi_code_processor.py | 31 ++++++++++++++++++++++++++----- qtconsole/mainwindow.py | 1 - 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/qtconsole/ansi_code_processor.py b/qtconsole/ansi_code_processor.py index 08f222f6..dfc542cb 100644 --- a/qtconsole/ansi_code_processor.py +++ b/qtconsole/ansi_code_processor.py @@ -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. @@ -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: diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index ac086001..bd3fc276 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -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+ From b2737879f2667a1b78abc023b647bed4f549bf05 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Tue, 11 Jun 2024 00:19:57 -0500 Subject: [PATCH 03/13] Format string hack --- qtconsole/mainwindow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index bd3fc276..bee10b0c 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -812,7 +812,7 @@ def set_syntax_style(self, syntax_style): self.active_frontend.reset(clear=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+ + f"VerboseTB._tb_highlight = 'bg:%{selectColor}'\n"+ f"VerboseTB._tb_highlight_style = '{syntax_style}'", True) From f5d14e3ed88482aeb6a9598194357c0a9a662209 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Tue, 11 Jun 2024 01:42:27 -0500 Subject: [PATCH 04/13] Fix test --- qtconsole/tests/test_jupyter_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/tests/test_jupyter_widget.py b/qtconsole/tests/test_jupyter_widget.py index 43ab4a18..3849814b 100644 --- a/qtconsole/tests/test_jupyter_widget.py +++ b/qtconsole/tests/test_jupyter_widget.py @@ -34,7 +34,7 @@ def test_stylesheet_changed(self): w = JupyterWidget(kind='rich') # By default, the background is light. White text is rendered as black - self.assertEqual(w._ansi_processor.get_color(15).name(), '#000000') + self.assertEqual(w._ansi_processor.get_color(15).name(), '#ffffff') # Change to a dark colorscheme. White text is rendered as white w.syntax_style = 'monokai' From ddedcdfc0da5571f2f99d037e0b54f499bdd8325 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Thu, 13 Jun 2024 00:13:27 -0500 Subject: [PATCH 05/13] Fix ANSI to RGB transformation function --- qtconsole/ansi_code_processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/ansi_code_processor.py b/qtconsole/ansi_code_processor.py index dfc542cb..69254c1d 100644 --- a/qtconsole/ansi_code_processor.py +++ b/qtconsole/ansi_code_processor.py @@ -298,7 +298,7 @@ def _parse_ansi_color(self, color, intensity): Based on: https://gist.github.com/MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b """ parsed_color = None - if color < 8: + if color < 16: # Adjust for intensity, if possible. if intensity > 0: color += 8 From 608d0e56bb35c3cabb39395653a46a7c56ccd088 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Thu, 13 Jun 2024 00:13:51 -0500 Subject: [PATCH 06/13] Fix use of "hack" --- qtconsole/mainwindow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index bee10b0c..970dad11 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -810,10 +810,10 @@ 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) - selectColor = styles.get_colors(syntax_style) - self.active_frontend._execute("from IPython.core.ultratb import VerboseTB\n"+ - f"VerboseTB._tb_highlight = 'bg:%{selectColor}'\n"+ - f"VerboseTB._tb_highlight_style = '{syntax_style}'", True) + self.active_frontend._execute( + f"from IPython.core.ultratb import VerboseTB;" + "VerboseTB._tb_highlight_style = '{syntax_style}'", + True) def close_active_frontend(self): From 65308b53a7e564623c0f3ed5eabd02ca3e123028 Mon Sep 17 00:00:00 2001 From: jsbautista <42411448+jsbautista@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:26:02 -0500 Subject: [PATCH 07/13] Update qtconsole/mainwindow.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Althviz Moré <16781833+dalthviz@users.noreply.github.com> --- qtconsole/mainwindow.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index 970dad11..2a53b131 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -811,8 +811,13 @@ def set_syntax_style(self, syntax_style): self.active_frontend._style_sheet_changed() self.active_frontend.reset(clear=True) self.active_frontend._execute( - f"from IPython.core.ultratb import VerboseTB;" - "VerboseTB._tb_highlight_style = '{syntax_style}'", +f""" +from IPython.core.ultratb import VerboseTB +try: + VerboseTB._tb_highlight_style = '{syntax_style}' +except AttributeError: + get_ipython().run_line_magic('colors', '{colors}') +""", True) From f59396101bf7b308c57aa40755ad25f2f11b193d Mon Sep 17 00:00:00 2001 From: jsbautista Date: Sun, 16 Jun 2024 23:26:47 -0500 Subject: [PATCH 08/13] Fix test --- qtconsole/tests/test_jupyter_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/tests/test_jupyter_widget.py b/qtconsole/tests/test_jupyter_widget.py index 3849814b..43ab4a18 100644 --- a/qtconsole/tests/test_jupyter_widget.py +++ b/qtconsole/tests/test_jupyter_widget.py @@ -34,7 +34,7 @@ def test_stylesheet_changed(self): w = JupyterWidget(kind='rich') # By default, the background is light. White text is rendered as black - self.assertEqual(w._ansi_processor.get_color(15).name(), '#ffffff') + self.assertEqual(w._ansi_processor.get_color(15).name(), '#000000') # Change to a dark colorscheme. White text is rendered as white w.syntax_style = 'monokai' From 7a1a34c6ef82d57eaf1da17f6738150c421397f5 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Fri, 21 Jun 2024 17:23:19 -0500 Subject: [PATCH 09/13] Fix test --- qtconsole/tests/test_jupyter_widget.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qtconsole/tests/test_jupyter_widget.py b/qtconsole/tests/test_jupyter_widget.py index 43ab4a18..b32ff1d5 100644 --- a/qtconsole/tests/test_jupyter_widget.py +++ b/qtconsole/tests/test_jupyter_widget.py @@ -39,6 +39,9 @@ def test_stylesheet_changed(self): # Change to a dark colorscheme. White text is rendered as white w.syntax_style = 'monokai' self.assertEqual(w._ansi_processor.get_color(15).name(), '#ffffff') + + # Color code 40 + self.assertEqual(w._ansi_processor.get_color(40).name(), '#37D737') @pytest.mark.skipif(not sys.platform.startswith('linux'), reason="Works only on Linux") From 2e752c68abe44ce65325f81660c2cdfb95f3ddca Mon Sep 17 00:00:00 2001 From: jsbautista <42411448+jsbautista@users.noreply.github.com> Date: Tue, 25 Jun 2024 13:16:18 -0500 Subject: [PATCH 10/13] Update qtconsole/mainwindow.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Althviz Moré <16781833+dalthviz@users.noreply.github.com> --- qtconsole/mainwindow.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qtconsole/mainwindow.py b/qtconsole/mainwindow.py index 2a53b131..4d5c86b6 100644 --- a/qtconsole/mainwindow.py +++ b/qtconsole/mainwindow.py @@ -813,9 +813,11 @@ def set_syntax_style(self, syntax_style): self.active_frontend._execute( f""" from IPython.core.ultratb import VerboseTB -try: +if getattr(VerboseTB, 'tb_highlight_style', None) is not None: + VerboseTB.tb_highlight_style = '{syntax_style}' +elif getattr(VerboseTB, '_tb_highlight_style', None) is not None: VerboseTB._tb_highlight_style = '{syntax_style}' -except AttributeError: +else: get_ipython().run_line_magic('colors', '{colors}') """, True) From ff4388018748173cae8a3e44bfc0c7624d8e4fca Mon Sep 17 00:00:00 2001 From: jsbautista Date: Tue, 25 Jun 2024 13:18:50 -0500 Subject: [PATCH 11/13] Fix test --- qtconsole/tests/test_jupyter_widget.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/qtconsole/tests/test_jupyter_widget.py b/qtconsole/tests/test_jupyter_widget.py index b32ff1d5..e5cc9f61 100644 --- a/qtconsole/tests/test_jupyter_widget.py +++ b/qtconsole/tests/test_jupyter_widget.py @@ -36,12 +36,15 @@ def test_stylesheet_changed(self): # By default, the background is light. White text is rendered as black self.assertEqual(w._ansi_processor.get_color(15).name(), '#000000') + # Color code 40 + self.assertEqual(w._ansi_processor.get_color(40).name(), '#37D737') + # Change to a dark colorscheme. White text is rendered as white w.syntax_style = 'monokai' self.assertEqual(w._ansi_processor.get_color(15).name(), '#ffffff') - # Color code 40 - self.assertEqual(w._ansi_processor.get_color(40).name(), '#37D737') + # Color code 40 with monokai + self.assertEqual(w._ansi_processor.get_color(40).name(), '#00d700') @pytest.mark.skipif(not sys.platform.startswith('linux'), reason="Works only on Linux") From dd5c5b3559322e40972b87425e3afe232fa515b0 Mon Sep 17 00:00:00 2001 From: jsbautista Date: Tue, 25 Jun 2024 14:10:21 -0500 Subject: [PATCH 12/13] Fix test --- qtconsole/tests/test_jupyter_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/tests/test_jupyter_widget.py b/qtconsole/tests/test_jupyter_widget.py index e5cc9f61..d1741ddd 100644 --- a/qtconsole/tests/test_jupyter_widget.py +++ b/qtconsole/tests/test_jupyter_widget.py @@ -37,7 +37,7 @@ def test_stylesheet_changed(self): self.assertEqual(w._ansi_processor.get_color(15).name(), '#000000') # Color code 40 - self.assertEqual(w._ansi_processor.get_color(40).name(), '#37D737') + self.assertEqual(w._ansi_processor.get_color(40).name(), '#00d700') # Change to a dark colorscheme. White text is rendered as white w.syntax_style = 'monokai' From cf7ad25f515e0ff8b151c369b094bf99296f1aee Mon Sep 17 00:00:00 2001 From: jsbautista <42411448+jsbautista@users.noreply.github.com> Date: Sun, 7 Jul 2024 09:44:44 -0500 Subject: [PATCH 13/13] Update qtconsole/ansi_code_processor.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Althviz Moré <16781833+dalthviz@users.noreply.github.com> --- qtconsole/ansi_code_processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qtconsole/ansi_code_processor.py b/qtconsole/ansi_code_processor.py index 69254c1d..1caec54c 100644 --- a/qtconsole/ansi_code_processor.py +++ b/qtconsole/ansi_code_processor.py @@ -300,7 +300,7 @@ def _parse_ansi_color(self, color, intensity): parsed_color = None if color < 16: # Adjust for intensity, if possible. - if intensity > 0: + if intensity > 0 and color < 8: color += 8 parsed_color = self.color_map.get(color, None) elif (color > 231):