Beispiel #1
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> RenderResult:
     for y in range(0, 5):
         for x in range(options.max_width):
             h = x / options.max_width
             l = 0.1 + ((y / 5) * 0.7)
             r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
             r2, g2, b2 = colorsys.hls_to_rgb(h, l + 0.7 / 10, 1.0)
             bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
             color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
             yield Segment("โ–„", Style(color=color, bgcolor=bgcolor))
         yield Segment.line()
Beispiel #2
0
def test_adjust_line_length():
    line = [Segment("Hello", "foo")]
    assert Segment.adjust_line_length(line, 10, style="bar") == [
        Segment("Hello", "foo"),
        Segment("     ", "bar"),
    ]

    line = [Segment("H"), Segment("ello, World!")]
    assert Segment.adjust_line_length(line,
                                      5) == [Segment("H"),
                                             Segment("ello")]

    line = [Segment("Hello")]
    assert Segment.adjust_line_length(line, 5) == line
Beispiel #3
0
def test_split_and_crop_lines():
    assert list(
        Segment.split_and_crop_lines(
            [Segment("Hello\nWorld!\n"),
             Segment("foo")], 4)) == [[Segment("Hell")], [Segment("Worl")],
                                      [Segment("foo"),
                                       Segment(" ")]]
Beispiel #4
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> Iterable[Segment]:
     height = console.size.height - 3
     for y in range(0, height):
         for x in range(options.max_width):
             h = x / options.max_width
             l = y / (height + 1)
             r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
             r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2),
                                              1.0)
             bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
             color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
             yield Segment("โ–„", Style(color=color, bgcolor=bgcolor))
         yield Segment.line()
Beispiel #5
0
def test_rich_console():
    renderable = "test renderable"
    style = Style(color="red")
    options = ConsoleOptions(
        ConsoleDimensions(80, 25),
        max_height=25,
        legacy_windows=False,
        min_width=10,
        max_width=20,
        is_terminal=False,
        encoding="utf-8",
    )

    expected_outputs = [
        Segment(renderable, style=style),
        Segment(" " * (20 - len(renderable)), style=style),
        Segment("\n", style=None),
    ]
    padding_generator = Padding(renderable, style=style).__rich_console__(
        Console(), options
    )
    for output, expected in zip(padding_generator, expected_outputs):
        assert output == expected
Beispiel #6
0
def test_repr():
    assert repr(Segment("foo")) == "Segment('foo', None)"
    assert repr(Segment.control("foo")) == "Segment.control('foo', None)"
