From ff88751ba450534fe9fd53c2dca87679120fc402 Mon Sep 17 00:00:00 2001 From: Jinnapat Indrapiromkul Date: Tue, 27 Aug 2024 11:38:23 +0200 Subject: [PATCH 1/3] Not print empty line when use capture in jupyter --- rich/console.py | 4 +++- tests/test_console.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/rich/console.py b/rich/console.py index 9e697327b..eb0cb6241 100644 --- a/rich/console.py +++ b/rich/console.py @@ -2037,7 +2037,9 @@ def _write_buffer(self) -> None: if self.is_jupyter: # pragma: no cover from .jupyter import display - display(self._buffer, self._render_buffer(self._buffer[:])) + output = self._render_buffer(self._buffer[:]) + if output and len(output) > 0: + display(self._buffer, output) del self._buffer[:] else: if WINDOWS: diff --git a/tests/test_console.py b/tests/test_console.py index 0340fc90e..4d21e403c 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -965,6 +965,15 @@ def test_force_color_jupyter(): assert not console.is_terminal +@mock.patch("rich.jupyter.display") +def test_capture_not_print_empty_line_jupyter(display: mock.MagicMock): + output = io.StringIO() + console = Console(file=output, force_jupyter=True) + with console.capture(): + pass + display.assert_not_called() + + def test_force_color(): console = Console( file=io.StringIO(), From 50dfba1420c276a978a5481781c4e56366b70938 Mon Sep 17 00:00:00 2001 From: Jinnapat Indrapiromkul Date: Tue, 27 Aug 2024 11:52:36 +0200 Subject: [PATCH 2/3] Add changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e14bd69a6..31abe204a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [13.8.1] + +### Fixed +- Fixed `console.capture` context printing empty line in jupyter https://github.com/Textualize/rich/pull/3474 + ## [13.8.0] - 2024-08-26 ### Fixed From c2dafc46f2ed99a9d5f411a3bd515451d82f4712 Mon Sep 17 00:00:00 2001 From: Jinnapat Indrapiromkul Date: Wed, 28 Aug 2024 09:13:38 +0200 Subject: [PATCH 3/3] remove unnecessary input --- tests/test_console.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_console.py b/tests/test_console.py index 4d21e403c..db3d885af 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -967,8 +967,7 @@ def test_force_color_jupyter(): @mock.patch("rich.jupyter.display") def test_capture_not_print_empty_line_jupyter(display: mock.MagicMock): - output = io.StringIO() - console = Console(file=output, force_jupyter=True) + console = Console(force_jupyter=True) with console.capture(): pass display.assert_not_called()