File: test_text_layout.py

package info (click to toggle)
urwid 2.6.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,236 kB
  • sloc: python: 29,290; javascript: 382; sh: 34; makefile: 22
file content (451 lines) | stat: -rw-r--r-- 16,278 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from __future__ import annotations

import typing
import unittest

import urwid
from urwid import text_layout
from urwid.util import get_encoding, set_temporary_encoding

if typing.TYPE_CHECKING:
    from typing_extensions import Literal


class CalcBreaksTest(unittest.TestCase):
    def cbtest(self, width, exp, mode, text):
        result = text_layout.default_layout.calculate_text_segments(text, width, mode)
        self.assertEqual(len(exp), len(result), f"Expected: {exp!r}, got {result!r}")
        for l, e in zip(result, exp):
            end = l[-1][-1]
            self.assertEqual(e, end, f"Expected: {exp!r}, got {result!r}")

    def validate(self, mode, text, do):
        for width, exp in do:
            self.cbtest(width, exp, mode, text)

    def test_calc_breaks_char(self):
        self.validate(
            mode="any",
            text=b"abfghsdjf askhtrvs\naltjhgsdf ljahtshgf",
            do=[
                (100, [18, 38]),
                (6, [6, 12, 18, 25, 31, 37, 38]),
                (10, [10, 18, 29, 38]),
            ],
        )

    def test_calc_reaks_db_char(self):
        with urwid.util.set_temporary_encoding("euc-jp"):
            self.validate(
                mode="any",
                text=b"abfgh\xA1\xA1j\xA1\xA1xskhtrvs\naltjhgsdf\xA1\xA1jahtshgf",
                do=[
                    (10, [10, 18, 28, 38]),
                    (6, [5, 11, 17, 18, 25, 31, 37, 38]),
                    (100, [18, 38]),
                ],
            )

    def test_calc_breaks_word(self):
        self.validate(
            mode="space",
            text=b"hello world\nout there. blah",
            do=[
                (10, [5, 11, 22, 27]),
                (5, [5, 11, 17, 22, 27]),
                (100, [11, 27]),
            ],
        )

    def test_calc_breaks_word_2(self):
        self.validate(
            mode="space",
            text=b"A simple set of words, really....",
            do=[
                (10, [8, 15, 22, 33]),
                (17, [15, 33]),
                (13, [12, 22, 33]),
            ],
        )

    def test_calc_breaks_db_word(self):
        with urwid.util.set_temporary_encoding("euc-jp"):
            self.validate(
                mode="space",
                text=b"hel\xA1\xA1 world\nout-\xA1\xA1tre blah",
                # tests
                do=[
                    (10, [5, 11, 21, 26]),
                    (5, [5, 11, 16, 21, 26]),
                    (100, [11, 26]),
                ],
            )

    def test_calc_breaks_utf8(self):
        with urwid.util.set_temporary_encoding("utf-8"):
            self.validate(
                mode="space",
                # As text: "替洼渎溏潺"
                text=b"\xe6\x9b\xbf\xe6\xb4\xbc\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba",
                do=[
                    (4, [6, 12, 15]),
                    (10, [15]),
                    (5, [6, 12, 15]),
                ],
            )


class CalcBreaksCantDisplayTest(unittest.TestCase):
    def test(self):
        with set_temporary_encoding("euc-jp"):
            self.assertRaises(
                text_layout.CanNotDisplayText,
                text_layout.default_layout.calculate_text_segments,
                b"\xA1\xA1",
                1,
                "space",
            )
        with set_temporary_encoding("utf-8"):
            self.assertRaises(
                text_layout.CanNotDisplayText,
                text_layout.default_layout.calculate_text_segments,
                "颖",
                1,
                "space",
            )


