diff --git a/CHANGELOG.md b/CHANGELOG.md index d355ae608..8a8c6517a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add type annotation for key_separator of pretty.Node https://github.com/Textualize/rich/issues/2625 +- Add new parameter for capture method called echo. https://github.com/Textualize/rich/issues/2338 ## [12.6.0] - 2022-10-02 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 7e61d0104..a9b479359 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -12,6 +12,7 @@ The following people have contributed to the development of Rich: - [Ed Davis](https://github.com/davised) - [Pete Davison](https://github.com/pd93) - [James Estevez](https://github.com/jstvz) +- [Sertug Fidan](https://github.com/SertugF) - [Oleksis Fraga](https://github.com/oleksis) - [Andy Gimblett](https://github.com/gimbo) - [Michał Górny](https://github.com/mgorny) diff --git a/docs/source/console.rst b/docs/source/console.rst index 5c0aeb329..e62998289 100644 --- a/docs/source/console.rst +++ b/docs/source/console.rst @@ -313,10 +313,10 @@ Note that when writing to a file you may want to explicitly set the ``width`` ar Capturing output ---------------- -There may be situations where you want to *capture* the output from a Console rather than writing it directly to the terminal. You can do this with the :meth:`~rich.console.Console.capture` method which returns a context manager. On exit from this context manager, call :meth:`~rich.console.Capture.get` to return the string that would have been written to the terminal. Here's an example:: +There may be situations where you want to *capture* the output from a Console without writing it directly to the terminal. You can do this with the :meth:`~rich.console.Console.capture` method using echo parameter which returns a context manager. If echo value is set to True(default) text is captured and written to the terminal. When echo value is set to False text captured but not written to the terminal. On exit from this context manager, call :meth:`~rich.console.Capture.get` to return the string that would have been written to the terminal. Here's an example:: from rich.console import Console - console = Console() + console = Console(echo=False) with console.capture() as capture: console.print("[bold red]Hello[/] World") str_output = capture.get() diff --git a/rich/console.py b/rich/console.py index 38aadb3e8..01e0a2066 100644 --- a/rich/console.py +++ b/rich/console.py @@ -327,9 +327,10 @@ class Capture: console (Console): A console instance to capture output. """ - def __init__(self, console: "Console") -> None: + def __init__(self, console: "Console", echo: bool = True) -> None: self._console = console self._result: Optional[str] = None + self.echo = echo def __enter__(self) -> "Capture": self._console.begin_capture() @@ -342,6 +343,9 @@ def __exit__( exc_tb: Optional[TracebackType], ) -> None: self._result = self._console.end_capture() + if self.echo: + # print to the original console + self._console.print(self._result, end="") def get(self) -> str: """Get the result of the capture.""" @@ -1080,9 +1084,12 @@ def bell(self) -> None: """Play a 'bell' sound (if supported by the terminal).""" self.control(Control.bell()) - def capture(self) -> Capture: + def capture(self, echo: bool = True) -> Capture: """A context manager to *capture* the result of print() or log() in a string, - rather than writing it to the console. + rather than writing it to the console depending on the echo argument. + + Args: + echo (bool, optional): Echos captured string to terminal. Defaults to True. Example: >>> from rich.console import Console @@ -1092,9 +1099,10 @@ def capture(self) -> Capture: >>> print(capture.get()) Returns: - Capture: Context manager with disables writing to the terminal. + Capture: Context manager depending on echo argument disables writing to the terminal. """ - capture = Capture(self) + + capture = Capture(self, echo=echo) return capture def pager( diff --git a/tests/test_console.py b/tests/test_console.py index 8c72e8da0..8c8de0190 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -384,7 +384,7 @@ def test_capture_and_record(capsys): recorder = Console(record=True) recorder.print("ABC") - with recorder.capture() as capture: + with recorder.capture(echo=False) as capture: recorder.print("Hello") assert capture.get() == "Hello\n" @@ -397,6 +397,26 @@ def test_capture_and_record(capsys): assert out == "ABC\n" +def test_capture_and_record_multiple(capsys): + recorder = Console(record=True) + + with recorder.capture(echo=False) as capture: + recorder.print("Hello") + recorder.print("World") + + assert capture.get() == "Hello\nWorld\n" + + with recorder.capture(echo=True) as capture: + recorder.print("Foo") + recorder.print("Bar") + + assert capture.get() == "Foo\nBar\n" + + out, err = capsys.readouterr() + + assert out == "Foo\nBar\n" + + def test_input(monkeypatch, capsys): def fake_input(prompt=""): console.file.write(prompt)