Context manager to capture the result of printing to the console. See :meth:`~rich.console.Console.capture` for how to use. Args: console (Console): A console instance to capture output.
| 308 | |
| 309 | |
| 310 | class Capture: |
| 311 | """Context manager to capture the result of printing to the console. |
| 312 | See :meth:`~rich.console.Console.capture` for how to use. |
| 313 | |
| 314 | Args: |
| 315 | console (Console): A console instance to capture output. |
| 316 | """ |
| 317 | |
| 318 | def __init__(self, console: "Console") -> None: |
| 319 | self._console = console |
| 320 | self._result: Optional[str] = None |
| 321 | |
| 322 | def __enter__(self) -> "Capture": |
| 323 | self._console.begin_capture() |
| 324 | return self |
| 325 | |
| 326 | def __exit__( |
| 327 | self, |
| 328 | exc_type: Optional[Type[BaseException]], |
| 329 | exc_val: Optional[BaseException], |
| 330 | exc_tb: Optional[TracebackType], |
| 331 | ) -> None: |
| 332 | self._result = self._console.end_capture() |
| 333 | |
| 334 | def get(self) -> str: |
| 335 | """Get the result of the capture.""" |
| 336 | if self._result is None: |
| 337 | raise CaptureError( |
| 338 | "Capture result is not available until context manager exits." |
| 339 | ) |
| 340 | return self._result |
| 341 | |
| 342 | |
| 343 | class ThemeContext: |