class SubsegTest(unittest.TestCase):
    def setUp(self):
        self.old_encoding = get_encoding()
        urwid.set_encoding("euc-jp")

    def tearDown(self) -> None:
        urwid.set_encoding(self.old_encoding)

    def st(self, seg, text: bytes, start: int, end: int, exp):
        s = urwid.LayoutSegment(seg)
        result = s.subseg(text, start, end)
        self.assertEqual(exp, result, f"Expected {exp!r}, got {result!r}")

    def test1_padding(self):
        self.st((10, None), b"", 0, 8, [(8, None)])
        self.st((10, None), b"", 2, 10, [(8, None)])
        self.st((10, 0), b"", 3, 7, [(4, 0)])
        self.st((10, 0), b"", 0, 20, [(10, 0)])

    def test2_text(self):
        self.st((10, 0, b"1234567890"), b"", 0, 8, [(8, 0, b"12345678")])
        self.st((10, 0, b"1234567890"), b"", 2, 10, [(8, 0, b"34567890")])
        self.st((10, 0, b"12\xA1\xA156\xA1\xA190"), b"", 2, 8, [(6, 0, b"\xA1\xA156\xA1\xA1")])
        self.st((10, 0, b"12\xA1\xA156\xA1\xA190"), b"", 3, 8, [(5, 0, b" 56\xA1\xA1")])
        self.st((10, 0, b"12\xA1\xA156\xA1\xA190"), b"", 2, 7, [(5, 0, b"\xA1\xA156 ")])
        self.st((10, 0, b"12\xA1\xA156\xA1\xA190"), b"", 3, 7, [(4, 0, b" 56 ")])
        self.st((10, 0, b"12\xA1\xA156\xA1\xA190"), b"", 0, 20, [(10, 0, b"12\xA1\xA156\xA1\xA190")])

    def test3_range(self):
        t = b"1234567890"
        self.st((10, 0, 10), t, 0, 8, [(8, 0, 8)])
        self.st((10, 0, 10), t, 2, 10, [(8, 2, 10)])
        self.st((6, 2, 8), t, 1, 6, [(5, 3, 8)])
        self.st((6, 2, 8), t, 0, 5, [(5, 2, 7)])
        self.st((6, 2, 8), t, 1, 5, [(4, 3, 7)])
        t = b"12\xA1\xA156\xA1\xA190"
        self.st((10, 0, 10), t, 0, 8, [(8, 0, 8)])
        self.st((10, 0, 10), t, 2, 10, [(8, 2, 10)])
        self.st((6, 2, 8), t, 1, 6, [(1, 3), (4, 4, 8)])
        self.st((6, 2, 8), t, 0, 5, [(4, 2, 6), (1, 6)])
        self.st((6, 2, 8), t, 1, 5, [(1, 3), (2, 4, 6), (1, 6)])


