From 316be7bf67715410ad64ea497688bf93b5e92b88 Mon Sep 17 00:00:00 2001 From: James Estevez Date: Sat, 6 Nov 2021 11:49:33 -0700 Subject: [PATCH 1/3] Values exceeding the console's width break print_json --- tests/test_rich_print.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_rich_print.py b/tests/test_rich_print.py index f6ea5110..a92b8000 100644 --- a/tests/test_rich_print.py +++ b/tests/test_rich_print.py @@ -1,4 +1,5 @@ import io +import json import rich from rich.console import Console @@ -38,6 +39,27 @@ def test_rich_print_json(): assert result == expected +def test_rich_print_json_round_trip(): + data = ["x" * 100, 2e128] + console = rich.get_console() + with console.capture() as capture: + rich.print_json(data=data, indent=4) + result = capture.get() + print(repr(result)) + result_data = json.loads(result) + assert result_data == data + + +def test_rich_print_json_no_truncation(): + console = rich.get_console() + with console.capture() as capture: + rich.print_json(f'["{"x" * 100}", {int(2e128)}]', indent=4) + result = capture.get() + print(repr(result)) + assert ("x" * 100) in result + assert str(int(2e128)) in result + + def test_rich_print_X(): console = rich.get_console() output = io.StringIO() From 3ee35156de58624fe9976eead3ab52d82f7205b2 Mon Sep 17 00:00:00 2001 From: James Estevez Date: Sat, 6 Nov 2021 11:51:24 -0700 Subject: [PATCH 2/3] Add soft_wrap=True to print_json This commit adds soft_wrap=True to the invocation of print in print_json. Without it, values wider than the console's width are truncated, and the output will not be valid JSON. --- rich/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/console.py b/rich/console.py index 3b08e394..4764e0bd 100644 --- a/rich/console.py +++ b/rich/console.py @@ -1679,7 +1679,7 @@ class Console: default=default, sort_keys=sort_keys, ) - self.print(json_renderable) + self.print(json_renderable, soft_wrap=True) def update_screen( self, From 07c6733027ef45dd1e2fef409522bfe7924bb7de Mon Sep 17 00:00:00 2001 From: James Estevez Date: Sat, 6 Nov 2021 12:02:16 -0700 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + CONTRIBUTORS.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a0dad5b..4d5c729d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed issue with TERM env vars that have more than one hyphen https://github.com/willmcgugan/rich/issues/1640 - Fixed missing new line after progress bar when terminal is not interactive https://github.com/willmcgugan/rich/issues/1606 - Fixed exception in IPython when disabling pprint with %pprint https://github.com/willmcgugan/rich/issues/1646 +- Fixed issue where values longer than the console width produced invalid JSON https://github.com/willmcgugan/rich/issues/1653 ## [10.12.0] - 2021-10-06 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ad5147af..ba0c7d67 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,6 +6,7 @@ The following people have contributed to the development of Rich: - [Gregory Beauregard](https://github.com/GBeauregard/pyffstream) - [Pete Davison](https://github.com/pd93) +- [James Estevez](https://github.com/jstvz) - [Oleksis Fraga](https://github.com/oleksis) - [Finn Hughes](https://github.com/finnhughes) - [Josh Karpel](https://github.com/JoshKarpel)