PHP 8.2.30
Preview: rule.py Size: 4.66 KB
/proc/thread-self/root/proc/self/root/opt/alt/python38/lib/python3.8/site-packages/pip/_vendor/rich/rule.py

from typing import Union

from .align import AlignMethod
from .cells import cell_len, set_cell_size
from .console import Console, ConsoleOptions, RenderResult
from .jupyter import JupyterMixin
from .measure import Measurement
from .style import Style
from .text import Text


class Rule(JupyterMixin):
    """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. Defaults to "rule.line".
        end (str, optional): Character at end of Rule. defaults to "\\\\n"
        align (str, optional): How to align the title, one of "left", "center", or "right". Defaults to "center".
    """

    def __init__(
        self,
        title: Union[str, Text] = "",
        *,
        characters: str = "─",
        style: Union[str, Style] = "rule.line",
        end: str = "\n",
        align: AlignMethod = "center",
    ) -> None:
        if cell_len(characters) < 1:
            raise ValueError(
                "'characters' argument must have a cell width of at least 1"
            )
        if align not in ("left", "center", "right"):
            raise ValueError(
                f'invalid value for align, expected "left", "center", "right" (not {align!r})'
            )
        self.title = title
        self.characters = characters
        self.style = style
        self.end = end
        self.align = align

    def __repr__(self) -> str:
        return f"Rule({self.title!r}, {self.characters!r})"

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:
        width = options.max_width

        # Python3.6 doesn't have an isascii method on str
        isascii = getattr(str, "isascii", None) or (
            lambda s: all(ord(c) < 128 for c in s)
        )
        characters = (
            "-"
            if (options.ascii_only and not isascii(self.characters))
            else self.characters
        )

        chars_len = cell_len(characters)
        if not self.title:
            yield self._rule_line(chars_len, width)
            return

        if isinstance(self.title, Text):
            title_text = self.title
        else:
            title_text = console.render_str(self.title, style="rule.text")

        title_text.plain = title_text.plain.replace("\n", " ")
        title_text.expand_tabs()

        required_space = 4 if self.align == "center" else 2
        truncate_width = max(0, width - required_space)
        if not truncate_width:
            yield self._rule_line(chars_len, width)
            return

        rule_text = Text(end=self.end)
        if self.align == "center":
            title_text.truncate(truncate_width, overflow="ellipsis")
            side_width = (width - cell_len(title_text.plain)) // 2
            left = Text(characters * (side_width // chars_len + 1))
            left.truncate(side_width - 1)
            right_length = width - cell_len(left.plain) - cell_len(title_text.plain)
            right = Text(characters * (side_width // chars_len + 1))
            right.truncate(right_length)
            rule_text.append(left.plain + " ", self.style)
            rule_text.append(title_text)
            rule_text.append(" " + right.plain, self.style)
        elif self.align == "left":
            title_text.truncate(truncate_width, overflow="ellipsis")
            rule_text.append(title_text)
            rule_text.append(" ")
            rule_text.append(characters * (width - rule_text.cell_len), self.style)
        elif self.align == "right":
            title_text.truncate(truncate_width, overflow="ellipsis")
            rule_text.append(characters * (width - title_text.cell_len - 1), self.style)
            rule_text.append(" ")
            rule_text.append(title_text)

        rule_text.plain = set_cell_size(rule_text.plain, width)
        yield rule_text

    def _rule_line(self, chars_len: int, width: int) -> Text:
        rule_text = Text(self.characters * ((width // chars_len) + 1), self.style)
        rule_text.truncate(width)
        rule_text.plain = set_cell_size(rule_text.plain, width)
        return rule_text

    def __rich_measure__(
        self, console: Console, options: ConsoleOptions
    ) -> Measurement:
        return Measurement(1, 1)


if __name__ == "__main__":  # pragma: no cover
    import sys

    from pip._vendor.rich.console import Console

    try:
        text = sys.argv[1]
    except IndexError:
        text = "Hello, World"
    console = Console()
    console.print(Rule(title=text))

    console = Console()
    console.print(Rule("foo"), width=4)

Directory Contents

Dirs: 1 × Files: 75

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 22:49:50
Edit Download
890 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
10.13 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
6.66 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.19 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
9.63 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.40 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
17.54 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.03 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
6.96 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
93.64 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.26 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
5.37 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
6.47 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
7.77 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
972 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
2.44 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
642 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
2.45 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.58 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
9.36 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.93 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.18 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
13.74 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
13.84 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.58 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
11.20 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
8.01 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
5.18 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.85 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
828 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.32 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
8.54 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
35.72 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
58.35 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
7.97 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
11.04 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.36 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
166 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.34 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.66 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
2.78 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.55 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
23.66 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.27 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
4.32 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
25.63 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.23 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
33.88 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
38.59 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.29 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
43.62 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.54 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
102 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
25.45 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
8.95 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
9.86 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
136.95 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.04 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
2.06 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
265 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
9.47 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
3.15 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.21 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
6.90 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
423 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
5.34 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
19.45 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
351 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
417 B lrw-r--r-- 2023-11-13 21:40:26
Edit Download
22.29 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.88 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
2.72 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
1.80 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
5.80 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download
8.60 KB lrw-r--r-- 2023-11-13 21:40:26
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).