gh-131507: Refactor screen and cursor position calculations (GH-131547)

This is based off #131509.
This commit is contained in:
Łukasz Langa 2025-03-21 18:27:35 +01:00 committed by GitHub
parent 61317074d4
commit 4cc82ffa37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 96 additions and 81 deletions

View file

@ -25,12 +25,11 @@ import sys
from contextlib import contextmanager from contextlib import contextmanager
from dataclasses import dataclass, field, fields from dataclasses import dataclass, field, fields
import unicodedata
from _colorize import can_colorize, ANSIColors from _colorize import can_colorize, ANSIColors
from . import commands, console, input from . import commands, console, input
from .utils import wlen, unbracket, str_width from .utils import wlen, unbracket, disp_str
from .trace import trace from .trace import trace
@ -39,36 +38,6 @@ Command = commands.Command
from .types import Callback, SimpleContextManager, KeySpec, CommandName from .types import Callback, SimpleContextManager, KeySpec, CommandName
def disp_str(buffer: str) -> tuple[str, list[int]]:
"""disp_str(buffer:string) -> (string, [int])
Return the string that should be the printed representation of
|buffer| and a list detailing where the characters of |buffer|
get used up. E.g.:
>>> disp_str(chr(3))
('^C', [1, 0])
"""
b: list[int] = []
s: list[str] = []
for c in buffer:
if c == '\x1a':
s.append(c)
b.append(2)
elif ord(c) < 128:
s.append(c)
b.append(1)
elif unicodedata.category(c).startswith("C"):
c = r"\u%04x" % ord(c)
s.append(c)
b.append(len(c))
else:
s.append(c)
b.append(str_width(c))
return "".join(s), b
# syntax classes: # syntax classes:
SYNTAX_WHITESPACE, SYNTAX_WORD, SYNTAX_SYMBOL = range(3) SYNTAX_WHITESPACE, SYNTAX_WORD, SYNTAX_SYMBOL = range(3)
@ -347,14 +316,12 @@ class Reader:
pos -= offset pos -= offset
prompt_from_cache = (offset and self.buffer[offset - 1] != "\n") prompt_from_cache = (offset and self.buffer[offset - 1] != "\n")
lines = "".join(self.buffer[offset:]).split("\n") lines = "".join(self.buffer[offset:]).split("\n")
cursor_found = False cursor_found = False
lines_beyond_cursor = 0 lines_beyond_cursor = 0
for ln, line in enumerate(lines, num_common_lines): for ln, line in enumerate(lines, num_common_lines):
ll = len(line) line_len = len(line)
if 0 <= pos <= ll: if 0 <= pos <= line_len:
self.lxy = pos, ln self.lxy = pos, ln
cursor_found = True cursor_found = True
elif cursor_found: elif cursor_found:
@ -368,34 +335,34 @@ class Reader:
prompt_from_cache = False prompt_from_cache = False
prompt = "" prompt = ""
else: else:
prompt = self.get_prompt(ln, ll >= pos >= 0) prompt = self.get_prompt(ln, line_len >= pos >= 0)
while "\n" in prompt: while "\n" in prompt:
pre_prompt, _, prompt = prompt.partition("\n") pre_prompt, _, prompt = prompt.partition("\n")
last_refresh_line_end_offsets.append(offset) last_refresh_line_end_offsets.append(offset)
screen.append(pre_prompt) screen.append(pre_prompt)
screeninfo.append((0, [])) screeninfo.append((0, []))
pos -= ll + 1 pos -= line_len + 1
prompt, lp = self.process_prompt(prompt) prompt, prompt_len = self.process_prompt(prompt)
l, l2 = disp_str(line) chars, char_widths = disp_str(line)
wrapcount = (wlen(l) + lp) // self.console.width wrapcount = (sum(char_widths) + prompt_len) // self.console.width
if wrapcount == 0: trace("wrapcount = {wrapcount}", wrapcount=wrapcount)
offset += ll + 1 # Takes all of the line plus the newline if wrapcount == 0 or not char_widths:
offset += line_len + 1 # Takes all of the line plus the newline
last_refresh_line_end_offsets.append(offset) last_refresh_line_end_offsets.append(offset)
screen.append(prompt + l) screen.append(prompt + "".join(chars))
screeninfo.append((lp, l2)) screeninfo.append((prompt_len, char_widths))
else: else:
i = 0 pre = prompt
while l: prelen = prompt_len
prelen = lp if i == 0 else 0 for wrap in range(wrapcount + 1):
index_to_wrap_before = 0 index_to_wrap_before = 0
column = 0 column = 0
for character_width in l2: for char_width in char_widths:
if column + character_width >= self.console.width - prelen: if column + char_width + prelen >= self.console.width:
break break
index_to_wrap_before += 1 index_to_wrap_before += 1
column += character_width column += char_width
pre = prompt if i == 0 else "" if len(chars) > index_to_wrap_before:
if len(l) > index_to_wrap_before:
offset += index_to_wrap_before offset += index_to_wrap_before
post = "\\" post = "\\"
after = [1] after = [1]
@ -404,11 +371,14 @@ class Reader:
post = "" post = ""
after = [] after = []
last_refresh_line_end_offsets.append(offset) last_refresh_line_end_offsets.append(offset)
screen.append(pre + l[:index_to_wrap_before] + post) render = pre + "".join(chars[:index_to_wrap_before]) + post
screeninfo.append((prelen, l2[:index_to_wrap_before] + after)) render_widths = char_widths[:index_to_wrap_before] + after
l = l[index_to_wrap_before:] screen.append(render)
l2 = l2[index_to_wrap_before:] screeninfo.append((prelen, render_widths))
i += 1 chars = chars[index_to_wrap_before:]
char_widths = char_widths[index_to_wrap_before:]
pre = ""
prelen = 0
self.screeninfo = screeninfo self.screeninfo = screeninfo
self.cxy = self.pos2xy() self.cxy = self.pos2xy()
if self.msg: if self.msg:
@ -537,9 +507,9 @@ class Reader:
pos = 0 pos = 0
i = 0 i = 0
while i < y: while i < y:
prompt_len, character_widths = self.screeninfo[i] prompt_len, char_widths = self.screeninfo[i]
offset = len(character_widths) - character_widths.count(0) offset = len(char_widths)
in_wrapped_line = prompt_len + sum(character_widths) >= self.console.width in_wrapped_line = prompt_len + sum(char_widths) >= self.console.width
if in_wrapped_line: if in_wrapped_line:
pos += offset - 1 # -1 cause backslash is not in buffer pos += offset - 1 # -1 cause backslash is not in buffer
else: else:
@ -560,29 +530,33 @@ class Reader:
def pos2xy(self) -> tuple[int, int]: def pos2xy(self) -> tuple[int, int]:
"""Return the x, y coordinates of position 'pos'.""" """Return the x, y coordinates of position 'pos'."""
# this *is* incomprehensible, yes.
p, y = 0, 0 prompt_len, y = 0, 0
l2: list[int] = [] char_widths: list[int] = []
pos = self.pos pos = self.pos
assert 0 <= pos <= len(self.buffer) assert 0 <= pos <= len(self.buffer)
# optimize for the common case: typing at the end of the buffer
if pos == len(self.buffer) and len(self.screeninfo) > 0: if pos == len(self.buffer) and len(self.screeninfo) > 0:
y = len(self.screeninfo) - 1 y = len(self.screeninfo) - 1
p, l2 = self.screeninfo[y] prompt_len, char_widths = self.screeninfo[y]
return p + sum(l2) + l2.count(0), y return prompt_len + sum(char_widths), y
for prompt_len, char_widths in self.screeninfo:
offset = len(char_widths)
in_wrapped_line = prompt_len + sum(char_widths) >= self.console.width
if in_wrapped_line:
offset -= 1 # need to remove line-wrapping backslash
for p, l2 in self.screeninfo:
l = len(l2) - l2.count(0)
in_wrapped_line = p + sum(l2) >= self.console.width
offset = l - 1 if in_wrapped_line else l # need to remove backslash
if offset >= pos: if offset >= pos:
break break
if p + sum(l2) >= self.console.width: if not in_wrapped_line:
pos -= l - 1 # -1 cause backslash is not in buffer offset += 1 # there's a newline in buffer
else:
pos -= l + 1 # +1 cause newline is in buffer pos -= offset
y += 1 y += 1
return p + sum(l2[:pos]), y return prompt_len + sum(char_widths[:pos]), y
def insert(self, text: str | list[str]) -> None: def insert(self, text: str | list[str]) -> None:
"""Insert 'text' at the insertion point.""" """Insert 'text' at the insertion point."""

