File size: 3,074 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from rich.segment import Segment
from rich.style import Style


from textual._segment_tools import line_crop, line_trim, line_pad


def test_line_crop():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment(" World!", italic),
    ]
    total = sum(segment.cell_length for segment in segments)

    assert line_crop(segments, 1, 2, total) == [Segment("e", bold)]
    assert line_crop(segments, 4, 20, total) == [
        Segment("o", bold),
        Segment(" World!", italic),
    ]


def test_line_crop_emoji():
    bold = Style(bold=True)
    italic = Style(italic=True)
    segments = [
        Segment("Hello", bold),
        Segment("๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ", italic),
    ]
    total = sum(segment.cell_length for segment in segments)
    assert line_crop(segments, 8, 11, total) == [Segment(" ๐Ÿ’ฉ", italic)]
    assert line_crop(segments, 9, 11, total) == [Segment("๐Ÿ’ฉ", italic)]


def test_line_crop_edge():
    segments = [Segment("foo"), Segment("bar"), Segment("baz")]
    total = sum(segment.cell_length for segment in segments)

    assert line_crop(segments, 2, 9, total) == [
        Segment("o"),
        Segment("bar"),
        Segment("baz"),
    ]
    assert line_crop(segments, 3, 9, total) == [Segment("bar"), Segment("baz")]
    assert line_crop(segments, 4, 9, total) == [Segment("ar"), Segment("baz")]
    assert line_crop(segments, 4, 8, total) == [Segment("ar"), Segment("ba")]


def test_line_crop_edge_2():
    segments = [
        Segment("โ•ญโ”€"),
        Segment(
            "โ”€โ”€โ”€โ”€โ”€โ”€ Placeholder โ”€โ”€โ”€โ”€โ”€โ”€โ”€",
        ),
        Segment(
            "โ”€โ•ฎ",
        ),
    ]
    total = sum(segment.cell_length for segment in segments)
    result = line_crop(segments, 30, 60, total)
    expected = []
    print(repr(result))
    assert result == expected


def test_line_trim():
    segments = [Segment("foo")]

    assert line_trim(segments, False, False) == segments
    assert line_trim(segments, True, False) == [Segment("oo")]
    assert line_trim(segments, False, True) == [Segment("fo")]
    assert line_trim(segments, True, True) == [Segment("o")]

    fob_segments = [Segment("f"), Segment("o"), Segment("b")]

    assert line_trim(fob_segments, True, False) == [
        Segment("o"),
        Segment("b"),
    ]

    assert line_trim(fob_segments, False, True) == [
        Segment("f"),
        Segment("o"),
    ]

    assert line_trim(fob_segments, True, True) == [
        Segment("o"),
    ]

    assert line_trim([], True, True) == []


def test_line_pad():
    segments = [Segment("foo"), Segment("bar")]
    style = Style.parse("red")
    assert line_pad(segments, 2, 3, style) == [
        Segment("  ", style),
        *segments,
        Segment("   ", style),
    ]

    assert line_pad(segments, 0, 3, style) == [
        *segments,
        Segment("   ", style),
    ]

    assert line_pad(segments, 2, 0, style) == [
        Segment("  ", style),
        *segments,
    ]

    assert line_pad(segments, 0, 0, style) == segments