1from typing
import cast, List, Optional, Tuple, TYPE_CHECKING, Union
10from .jupyter
import JupyterMixin
11from .measure
import Measurement
12from .style
import Style
13from .segment
import Segment
16PaddingDimensions = Union[int, Tuple[int], Tuple[int, int], Tuple[int, int, int, int]]
20 """Draw space around content.
23 >>> print(Padding("Hello", (2, 4), style="on blue"))
26 renderable (RenderableType): String or other renderable.
27 pad (Union[int, Tuple[int]]): Padding for top, right, bottom, and left borders.
28 May be specified with 1, 2, or 4 integers (CSS style).
29 style (Union[str, Style], optional): Style for padding characters. Defaults to "none".
30 expand (bool, optional): Expand padding to fit available width. Defaults to True.
35 renderable:
"RenderableType",
36 pad:
"PaddingDimensions" = (0, 0, 0, 0),
38 style: Union[str, Style] =
"none",
47 def indent(cls, renderable:
"RenderableType", level: int) ->
"Padding":
48 """Make padding instance to render an indent.
51 renderable (RenderableType): String or other renderable.
52 level (int): Number of characters to indent.
55 Padding: A Padding instance.
58 return Padding(renderable, pad=(0, 0, 0, level), expand=
False)
61 def unpack(pad:
"PaddingDimensions") -> Tuple[int, int, int, int]:
62 """Unpack padding specified in CSS style."""
64 return (pad, pad, pad, pad)
67 return (_pad, _pad, _pad, _pad)
69 pad_top, pad_right = cast(Tuple[int, int], pad)
70 return (pad_top, pad_right, pad_top, pad_right)
72 top, right, bottom, left = cast(Tuple[int, int, int, int], pad)
73 return (top, right, bottom, left)
74 raise ValueError(f
"1, 2 or 4 integers required for padding; {len(pad)} given")
77 return f
"Padding({self.renderable!r}, ({self.top},{self.right},{self.bottom},{self.left}))"
80 self, console:
"Console", options:
"ConsoleOptions"
98 self.
renderable, render_options, style=style, pad=
True
108 blank_line: Optional[List[Segment]] =
None
110 blank_line = [
_Segment(f
'{" " * width}\n', style)]
111 yield from blank_line * self.
top
122 blank_line = blank_line
or [
_Segment(f
'{" " * width}\n', style)]
123 yield from blank_line * self.
bottom
126 self, console:
"Console", options:
"ConsoleOptions"
130 if max_width - extra_width < 1:
133 measurement =
Measurement(measure_min + extra_width, measure_max + extra_width)
138if __name__ ==
"__main__":
141 print(
Padding(
"Hello, World", (2, 4), style=
"on blue"))
"Measurement" __rich_measure__(self, "Console" console, "ConsoleOptions" options)
Tuple[int, int, int, int] unpack("PaddingDimensions" pad)
__init__(self, "RenderableType" renderable, "PaddingDimensions" pad=(0, 0, 0, 0), *Union[str, Style] style="none", bool expand=True)
"RenderResult" __rich_console__(self, "Console" console, "ConsoleOptions" options)
"Padding" indent(cls, "RenderableType" renderable, int level)