Return the x, y coordinates of position 'pos'.
(self)
| 537 | self.pos = pos |
| 538 | |
| 539 | def pos2xy(self) -> tuple[int, int]: |
| 540 | """Return the x, y coordinates of position 'pos'.""" |
| 541 | |
| 542 | prompt_len, y = 0, 0 |
| 543 | char_widths: list[int] = [] |
| 544 | pos = self.pos |
| 545 | assert 0 <= pos <= len(self.buffer) |
| 546 | |
| 547 | # optimize for the common case: typing at the end of the buffer |
| 548 | if pos == len(self.buffer) and len(self.screeninfo) > 0: |
| 549 | y = len(self.screeninfo) - 1 |
| 550 | prompt_len, char_widths = self.screeninfo[y] |
| 551 | return prompt_len + sum(char_widths), y |
| 552 | |
| 553 | for prompt_len, char_widths in self.screeninfo: |
| 554 | offset = len(char_widths) |
| 555 | in_wrapped_line = prompt_len + sum(char_widths) >= self.console.width |
| 556 | if in_wrapped_line: |
| 557 | offset -= 1 # need to remove line-wrapping backslash |
| 558 | |
| 559 | if offset >= pos: |
| 560 | break |
| 561 | |
| 562 | if not in_wrapped_line: |
| 563 | offset += 1 # there's a newline in buffer |
| 564 | |
| 565 | pos -= offset |
| 566 | y += 1 |
| 567 | return prompt_len + sum(char_widths[:pos]), y |
| 568 | |
| 569 | def insert(self, text: str | list[str]) -> None: |
| 570 | """Insert 'text' at the insertion point.""" |
no outgoing calls