|
5 | 5 |
|
6 | 6 | from ._cell_widths import CELL_WIDTHS
|
7 | 7 |
|
8 |
| -_SINGLE_CELLS = frozenset( |
9 |
| - [ |
10 |
| - *map(chr, range(0x20, 0x7E + 1)), |
11 |
| - *map(chr, range(0xA0, 0x02FF + 1)), |
12 |
| - *map(chr, range(0x0370, 0x0482 + 1)), |
13 |
| - *map(chr, range(0x2500, 0x25FF + 1)), |
| 8 | +# Ranges of unicode ordinals that produce a 1-cell wide character |
| 9 | +_SINGLE_CELL_UNICODE_RANGES: list[tuple[int, int]] = [ |
| 10 | + (0x20, 0x7E), # Latin (excluding non-printable) |
| 11 | + (0xA0, 0xAC), |
| 12 | + (0xAE, 0x002FF), |
| 13 | + (0x00370, 0x00482), # Greek / Cyrillic |
| 14 | + (0x02500, 0x025FC), # Box drawing, box elements, geometric shapes |
| 15 | + (0x02800, 0x028FF), # Braille |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +def _make_single_cell_set() -> frozenset[str]: |
| 20 | + """Combine ranges of ordinals in to a frozen set of strings. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + A frozenset of single cell characters. |
| 24 | +
|
| 25 | + """ |
| 26 | + character_range_lists = [ |
| 27 | + list(map(chr, range(_start, _end + 1))) |
| 28 | + for _start, _end in _SINGLE_CELL_UNICODE_RANGES |
14 | 29 | ]
|
15 |
| -) |
| 30 | + return frozenset(sum(character_range_lists, start=[])) |
| 31 | + |
| 32 | + |
| 33 | +# A set of characters that are a single cell wide |
| 34 | +_SINGLE_CELLS = _make_single_cell_set() |
16 | 35 |
|
17 |
| -_is_single_cell_widths = _SINGLE_CELLS.issuperset |
| 36 | +# When called with a string this will return True if all |
| 37 | +# characters are single-cell, otherwise False |
| 38 | +_is_single_cell_widths: Callable[[str], bool] = _SINGLE_CELLS.issuperset |
18 | 39 |
|
19 | 40 |
|
20 | 41 | @lru_cache(4096)
|
@@ -61,20 +82,7 @@ def get_character_cell_size(character: str) -> int:
|
61 | 82 | Returns:
|
62 | 83 | int: Number of cells (0, 1 or 2) occupied by that character.
|
63 | 84 | """
|
64 |
| - return _get_codepoint_cell_size(ord(character)) |
65 |
| - |
66 |
| - |
67 |
| -@lru_cache(maxsize=4096) |
68 |
| -def _get_codepoint_cell_size(codepoint: int) -> int: |
69 |
| - """Get the cell size of a character. |
70 |
| -
|
71 |
| - Args: |
72 |
| - codepoint (int): Codepoint of a character. |
73 |
| -
|
74 |
| - Returns: |
75 |
| - int: Number of cells (0, 1 or 2) occupied by that character. |
76 |
| - """ |
77 |
| - |
| 85 | + codepoint = ord(character) |
78 | 86 | _table = CELL_WIDTHS
|
79 | 87 | lower_bound = 0
|
80 | 88 | upper_bound = len(_table) - 1
|
|
0 commit comments