Skip to content

Commit 6cef0bc

Browse files
committed
leaner cell_len
1 parent 46150cd commit 6cef0bc

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

rich/cells.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@
55

66
from ._cell_widths import CELL_WIDTHS
77

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
1429
]
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()
1635

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
1839

1940

2041
@lru_cache(4096)
@@ -61,20 +82,7 @@ def get_character_cell_size(character: str) -> int:
6182
Returns:
6283
int: Number of cells (0, 1 or 2) occupied by that character.
6384
"""
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)
7886
_table = CELL_WIDTHS
7987
lower_bound = 0
8088
upper_bound = len(_table) - 1

0 commit comments

Comments
 (0)