|
30 | 30 |
|
31 | 31 |
|
32 | 32 | from . import commands, console, input |
33 | | -from .utils import ANSI_ESCAPE_SEQUENCE, wlen, str_width |
| 33 | +from .utils import wlen, unbracket, str_width |
34 | 34 | from .trace import trace |
35 | 35 |
|
36 | 36 |
|
@@ -421,42 +421,15 @@ def calc_screen(self) -> list[str]: |
421 | 421 |
|
422 | 422 | @staticmethod |
423 | 423 | def process_prompt(prompt: str) -> tuple[str, int]: |
424 | | - """Process the prompt. |
| 424 | + r"""Return a tuple with the prompt string and its visible length. |
425 | 425 |
|
426 | | - This means calculate the length of the prompt. The character \x01 |
427 | | - and \x02 are used to bracket ANSI control sequences and need to be |
428 | | - excluded from the length calculation. So also a copy of the prompt |
429 | | - is returned with these control characters removed.""" |
430 | | - |
431 | | - # The logic below also ignores the length of common escape |
432 | | - # sequences if they were not explicitly within \x01...\x02. |
433 | | - # They are CSI (or ANSI) sequences ( ESC [ ... LETTER ) |
434 | | - |
435 | | - # wlen from utils already excludes ANSI_ESCAPE_SEQUENCE chars, |
436 | | - # which breaks the logic below so we redefine it here. |
437 | | - def wlen(s: str) -> int: |
438 | | - return sum(str_width(i) for i in s) |
439 | | - |
440 | | - out_prompt = "" |
441 | | - l = wlen(prompt) |
442 | | - pos = 0 |
443 | | - while True: |
444 | | - s = prompt.find("\x01", pos) |
445 | | - if s == -1: |
446 | | - break |
447 | | - e = prompt.find("\x02", s) |
448 | | - if e == -1: |
449 | | - break |
450 | | - # Found start and end brackets, subtract from string length |
451 | | - l = l - (e - s + 1) |
452 | | - keep = prompt[pos:s] |
453 | | - l -= sum(map(wlen, ANSI_ESCAPE_SEQUENCE.findall(keep))) |
454 | | - out_prompt += keep + prompt[s + 1 : e] |
455 | | - pos = e + 1 |
456 | | - keep = prompt[pos:] |
457 | | - l -= sum(map(wlen, ANSI_ESCAPE_SEQUENCE.findall(keep))) |
458 | | - out_prompt += keep |
459 | | - return out_prompt, l |
| 426 | + The prompt string has the zero-width brackets recognized by shells |
| 427 | + (\x01 and \x02) removed. The length ignores anything between those |
| 428 | + brackets as well as any ANSI escape sequences. |
| 429 | + """ |
| 430 | + out_prompt = unbracket(prompt, including_content=False) |
| 431 | + visible_prompt = unbracket(prompt, including_content=True) |
| 432 | + return out_prompt, wlen(visible_prompt) |
460 | 433 |
|
461 | 434 | def bow(self, p: int | None = None) -> int: |
462 | 435 | """Return the 0-based index of the word break preceding p most |
|
0 commit comments