mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
gh-131507: Refactor screen and cursor position calculations (GH-131547)
This is based off #131509.
This commit is contained in:
parent
61317074d4
commit
4cc82ffa37
3 changed files with 96 additions and 81 deletions
|
@ -2,6 +2,9 @@ import re
|
|||
import unicodedata
|
||||
import functools
|
||||
|
||||
from .types import CharBuffer, CharWidths
|
||||
from .trace import trace
|
||||
|
||||
ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
|
||||
ZERO_WIDTH_BRACKET = re.compile(r"\x01.*?\x02")
|
||||
ZERO_WIDTH_TRANS = str.maketrans({"\x01": "", "\x02": ""})
|
||||
|
@ -36,3 +39,39 @@ def unbracket(s: str, including_content: bool = False) -> str:
|
|||
if including_content:
|
||||
return ZERO_WIDTH_BRACKET.sub("", s)
|
||||
return s.translate(ZERO_WIDTH_TRANS)
|
||||
|
||||
|
||||
def disp_str(buffer: str) -> tuple[CharBuffer, CharWidths]:
|
||||
r"""Decompose the input buffer into a printable variant.
|
||||
|
||||
Returns a tuple of two lists:
|
||||
- the first list is the input buffer, character by character;
|
||||
- the second list is the visible width of each character in the input
|
||||
buffer.
|
||||
|
||||
Examples:
|
||||
>>> utils.disp_str("a = 9")
|
||||
(['a', ' ', '=', ' ', '9'], [1, 1, 1, 1, 1])
|
||||
"""
|
||||
chars: CharBuffer = []
|
||||
char_widths: CharWidths = []
|
||||
|
||||
if not buffer:
|
||||
return chars, char_widths
|
||||
|
||||
for c in buffer:
|
||||
if c == "\x1a": # CTRL-Z on Windows
|
||||
chars.append(c)
|
||||
char_widths.append(2)
|
||||
elif ord(c) < 128:
|
||||
chars.append(c)
|
||||
char_widths.append(1)
|
||||
elif unicodedata.category(c).startswith("C"):
|
||||
c = r"\u%04x" % ord(c)
|
||||
chars.append(c)
|
||||
char_widths.append(len(c))
|
||||
else:
|
||||
chars.append(c)
|
||||
char_widths.append(str_width(c))
|
||||
trace("disp_str({buffer}) = {s}, {b}", buffer=repr(buffer), s=chars, b=char_widths)
|
||||
return chars, char_widths
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue