Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed selective enabling of highlighting when disabled in the `Console` https://github.com/Textualize/rich/issues/3419
- Fixed BrokenPipeError writing an error message https://github.com/Textualize/rich/pull/3468
- Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469
- Fixed issue with record and capture interaction https://github.com/Textualize/rich/pull/3470

### Changed

Expand Down
2 changes: 1 addition & 1 deletion rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ def _write_buffer(self) -> None:
"""Write the buffer to the output file."""

with self._lock:
if self.record:
if self.record and not self._buffer_index:
with self._record_buffer_lock:
self._record_buffer.extend(self._buffer[:])

Expand Down
38 changes: 21 additions & 17 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,23 +381,6 @@ 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)
Expand Down Expand Up @@ -1038,3 +1021,24 @@ def test_brokenpipeerror() -> None:
proc2.wait()
assert proc1.returncode == 1
assert proc2.returncode == 0


def test_capture_and_record() -> None:
"""Regression test for https://github.com/Textualize/rich/issues/2563"""

console = Console(record=True)
print("Before Capture started:")
console.print("[blue underline]Print 0")
with console.capture() as capture:
console.print("[blue underline]Print 1")
console.print("[blue underline]Print 2")
console.print("[blue underline]Print 3")
console.print("[blue underline]Print 4")

capture_content = capture.get()
print(repr(capture_content))
assert capture_content == "Print 1\nPrint 2\nPrint 3\nPrint 4\n"

recorded_content = console.export_text()
print(repr(recorded_content))
assert recorded_content == "Print 0\n"