diff --git a/CHANGELOG.md b/CHANGELOG.md index 24306ba81..6888cd36b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305 - Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294 - Fix edges used in first row of tables when `show_header=False` https://github.com/Textualize/rich/pull/2330 +- Fix interaction between `Capture` contexts and `Console(record=True)` https://github.com/Textualize/rich/pull/2343 - Fixed hash issue in Styles class https://github.com/Textualize/rich/pull/2346 ## [12.4.4] - 2022-05-24 diff --git a/rich/console.py b/rich/console.py index eba27611d..230ca769c 100644 --- a/rich/console.py +++ b/rich/console.py @@ -1959,11 +1959,11 @@ def _check_buffer(self) -> None: del self._buffer[:] return with self._lock: - if self._buffer_index == 0: + if self.record: + with self._record_buffer_lock: + self._record_buffer.extend(self._buffer[:]) - if self.record: - with self._record_buffer_lock: - self._record_buffer.extend(self._buffer[:]) + if self._buffer_index == 0: if self.is_jupyter: # pragma: no cover from .jupyter import display diff --git a/tests/test_console.py b/tests/test_console.py index 828d37757..2db88af0b 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -351,6 +351,23 @@ def test_capture(): assert capture.get() == "Hello\n" +def test_capture_and_record(capsys): + recorder = Console(record=True) + recorder.print("ABC") + + with recorder.capture() as capture: + recorder.print("Hello") + + assert capture.get() == "Hello\n" + + recorded_text = recorder.export_text() + out, err = capsys.readouterr() + + assert recorded_text == "ABC\nHello\n" + assert capture.get() == "Hello\n" + assert out == "ABC\n" + + def test_input(monkeypatch, capsys): def fake_input(prompt=""): console.file.write(prompt)