rich/rich/screen.py

55 lines
1.5 KiB
Python
Raw Normal View History

2021-04-23 02:56:02 +00:00
from typing import Optional, TYPE_CHECKING
2021-02-14 09:48:34 +00:00
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:
2021-05-22 16:06:43 +00:00
from .console import (
Console,
ConsoleOptions,
RenderResult,
RenderableType,
2021-07-28 08:10:23 +00:00
Group,
2021-05-22 16:06:43 +00:00
)
2021-02-14 09:48:34 +00:00
2021-02-12 21:59:35 +00:00
class Screen:
2021-02-15 20:48:36 +00:00
"""A renderable that fills the terminal screen and crops excess.
Args:
renderable (RenderableType): Child renderable.
style (StyleType, optional): Optional background style. Defaults to None.
"""
2021-05-28 15:09:56 +00:00
renderable: "RenderableType"
2021-02-14 09:48:34 +00:00
def __init__(
2021-04-23 02:56:02 +00:00
self,
2021-05-22 16:06:43 +00:00
*renderables: "RenderableType",
2021-04-23 02:56:02 +00:00
style: Optional[StyleType] = None,
2021-05-22 16:06:43 +00:00
application_mode: bool = False,
2021-02-14 09:48:34 +00:00
) -> None:
2021-07-28 08:10:23 +00:00
from rich.console import Group
2021-05-22 16:06:43 +00:00
2021-07-28 08:10:23 +00:00
self.renderable = Group(*renderables)
2021-02-12 21:59:35 +00:00
self.style = style
2021-05-22 16:06:43 +00:00
self.application_mode = application_mode
2021-02-12 21:59:35 +00:00
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)
2021-05-22 16:06:43 +00:00
new_line = Segment("\n\r") if self.application_mode else Segment.line()
2021-02-12 21:59:35 +00:00
for last, line in loop_last(lines):
yield from line
if not last:
yield new_line