Beispiel #7
0
def test_get_pulse_segments():
    bar = Bar()
    segments = bar._get_pulse_segments(
        Style.parse("red"), Style.parse("yellow"), "standard", False
    )
    print(repr(segments))
    expected = [
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("red"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
        Segment("โ”", Style.parse("yellow"), False),
    ]
    assert segments == expected
Beispiel #8
0
def test_split_lines():
    lines = [Segment("Hello\nWorld")]
    assert list(Segment.split_lines(lines)) == [[Segment("Hello")],
                                                [Segment("World")]]
Beispiel #9
0
def test_strip_links():
    segments = [
        Segment("foo", Style(bold=True, link="https://www.example.org"))
    ]
    assert list(
        Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]
Beispiel #10
0
def test_render_size():
    console = Console(width=63, height=46, legacy_windows=False)
    options = console.options.update_dimensions(80, 4)
    lines = console.render_lines(Panel("foo", title="Hello"), options=options)
    print(repr(lines))
    expected = [
        [
            Segment("โ•ญโ”€", Style()),
            Segment(
                "โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Hello โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€"
            ),
            Segment("โ”€โ•ฎ", Style()),
        ],
        [
            Segment("โ”‚", Style()),
            Segment(" ", Style()),
            Segment("foo"),
            Segment(
                "                                                                         "
            ),
            Segment(" ", Style()),
            Segment("โ”‚", Style()),
        ],
        [
            Segment("โ”‚", Style()),
            Segment(" ", Style()),
            Segment(
                "                                                                            ",
                Style(),
            ),
            Segment(" ", Style()),
            Segment("โ”‚", Style()),
        ],
        [
            Segment(
                "โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ",
                Style(),
            )
        ],
    ]
    assert lines == expected
Beispiel #11
0
def test_strip_styles():
    segments = [Segment("foo", Style(bold=True))]
    assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
Beispiel #12
0
def test_line():
    assert Segment.line() == Segment("\n")
Beispiel #13
0
def test_repr():
    assert repr(Segment("foo")) == "Segment('foo', None)"
    home = (ControlType.HOME, 0)
    assert (repr(Segment(
        "foo", None,
        [home])) == "Segment('foo', None, [(<ControlType.HOME: 3>, 0)])")
Beispiel #14
0
def test_control_move_to():
    control = Control.move_to(5, 10)
    print(control.segment)
    assert control.segment == Segment("\x1b[11;6H", None,
                                      [(ControlType.CURSOR_MOVE_TO, 5, 10)])
Beispiel #15
0
def test_segment_lines_renderable():
    lines = [[Segment("hello"),
              Segment(" "), Segment("world")], [Segment("foo")]]
    segment_lines = SegmentLines(lines)
    assert list(segment_lines.__rich_console__(None, None)) == [
        Segment("hello"),
        Segment(" "),
        Segment("world"),
        Segment("foo"),
    ]

    segment_lines = SegmentLines(lines, new_lines=True)
    assert list(segment_lines.__rich_console__(None, None)) == [
        Segment("hello"),
        Segment(" "),
        Segment("world"),
        Segment("\n"),
        Segment("foo"),
        Segment("\n"),
    ]
Beispiel #16
0
def test_split_cells_emoji(text, split, result):
    assert Segment(text).split_cells(split) == result
Beispiel #17
0
def test_divide_emoji():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment("๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ", italic),
    ]

    assert list(Segment.divide(segments, [7])) == [
        [Segment("Hello", bold), Segment("๐Ÿ’ฉ", italic)],
    ]
    assert list(Segment.divide(segments, [8])) == [
        [Segment("Hello", bold), Segment("๐Ÿ’ฉ ", italic)],
    ]
    assert list(Segment.divide(segments, [9])) == [
        [Segment("Hello", bold), Segment("๐Ÿ’ฉ๐Ÿ’ฉ", italic)],
    ]
    assert list(Segment.divide(segments, [8, 11])) == [
        [Segment("Hello", bold), Segment("๐Ÿ’ฉ ", italic)],
        [Segment(" ๐Ÿ’ฉ", italic)],
    ]
    assert list(Segment.divide(segments, [9, 11])) == [
        [Segment("Hello", bold), Segment("๐Ÿ’ฉ๐Ÿ’ฉ", italic)],
        [Segment("๐Ÿ’ฉ", italic)],
    ]
Beispiel #18
0
def test_divide():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment(" World!", italic),
    ]

    assert list(Segment.divide(segments, [])) == []
    assert list(Segment.divide([], [1])) == [[]]

    assert list(Segment.divide(segments, [1])) == [[Segment("H", bold)]]

    assert list(Segment.divide(segments, [1, 2])) == [
        [Segment("H", bold)],
        [Segment("e", bold)],
    ]

    assert list(Segment.divide(segments, [1, 2, 12])) == [
        [Segment("H", bold)],
        [Segment("e", bold)],
        [Segment("llo", bold),
         Segment(" World!", italic)],
    ]

    assert list(Segment.divide(segments, [4, 20])) == [
        [Segment("Hell", bold)],
        [Segment("o", bold), Segment(" World!", italic)],
    ]
Beispiel #19
0
def test_divide_edge_2():
    segments = [
        Segment("โ•ญโ”€"),
        Segment("โ”€โ”€โ”€โ”€โ”€โ”€ Placeholder โ”€โ”€โ”€โ”€โ”€โ”€โ”€", ),
        Segment("โ”€โ•ฎ", ),
    ]
    result = list(Segment.divide(segments, [30, 60]))
    expected = [segments, []]
    print(repr(result))
    assert result == expected


