A context manager that enables an alternative screen. See :meth:`~rich.console.Console.screen` for usage.
| 401 | |
| 402 | |
| 403 | class ScreenContext: |
| 404 | """A context manager that enables an alternative screen. See :meth:`~rich.console.Console.screen` for usage.""" |
| 405 | |
| 406 | def __init__( |
| 407 | self, console: "Console", hide_cursor: bool, style: StyleType = "" |
| 408 | ) -> None: |
| 409 | self.console = console |
| 410 | self.hide_cursor = hide_cursor |
| 411 | self.screen = Screen(style=style) |
| 412 | self._changed = False |
| 413 | |
| 414 | def update( |
| 415 | self, *renderables: RenderableType, style: Optional[StyleType] = None |
| 416 | ) -> None: |
| 417 | """Update the screen. |
| 418 | |
| 419 | Args: |
| 420 | renderable (RenderableType, optional): Optional renderable to replace current renderable, |
| 421 | or None for no change. Defaults to None. |
| 422 | style: (Style, optional): Replacement style, or None for no change. Defaults to None. |
| 423 | """ |
| 424 | if renderables: |
| 425 | self.screen.renderable = ( |
| 426 | Group(*renderables) if len(renderables) > 1 else renderables[0] |
| 427 | ) |
| 428 | if style is not None: |
| 429 | self.screen.style = style |
| 430 | self.console.print(self.screen, end="") |
| 431 | |
| 432 | def __enter__(self) -> "ScreenContext": |
| 433 | self._changed = self.console.set_alt_screen(True) |
| 434 | if self._changed and self.hide_cursor: |
| 435 | self.console.show_cursor(False) |
| 436 | return self |
| 437 | |
| 438 | def __exit__( |
| 439 | self, |
| 440 | exc_type: Optional[Type[BaseException]], |
| 441 | exc_val: Optional[BaseException], |
| 442 | exc_tb: Optional[TracebackType], |
| 443 | ) -> None: |
| 444 | if self._changed: |
| 445 | self.console.set_alt_screen(False) |
| 446 | if self.hide_cursor: |
| 447 | self.console.show_cursor(True) |
| 448 | |
| 449 | |
| 450 | class Group: |