|
25 | 25 | BUILTINS = frozenset({str(name) for name in dir(builtins) if not name.startswith('_')}) |
26 | 26 |
|
27 | 27 |
|
28 | | -@functools.cache |
29 | | -def str_width(c: str) -> int: |
30 | | - if ord(c) < 128: |
31 | | - return 1 |
32 | | - # gh-139246 for zero-width joiner and combining characters |
33 | | - if unicodedata.combining(c): |
34 | | - return 0 |
35 | | - category = unicodedata.category(c) |
36 | | - if category == "Cf" and c != "\u00ad": |
37 | | - return 0 |
38 | | - w = unicodedata.east_asian_width(c) |
39 | | - if w in ("N", "Na", "H", "A"): |
40 | | - return 1 |
41 | | - return 2 |
42 | | - |
43 | | - |
44 | | -def wlen(s: str) -> int: |
45 | | - if len(s) == 1 and s != "\x1a": |
46 | | - return str_width(s) |
47 | | - length = sum(str_width(i) for i in s) |
48 | | - # remove lengths of any escape sequences |
49 | | - sequence = ANSI_ESCAPE_SEQUENCE.findall(s) |
50 | | - ctrl_z_cnt = s.count("\x1a") |
51 | | - return length - sum(len(i) for i in sequence) + ctrl_z_cnt |
52 | | - |
53 | | - |
54 | 28 | def THEME(**kwargs): |
55 | 29 | # Not cached: the user can modify the theme inside the interactive session. |
56 | 30 | return _colorize.get_theme(**kwargs).syntax |
@@ -86,6 +60,34 @@ class ColorSpan(NamedTuple): |
86 | 60 | tag: str |
87 | 61 |
|
88 | 62 |
|
| 63 | +@functools.cache |
| 64 | +def str_width(c: str) -> int: |
| 65 | + if ord(c) < 128: |
| 66 | + return 1 |
| 67 | + # gh-139246 for zero-width joiner and combining characters |
| 68 | + if unicodedata.combining(c): |
| 69 | + return 0 |
| 70 | + category = unicodedata.category(c) |
| 71 | + if category == "Cf" and c != "\u00ad": |
| 72 | + return 0 |
| 73 | + w = unicodedata.east_asian_width(c) |
| 74 | + if w in ("N", "Na", "H", "A"): |
| 75 | + return 1 |
| 76 | + return 2 |
| 77 | + |
| 78 | + |
| 79 | +def wlen(s: str) -> int: |
| 80 | + if len(s) == 1 and s != "\x1a": |
| 81 | + return str_width(s) |
| 82 | + length = sum(str_width(i) for i in s) |
| 83 | + # remove lengths of any escape sequences |
| 84 | + sequence = ANSI_ESCAPE_SEQUENCE.findall(s) |
| 85 | + ctrl_z_cnt = s.count("\x1a") |
| 86 | + return length - sum(len(i) for i in sequence) + ctrl_z_cnt |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
89 | 91 | def unbracket(s: str, including_content: bool = False) -> str: |
90 | 92 | r"""Return `s` with \001 and \002 characters removed. |
91 | 93 |
|
|
0 commit comments