@pytest.mark.parametrize(
    "text,split,result",
    [
        ("X", 1, (Segment("X"), Segment(""))),
        ("๐Ÿ’ฉ", 1, (Segment(" "), Segment(" "))),
        ("XY", 1, (Segment("X"), Segment("Y"))),
        ("๐Ÿ’ฉX", 1, (Segment(" "), Segment(" X"))),
        ("๐Ÿ’ฉ๐Ÿ’ฉ", 1, (Segment(" "), Segment(" ๐Ÿ’ฉ"))),
        ("X๐Ÿ’ฉY", 2, (Segment("X "), Segment(" Y"))),
        ("X๐Ÿ’ฉYZ", 2, (Segment("X "), Segment(" YZ"))),
        ("X๐Ÿ’ฉ๐Ÿ’ฉZ", 2, (Segment("X "), Segment(" ๐Ÿ’ฉZ"))),
        ("X๐Ÿ’ฉ๐Ÿ’ฉZ", 3, (Segment("X๐Ÿ’ฉ"), Segment("๐Ÿ’ฉZ"))),
        ("X๐Ÿ’ฉ๐Ÿ’ฉZ", 4, (Segment("X๐Ÿ’ฉ "), Segment(" Z"))),
        ("X๐Ÿ’ฉ๐Ÿ’ฉZ", 5, (Segment("X๐Ÿ’ฉ๐Ÿ’ฉ"), Segment("Z"))),
        ("X๐Ÿ’ฉ๐Ÿ’ฉZ", 6, (Segment("X๐Ÿ’ฉ๐Ÿ’ฉZ"), Segment(""))),
        ("XYZABC๐Ÿ’ฉ๐Ÿ’ฉ", 6, (Segment("XYZABC"), Segment("๐Ÿ’ฉ๐Ÿ’ฉ"))),
        ("XYZABC๐Ÿ’ฉ๐Ÿ’ฉ", 7, (Segment("XYZABC "), Segment(" ๐Ÿ’ฉ"))),
        ("XYZABC๐Ÿ’ฉ๐Ÿ’ฉ", 8, (Segment("XYZABC๐Ÿ’ฉ"), Segment("๐Ÿ’ฉ"))),
        ("XYZABC๐Ÿ’ฉ๐Ÿ’ฉ", 9, (Segment("XYZABC๐Ÿ’ฉ "), Segment(" "))),
Beispiel #20
0
def test_get_line_length():
    assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6
Beispiel #21
0
def test_get_shape():
    assert Segment.get_shape([[Segment("Hello")]]) == (5, 1)
    assert Segment.get_shape([[Segment("Hello")],
                              [Segment("World!")]]) == (6, 2)
Beispiel #22
0
def test_control_hide_cursor(legacy_term_mock):
    buffer = [Segment("", None, [(ControlType.HIDE_CURSOR, )])]

    legacy_windows_render(buffer, legacy_term_mock)

    legacy_term_mock.hide_cursor.assert_called_once_with()
Beispiel #23
0
def test_filter_control():
    segments = [Segment("foo"), Segment("bar", is_control=True)]
    assert list(Segment.filter_control(segments)) == [Segment("foo")]
    assert list(Segment.filter_control(
        segments, is_control=True)) == [Segment("bar", is_control=True)]
Beispiel #24
0
def test_text_only(legacy_term_mock):
    text = "Hello, world!"
    buffer = [Segment(text)]
    legacy_windows_render(buffer, legacy_term_mock)

    legacy_term_mock.write_text.assert_called_once_with(text)
Beispiel #25
0
def test_control_erase_line(legacy_term_mock, erase_mode, method_name):
    buffer = [Segment("", None, [(ControlType.ERASE_IN_LINE, erase_mode)])]

    legacy_windows_render(buffer, legacy_term_mock)

    getattr(legacy_term_mock, method_name).assert_called_once_with()
Beispiel #26
0
    def render_bar(
            cls,
            size: int = 25,
            virtual_size: float = 50,
            window_size: float = 20,
            position: float = 0,
            ascii_only: bool = False,
            thickness: int = 1,
            vertical: bool = True,
            back_color: Color = Color.parse("#555555"),
            bar_color: Color = Color.parse("bright_magenta"),
    ) -> Segments:

        if vertical:
            if ascii_only:
                bars = ["|", "|", "|", "|", "|", "|", "|", "|"]
            else:
                bars = ["โ–", "โ–‚", "โ–ƒ", "โ–„", "โ–…", "โ–†", "โ–‡", "โ–ˆ"]
        else:
            if ascii_only:
                bars = ["-", "-", "-", "-", "-", "-", "-", "-"]
            else:
                bars = ["โ–ˆ", "โ–‰", "โ–Š", "โ–‹", "โ–Œ", "โ–", "โ–Ž", "โ–"]

        back = back_color
        bar = bar_color

        width_thickness = thickness if vertical else 1

        _Segment = Segment
        _Style = Style
        blank = " " * width_thickness

        foreground_meta = {"@mouse.up": "release", "@mouse.down": "grab"}
        if window_size and size and virtual_size:
            step_size = virtual_size / size

            start = int(position / step_size * 8)
            end = start + max(8, int(window_size / step_size * 8))

            start_index, start_bar = divmod(start, 8)
            end_index, end_bar = divmod(end, 8)

            upper = {"@click": "scroll_up"}
            lower = {"@click": "scroll_down"}

            upper_back_segment = Segment(blank, _Style(bgcolor=back,
                                                       meta=upper))
            lower_back_segment = Segment(blank, _Style(bgcolor=back,
                                                       meta=lower))

            segments = [upper_back_segment] * int(size)
            segments[end_index:] = [lower_back_segment] * (size - end_index)

            segments[start_index:end_index] = [
                _Segment(blank, _Style(bgcolor=bar, meta=foreground_meta))
            ] * (end_index - start_index)

            if start_index < len(segments):
                segments[start_index] = _Segment(
                    bars[7 - start_bar] * width_thickness,
                    _Style(bgcolor=back, color=bar, meta=foreground_meta)
                    if vertical else _Style(
                        bgcolor=bar, color=back, meta=foreground_meta),
                )
            if end_index < len(segments):
                segments[end_index] = _Segment(
                    bars[7 - end_bar] * width_thickness,
                    _Style(bgcolor=bar, color=back, meta=foreground_meta)
                    if vertical else _Style(
                        bgcolor=back, color=bar, meta=foreground_meta),
                )
        else:
            segments = [_Segment(blank)] * int(size)
        if vertical:
            return Segments(segments, new_lines=True)
        else:
            return Segments((segments + [_Segment.line()]) * thickness,
                            new_lines=False)
Beispiel #27
0
def test_control_cursor_move_to_column(legacy_term_mock):
    buffer = [Segment("", None, [(ControlType.CURSOR_MOVE_TO_COLUMN, 3)])]

    legacy_windows_render(buffer, legacy_term_mock)

    legacy_term_mock.move_cursor_to_column.assert_called_once_with(2)
Beispiel #28
0
def test_is_control():
    assert Segment("foo", Style(bold=True)).is_control == False
    assert Segment("foo", Style(bold=True), []).is_control == True
    assert Segment("foo", Style(bold=True), [(ControlType.HOME, 0)]).is_control == True
Beispiel #29
0
def test_control_carriage_return(legacy_term_mock):
    buffer = [Segment("", None, [(ControlType.CARRIAGE_RETURN, )])]

    legacy_windows_render(buffer, legacy_term_mock)

    legacy_term_mock.write_text.assert_called_once_with("\r")
Beispiel #30
0
def test_repr():
    assert repr(Segment("foo")) == "Segment('foo', None, False)"