screen.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from typing import Optional, TYPE_CHECKING
  2. from .segment import Segment
  3. from .style import StyleType
  4. from ._loop import loop_last
  5. if TYPE_CHECKING:
  6. from .console import (
  7. Console,
  8. ConsoleOptions,
  9. RenderResult,
  10. RenderableType,
  11. Group,
  12. )
  13. class Screen:
  14. """A renderable that fills the terminal screen and crops excess.
  15. Args:
  16. renderable (RenderableType): Child renderable.
  17. style (StyleType, optional): Optional background style. Defaults to None.
  18. """
  19. renderable: "RenderableType"
  20. def __init__(
  21. self,
  22. *renderables: "RenderableType",
  23. style: Optional[StyleType] = None,
  24. application_mode: bool = False,
  25. ) -> None:
  26. from pip._vendor.rich.console import Group
  27. self.renderable = Group(*renderables)
  28. self.style = style
  29. self.application_mode = application_mode
  30. def __rich_console__(
  31. self, console: "Console", options: "ConsoleOptions"
  32. ) -> "RenderResult":
  33. width, height = options.size
  34. style = console.get_style(self.style) if self.style else None
  35. render_options = options.update(width=width, height=height)
  36. lines = console.render_lines(
  37. self.renderable or "", render_options, style=style, pad=True
  38. )
  39. lines = Segment.set_shape(lines, width, height, style=style)
  40. new_line = Segment("\n\r") if self.application_mode else Segment.line()
  41. for last, line in loop_last(lines):
  42. yield from line
  43. if not last:
  44. yield new_line