A console renderable to draw a horizontal rule (line). Args: title (Union[str, Text], optional): Text to render in the rule. Defaults to "". characters (str, optional): Character(s) used to draw the line. Defaults to "─". style (StyleType, optional): Style of Rule. Defau
| 10 | |
| 11 | |
| 12 | class Rule(JupyterMixin): |
| 13 | """A console renderable to draw a horizontal rule (line). |
| 14 | |
| 15 | Args: |
| 16 | title (Union[str, Text], optional): Text to render in the rule. Defaults to "". |
| 17 | characters (str, optional): Character(s) used to draw the line. Defaults to "─". |
| 18 | style (StyleType, optional): Style of Rule. Defaults to "rule.line". |
| 19 | end (str, optional): Character at end of Rule. defaults to "\\\\n" |
| 20 | align (str, optional): How to align the title, one of "left", "center", or "right". Defaults to "center". |
| 21 | """ |
| 22 | |
| 23 | def __init__( |
| 24 | self, |
| 25 | title: Union[str, Text] = "", |
| 26 | *, |
| 27 | characters: str = "─", |
| 28 | style: Union[str, Style] = "rule.line", |
| 29 | end: str = "\n", |
| 30 | align: AlignMethod = "center", |
| 31 | ) -> None: |
| 32 | if cell_len(characters) < 1: |
| 33 | raise ValueError( |
| 34 | "'characters' argument must have a cell width of at least 1" |
| 35 | ) |
| 36 | if align not in ("left", "center", "right"): |
| 37 | raise ValueError( |
| 38 | f'invalid value for align, expected "left", "center", "right" (not {align!r})' |
| 39 | ) |
| 40 | self.title = title |
| 41 | self.characters = characters |
| 42 | self.style = style |
| 43 | self.end = end |
| 44 | self.align = align |
| 45 | |
| 46 | def __repr__(self) -> str: |
| 47 | return f"Rule({self.title!r}, {self.characters!r})" |
| 48 | |
| 49 | def __rich_console__( |
| 50 | self, console: Console, options: ConsoleOptions |
| 51 | ) -> RenderResult: |
| 52 | width = options.max_width |
| 53 | |
| 54 | characters = ( |
| 55 | "-" |
| 56 | if (options.ascii_only and not self.characters.isascii()) |
| 57 | else self.characters |
| 58 | ) |
| 59 | |
| 60 | chars_len = cell_len(characters) |
| 61 | if not self.title: |
| 62 | yield self._rule_line(chars_len, width) |
| 63 | return |
| 64 | |
| 65 | if isinstance(self.title, Text): |
| 66 | title_text = self.title |
| 67 | else: |
| 68 | title_text = console.render_str(self.title, style="rule.text") |
| 69 |
no outgoing calls