Align a renderable by adding spaces if necessary. Args: renderable (RenderableType): A console renderable. align (AlignMethod): One of "left", "center", or "right"" style (StyleType, optional): An optional style to apply to the background. vertical (Optional[Vert
| 15 | |
| 16 | |
| 17 | class Align(JupyterMixin): |
| 18 | class="st">"""Align a renderable by adding spaces if necessary. |
| 19 | |
| 20 | Args: |
| 21 | renderable (RenderableType): A console renderable. |
| 22 | align (AlignMethod): One of class="st">"left", class="st">"center", or class="st">"right"" |
| 23 | style (StyleType, optional): An optional style to apply to the background. |
| 24 | vertical (Optional[VerticalAlignMethod], optional): Optional vertical align, one of class="st">"top", class="st">"middle", or class="st">"bottom". Defaults to None. |
| 25 | pad (bool, optional): Pad the right with spaces. Defaults to True. |
| 26 | width (int, optional): Restrict contents to given width, or None to use default width. Defaults to None. |
| 27 | height (int, optional): Set height of align renderable, or None to fit to contents. Defaults to None. |
| 28 | |
| 29 | Raises: |
| 30 | ValueError: if ``align`` is not one of the expected values. |
| 31 | |
| 32 | Example: |
| 33 | .. code-block:: python |
| 34 | |
| 35 | from rich.console import Console |
| 36 | from rich.align import Align |
| 37 | from rich.panel import Panel |
| 38 | |
| 39 | console = Console() |
| 40 | class="cm"># Create a panel 20 characters wide |
| 41 | p = Panel(class="st">"Hello, [b]World[/b]!", style=class="st">"on green", width=20) |
| 42 | |
| 43 | class="cm"># Renders the panel centered in the terminal |
| 44 | console.print(Align(p, align=class="st">"center")) |
| 45 | class="st">""" |
| 46 | |
| 47 | def __init__( |
| 48 | self, |
| 49 | renderable: class="st">"RenderableType", |
| 50 | align: AlignMethod = class="st">"left", |
| 51 | style: Optional[StyleType] = None, |
| 52 | *, |
| 53 | vertical: Optional[VerticalAlignMethod] = None, |
| 54 | pad: bool = True, |
| 55 | width: Optional[int] = None, |
| 56 | height: Optional[int] = None, |
| 57 | ) -> None: |
| 58 | if align not in (class="st">"left", class="st">"center", class="st">"right"): |
| 59 | raise ValueError( |
| 60 | f&class="cm">#x27;invalid value for align, expected class="st">"left", class="st">"center", or class="st">"right" (not {align!r})' |
| 61 | ) |
| 62 | if vertical is not None and vertical not in (class="st">"top", class="st">"middle", class="st">"bottom"): |
| 63 | raise ValueError( |
| 64 | f&class="cm">#x27;invalid value for vertical, expected class="st">"top", class="st">"middle", or class="st">"bottom" (not {vertical!r})' |
| 65 | ) |
| 66 | self.renderable = renderable |
| 67 | self.align = align |
| 68 | self.style = style |
| 69 | self.vertical = vertical |
| 70 | self.pad = pad |
| 71 | self.width = width |
| 72 | self.height = height |
| 73 | |
| 74 | def __repr__(self) -> str: |
no outgoing calls