mirror of
https://github.com/python/cpython.git
synced 2025-07-26 20:54:39 +00:00

(cherry picked from commite6572e8f98
) Also includes: * gh-111201: Use calc_complete_screen after bracketed paste in PyREPL (GH-119432) (cherry picked from commit14b063cbf1
) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
22 lines
506 B
Python
22 lines
506 B
Python
import re
|
|
import unicodedata
|
|
import functools
|
|
|
|
ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
|
|
|
|
|
|
@functools.cache
|
|
def str_width(c: str) -> int:
|
|
if ord(c) < 128:
|
|
return 1
|
|
w = unicodedata.east_asian_width(c)
|
|
if w in ('N', 'Na', 'H', 'A'):
|
|
return 1
|
|
return 2
|
|
|
|
|
|
def wlen(s: str) -> int:
|
|
length = sum(str_width(i) for i in s)
|
|
# remove lengths of any escape sequences
|
|
sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
|
|
return length - sum(len(i) for i in sequence)
|