Process the prompt. This means calculate the length of the prompt. The character \x01 and \x02 are used to bracket ANSI control sequences and need to be excluded from the length calculation. So also a copy of the prompt is returned with these control characters remo
(prompt: str)
| 421 | |
| 422 | @staticmethod |
| 423 | def process_prompt(prompt: str) -> tuple[str, int]: |
| 424 | """Process the prompt. |
| 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 |
| 460 | |
| 461 | def bow(self, p: int | None = None) -> int: |
| 462 | """Return the 0-based index of the word break preceding p most |
no test coverage detected