rich/rich/screen.py

40 lines
1.2 KiB
Python
Raw Normal View History

2021-02-14 09:48:34 +00:00
from typing import TYPE_CHECKING
2021-02-12 21:59:35 +00:00
from .segment import Segment
from .style import StyleType
from ._loop import loop_last
2021-02-14 09:48:34 +00:00
if TYPE_CHECKING:
from .console import Console, ConsoleOptions, RenderResult, RenderableType
2021-02-12 21:59:35 +00:00
class Screen:
2021-02-14 09:48:34 +00:00
def __init__(
self, renderable: "RenderableType" = None, style: StyleType = None
) -> None:
2021-02-12 21:59:35 +00:00
"""A renderable that fills the terminal screen.
Args:
renderable (RenderableType): Child renderable.
style (StyleType, optional): Optional background style. Defaults to None.
"""
self.renderable = renderable
self.style = style
def __rich_console__(
2021-02-14 09:48:34 +00:00
self, console: "Console", options: "ConsoleOptions"
) -> "RenderResult":
2021-02-14 11:07:16 +00:00
width, height = options.size
2021-02-12 21:59:35 +00:00
style = console.get_style(self.style) if self.style else None
render_options = options.update(width=width, height=height)
lines = console.render_lines(
2021-02-14 09:48:34 +00:00
self.renderable or "", render_options, style=style, pad=True
2021-02-12 21:59:35 +00:00
)
lines = Segment.set_shape(lines, width, height, style=style)
new_line = Segment.line()
for last, line in loop_last(lines):
yield from line
if not last:
yield new_line