| 1 |
import re |
| 2 |
import unicodedata |
| 3 |
import functools |
| 4 |
|
| 5 |
from .types import CharBuffer, CharWidths |
| 6 |
from .trace import trace |
| 7 |
|
| 8 |
ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]") |
| 9 |
ZERO_WIDTH_BRACKET = re.compile(r"\x01.*?\x02") |
| 10 |
ZERO_WIDTH_TRANS = str.maketrans({"\x01": "", "\x02": ""}) |
| 11 |
|
| 12 |
|
| 13 |
@functools.cache |
| 14 |
def str_width(c: str) -> int: |
| 15 |
if ord(c) < 128: |
| 16 |
return 1 |
| 17 |
# gh-139246 for zero-width joiner and combining characters |
| 18 |
if unicodedata.combining(c): |
| 19 |
return 0 |
| 20 |
category = unicodedata.category(c) |
| 21 |
if category == "Cf" and c != "\u00ad": |
| 22 |
return 0 |
| 23 |
w = unicodedata.east_asian_width(c) |
| 24 |
if w in ("N", "Na", "H", "A"): |
| 25 |
return 1 |
| 26 |
return 2 |
| 27 |
|
| 28 |
|
| 29 |
def wlen(s: str) -> int: |
| 30 |
if len(s) == 1 and s != "\x1a": |
| 31 |
return str_width(s) |
| 32 |
length = sum(str_width(i) for i in s) |
| 33 |
# remove lengths of any escape sequences |
| 34 |
sequence = ANSI_ESCAPE_SEQUENCE.findall(s) |
| 35 |
ctrl_z_cnt = s.count("\x1a") |
| 36 |
return length - sum(len(i) for i in sequence) + ctrl_z_cnt |
| 37 |
|
| 38 |
|
| 39 |
def unbracket(s: str, including_content: bool = False) -> str: |
| 40 |
r"""Return `s` with \001 and \002 characters removed. |
| 41 |
|
| 42 |
If `including_content` is True, content between \001 and \002 is also |
| 43 |
stripped. |
| 44 |
""" |
| 45 |
if including_content: |
| 46 |
return ZERO_WIDTH_BRACKET.sub("", s) |
| 47 |
return s.translate(ZERO_WIDTH_TRANS) |
| 48 |
|
| 49 |
|
| 50 |
def disp_str(buffer: str) -> tuple[CharBuffer, CharWidths]: |
| 51 |
r"""Decompose the input buffer into a printable variant. |
| 52 |
|
| 53 |
Returns a tuple of two lists: |
| 54 |
- the first list is the input buffer, character by character; |
| 55 |
- the second list is the visible width of each character in the input |
| 56 |
buffer. |
| 57 |
|
| 58 |
Examples: |
| 59 |
>>> utils.disp_str("a = 9") |
| 60 |
(['a', ' ', '=', ' ', '9'], [1, 1, 1, 1, 1]) |
| 61 |
""" |
| 62 |
chars: CharBuffer = [] |
| 63 |
char_widths: CharWidths = [] |
| 64 |
|
| 65 |
if not buffer: |
| 66 |
return chars, char_widths |
| 67 |
|
| 68 |
for c in buffer: |
| 69 |
if c == "\x1a": # CTRL-Z on Windows |
| 70 |
chars.append(c) |
| 71 |
char_widths.append(2) |
| 72 |
elif ord(c) < 128: |
| 73 |
chars.append(c) |
| 74 |
char_widths.append(1) |
| 75 |
elif unicodedata.category(c).startswith("C"): |
| 76 |
c = r"\u%04x" % ord(c) |
| 77 |
chars.append(c) |
| 78 |
char_widths.append(len(c)) |
| 79 |
else: |
| 80 |
chars.append(c) |
| 81 |
char_widths.append(str_width(c)) |
| 82 |
trace("disp_str({buffer}) = {s}, {b}", buffer=repr(buffer), s=chars, b=char_widths) |
| 83 |
return chars, char_widths |