View file

@ -1,8 +1,10 @@
from collections.abc import Callable, Iterator from collections.abc import Callable, Iterator
Callback = Callable[[], object] type Callback = Callable[[], object]
SimpleContextManager = Iterator[None] type SimpleContextManager = Iterator[None]
KeySpec = str # like r"\C-c" type KeySpec = str # like r"\C-c"
CommandName = str # like "interrupt" type CommandName = str # like "interrupt"
EventTuple = tuple[CommandName, str] type EventTuple = tuple[CommandName, str]
Completer = Callable[[str, int], str | None] type Completer = Callable[[str, int], str | None]
type CharBuffer = list[str]
type CharWidths = list[int]

View file

@ -2,6 +2,9 @@ import re
import unicodedata import unicodedata
import functools import functools
from .types import CharBuffer, CharWidths
from .trace import trace
ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]") ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
ZERO_WIDTH_BRACKET = re.compile(r"\x01.*?\x02") ZERO_WIDTH_BRACKET = re.compile(r"\x01.*?\x02")
ZERO_WIDTH_TRANS = str.maketrans({"\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: if including_content:
return ZERO_WIDTH_BRACKET.sub("", s) return ZERO_WIDTH_BRACKET.sub("", s)
return s.translate(ZERO_WIDTH_TRANS) 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