Debian Codegraph @

lib_pypy/pyrepl/utils.py

raw symbols SCIP
1 import re
2 import unicodedata
3 import functools
4
5 ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
6
7
8 @functools.cache
9 def str_width(c: str) -> int:
10 if ord(c) < 128:
11 return 1
12 w = unicodedata.east_asian_width(c)
13 if w in ('N', 'Na', 'H', 'A'):
14 return 1
15 return 2
16
17
18 def wlen(s: str) -> int:
19 if len(s) == 1:
20 return str_width(s)
21 length = sum(str_width(i) for i in s)
22 # remove lengths of any escape sequences
23 sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
24 return length - sum(len(i) for i in sequence)