class CalcTranslateTest(unittest.TestCase):
    def setUp(self) -> None:
        self.old_encoding = get_encoding()
        urwid.set_encoding("utf-8")

    def tearDown(self) -> None:
        urwid.set_encoding(self.old_encoding)

    def check(self, text, mode, width, result_left, result_center, result_right) -> None:
        with self.subTest("left"):
            result = urwid.default_layout.layout(text, width, "left", mode)
            self.assertEqual(result_left, result)

        with self.subTest("center"):
            result = urwid.default_layout.layout(text, width, "center", mode)
            self.assertEqual(result_center, result)

        with self.subTest("right"):
            result = urwid.default_layout.layout(text, width, "right", mode)
            self.assertEqual(result_right, result)

    def test_calc_translate_char(self):
        self.check(
            text="It's out of control!\nYou've got to",
            mode="any",
            width=15,
            result_left=[[(15, 0, 15)], [(5, 15, 20), (0, 20)], [(13, 21, 34), (0, 34)]],
            result_center=[[(15, 0, 15)], [(5, None), (5, 15, 20), (0, 20)], [(1, None), (13, 21, 34), (0, 34)]],
            result_right=[[(15, 0, 15)], [(10, None), (5, 15, 20), (0, 20)], [(2, None), (13, 21, 34), (0, 34)]],
        )

    def test_calc_translate_word(self):
        self.check(
            text="It's out of control!\nYou've got to",
            mode="space",
            width=14,
            result_left=[
                [(11, 0, 11), (0, 11)],
                [(8, 12, 20), (0, 20)],
                [(13, 21, 34), (0, 34)],
            ],
            result_center=[
                [(2, None), (11, 0, 11), (0, 11)],
                [(3, None), (8, 12, 20), (0, 20)],
                [(1, None), (13, 21, 34), (0, 34)],
            ],
            result_right=[
                [(3, None), (11, 0, 11), (0, 11)],
                [(6, None), (8, 12, 20), (0, 20)],
                [(1, None), (13, 21, 34), (0, 34)],
            ],
        )

    def test_calc_translate(self):
        self.check(
            text="It's out of control!\nYou've got to ",
            mode="space",
            width=14,
            result_left=[
                [(11, 0, 11), (0, 11)],
                [(8, 12, 20), (0, 20)],
                [(14, 21, 35), (0, 35)],
            ],
            result_center=[
                [(2, None), (11, 0, 11), (0, 11)],
                [(3, None), (8, 12, 20), (0, 20)],
                [(14, 21, 35), (0, 35)],
            ],
            result_right=[
                [(3, None), (11, 0, 11), (0, 11)],
                [(6, None), (8, 12, 20), (0, 20)],
                [(14, 21, 35), (0, 35)],
            ],
        )

    def test_calc_translate_word_2(self):
        self.check(
            text="It's out of control!\nYou've got to ",
            mode="space",
            width=14,
            result_left=[[(11, 0, 11), (0, 11)], [(8, 12, 20), (0, 20)], [(14, 21, 35), (0, 35)]],
            result_center=[
                [(2, None), (11, 0, 11), (0, 11)],
                [(3, None), (8, 12, 20), (0, 20)],
                [(14, 21, 35), (0, 35)],
            ],
            result_right=[
                [(3, None), (11, 0, 11), (0, 11)],
                [(6, None), (8, 12, 20), (0, 20)],
                [(14, 21, 35), (0, 35)],
            ],
        )

    def test_calc_translate_word_3(self):
        # As bytes: b'\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba'
        # Decoded as UTF-8: "替洼\n渎溏潺"
        self.check(
            text=b"\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba",
            width=10,
            mode="space",
            result_left=[[(4, 0, 6), (0, 6)], [(6, 7, 16), (0, 16)]],
            result_center=[[(3, None), (4, 0, 6), (0, 6)], [(2, None), (6, 7, 16), (0, 16)]],
            result_right=[[(6, None), (4, 0, 6), (0, 6)], [(4, None), (6, 7, 16), (0, 16)]],
        )

    def test_calc_translate_word_3_decoded(self):
        # As bytes: b'\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba'
        # Decoded as UTF-8: "替洼\n渎溏潺"
        self.check(
            text="替洼\n渎溏潺",
            width=10,
            mode="space",
            result_left=[[(4, 0, 2), (0, 2)], [(6, 3, 6), (0, 6)]],
            result_center=[[(3, None), (4, 0, 2), (0, 2)], [(2, None), (6, 3, 6), (0, 6)]],
            result_right=[[(6, None), (4, 0, 2), (0, 2)], [(4, None), (6, 3, 6), (0, 6)]],
        )

    def test_calc_translate_word_4(self):
        self.check(
            text=" Die Gedank",
            width=3,
            mode="space",
            result_left=[[(0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]],
            result_center=[[(2, None), (0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]],
            result_right=[[(3, None), (0, 0)], [(3, 1, 4), (0, 4)], [(3, 5, 8)], [(3, 8, 11), (0, 11)]],
        )

    def test_calc_translate_word_5(self):
        self.check(
            text=" Word.",
            width=3,
            mode="space",
            result_left=[[(3, 0, 3)], [(3, 3, 6), (0, 6)]],
            result_center=[[(3, 0, 3)], [(3, 3, 6), (0, 6)]],
            result_right=[[(3, 0, 3)], [(3, 3, 6), (0, 6)]],
        )

    def test_calc_translate_clip(self):
        self.check(
            text="It's out of control!\nYou've got to\n\nturn it off!!!",
            mode="clip",
            width=14,
            result_left=[
                [(20, 0, 20), (0, 20)],
                [(13, 21, 34), (0, 34)],
                [(0, 35)],
                [(14, 36, 50), (0, 50)],
            ],
            result_center=[
                [(-3, None), (20, 0, 20), (0, 20)],
                [(1, None), (13, 21, 34), (0, 34)],
                [(7, None), (0, 35)],
                [(14, 36, 50), (0, 50)],
            ],
            result_right=[
                [(-6, None), (20, 0, 20), (0, 20)],
                [(1, None), (13, 21, 34), (0, 34)],
                [(14, None), (0, 35)],
                [(14, 36, 50), (0, 50)],
            ],
        )

    def test_calc_translate_clip_2(self):
        self.check(
            text="Hello!\nto\nWorld!",
            mode="clip",
            width=5,  # line width (of first and last lines) minus one
            result_left=[
                [(6, 0, 6), (0, 6)],
                [(2, 7, 9), (0, 9)],
                [(6, 10, 16), (0, 16)],
            ],
            result_center=[
                [(6, 0, 6), (0, 6)],
                [(2, None), (2, 7, 9), (0, 9)],
                [(6, 10, 16), (0, 16)],
            ],
            result_right=[
                [(-1, None), (6, 0, 6), (0, 6)],
                [(3, None), (2, 7, 9), (0, 9)],
                [(-1, None), (6, 10, 16), (0, 16)],
            ],
        )

    def test_calc_translate_cant_display(self):
        self.check(
            text="Hello颖",
            mode="space",
            width=1,
            result_left=[[]],
            result_center=[[]],
            result_right=[[]],
        )


class CalcPosTest(unittest.TestCase):
    def setUp(self):
        self.text = "A" * 27
        self.trans = [[(2, None), (7, 0, 7), (0, 7)], [(13, 8, 21), (0, 21)], [(3, None), (5, 22, 27), (0, 27)]]
        self.mytests = [
            (1, 0, 0),
            (2, 0, 0),
            (11, 0, 7),
            (-3, 1, 8),
            (-2, 1, 8),
            (1, 1, 9),
            (31, 1, 21),
            (1, 2, 22),
            (11, 2, 27),
        ]

    def tests(self):
        for x, y, expected in self.mytests:
            got = text_layout.calc_pos(self.text, self.trans, x, y)
            self.assertEqual(expected, got, f"{x, y!r} got:{got!r} expected:{expected!r}")


class Pos2CoordsTest(unittest.TestCase):
    pos_list = [5, 9, 20, 26]
    text = "1234567890" * 3
    mytests = [
        ([[(15, 0, 15)], [(15, 15, 30), (0, 30)]], [(5, 0), (9, 0), (5, 1), (11, 1)]),
        ([[(9, 0, 9)], [(12, 9, 21)], [(9, 21, 30), (0, 30)]], [(5, 0), (0, 1), (11, 1), (5, 2)]),
        ([[(2, None), (15, 0, 15)], [(2, None), (15, 15, 30), (0, 30)]], [(7, 0), (11, 0), (7, 1), (13, 1)]),
        ([[(3, 6, 9), (0, 9)], [(5, 20, 25), (0, 25)]], [(0, 0), (3, 0), (0, 1), (5, 1)]),
        ([[(10, 0, 10), (0, 10)]], [(5, 0), (9, 0), (10, 0), (10, 0)]),
    ]

    def test(self):
        for t, answer in self.mytests:
            for pos, a in zip(self.pos_list, answer):
                r = text_layout.calc_coords(self.text, t, pos)
                self.assertEqual(a, r, f"{t!r} got: {r!r} expected: {a!r}")


class TestEllipsis(unittest.TestCase):
    def test_ellipsis_encoding_support(self):
        widget = urwid.Text("Test label", wrap=urwid.WrapMode.ELLIPSIS)

        with self.subTest("Unicode"), set_temporary_encoding("utf-8"):
            widget._invalidate()
            canvas = widget.render((5,))
            self.assertEqual("Test…", str(canvas))

        with self.subTest("ascii"), set_temporary_encoding("ascii"):
            widget._invalidate()
            canvas = widget.render((5,))
            self.assertEqual("Te...", str(canvas))

        with self.subTest("ascii not fit"), set_temporary_encoding("ascii"):
            widget._invalidate()
            canvas = widget.render((3,))
            self.assertEqual("T..", str(canvas))

        with self.subTest("ascii nothing fit"), set_temporary_encoding("ascii"):
            widget._invalidate()
            canvas = widget.render((1,))
            self.assertEqual("T", str(canvas))


class NumericLayout(urwid.TextLayout):
    """
    TextLayout class for bottom-right aligned numbers
    """

    def layout(
        self,
        text: str | bytes,
        width: int,
        align: Literal["left", "center", "right"] | urwid.Align,
        wrap: Literal["any", "space", "clip", "ellipsis"] | urwid.WrapMode,
    ) -> list[list[tuple[int, int, int | bytes] | tuple[int, int | None]]]:
        """
        Return layout structure for right justified numbers.
        """
        lt = len(text)
        r = lt % width  # remaining segment not full width wide
        if r:
            return [
                [(width - r, None), (r, 0, r)],  # right-align the remaining segment on 1st line
                *([(width, x, x + width)] for x in range(r, lt, width)),  # fill the rest of the lines
            ]

        return [[(width, x, x + width)] for x in range(0, lt, width)]


class TestTextLayoutNoPack(unittest.TestCase):
    def test(self):
        """Text widget pack should work also with layout not supporting `pack` method."""
        widget = urwid.Text("123", layout=NumericLayout())
        self.assertEqual((3, 1), widget.pack((3,)))