From 4b018fdfcdc70d472ad3480b9fff1ec0c59c4d33 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 24 Jul 2025 13:31:23 -0400 Subject: [PATCH 1/3] Don't strip newlines when ANSI text is sent to print() AnsiDecoder.decode() takes a single str and returns a list of Text split by newline. If this is used directly, newlines in the source will not be restored when sent to print(). Using Lines instead of list lets us indicate to Rich that these Text objects should be interspersed with newlines. Ref https://github.com/Textualize/rich/blob/master/rich/ansi.py#L126 --- src/enrich/console.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/enrich/console.py b/src/enrich/console.py index a56dd00..1778f49 100644 --- a/src/enrich/console.py +++ b/src/enrich/console.py @@ -6,6 +6,7 @@ import rich.console as rich_console from rich.ansi import AnsiDecoder +from rich.containers import Lines from rich.file_proxy import FileProxy @@ -48,8 +49,8 @@ def print(self, *args, **kwargs) -> None: # type: ignore if args and isinstance(args[0], str) and "\033" in args[0]: text = format(*args) + "\n" decoder = AnsiDecoder() - args = list(decoder.decode(text)) # type: ignore - super().print(*args, **kwargs) + args = Lines(decoder.decode(text)) # type: ignore[assignment] + super().print(args, **kwargs) # Based on Ansible implementation From 0ea52ec0d2c1a6080b2754f8c8568fc0fe877b1f Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 24 Jul 2025 13:45:21 -0400 Subject: [PATCH 2/3] Fix calling rich's print decoded vs not --- src/enrich/console.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/enrich/console.py b/src/enrich/console.py index 1778f49..8e2f56c 100644 --- a/src/enrich/console.py +++ b/src/enrich/console.py @@ -50,7 +50,9 @@ def print(self, *args, **kwargs) -> None: # type: ignore text = format(*args) + "\n" decoder = AnsiDecoder() args = Lines(decoder.decode(text)) # type: ignore[assignment] - super().print(args, **kwargs) + super().print(args, **kwargs) + else: + super().print(*args, **kwargs) # Based on Ansible implementation From 4d4fdc64bc78511b7ff3fbb06027a04daa2cc429 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 24 Jul 2025 13:51:49 -0400 Subject: [PATCH 3/3] Update console test to show newline issue --- test/test_console.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_console.py b/test/test_console.py index 43d8ad3..d619e5b 100644 --- a/test/test_console.py +++ b/test/test_console.py @@ -56,10 +56,10 @@ def test_console_soft_wrap() -> None: def test_console_print_ansi() -> None: """Validates that Console.print() with ANSI does not make break them.""" console = Console(force_terminal=True, record=True, soft_wrap=True, redirect=True) - text = "\033[92mfuture is green!\033[0m" + text = "\033[92mfuture is green!\033[0m\nwowsers!" console.print(text) text_result = console.export_text(clear=False) - assert "future is green!" in text_result + assert "future is green!\nwowsers!" in text_result html_result = console.export_html() assert "#00ff00" in html_result