MCPcopy Index your code
hub / github.com/RustPython/RustPython / pos2xy

Method pos2xy

Lib/_pyrepl/reader.py:587–611  ·  view source on GitHub ↗

Return the x, y coordinates of position 'pos'.

(self)

Source from the content-addressed store, hash-verified

585 self.pos = pos
586
587 def pos2xy(self) -> tuple[int, int]:
588 """Return the x, y coordinates of position 'pos'."""
589 # this *is* incomprehensible, yes.
590 p, y = 0, 0
591 l2: list[int] = []
592 pos = self.pos
593 assert 0 <= pos <= len(self.buffer)
594 if pos == len(self.buffer) and len(self.screeninfo) > 0:
595 y = len(self.screeninfo) - 1
596 p, l2 = self.screeninfo[y]
597 return p + sum(l2) + l2.count(0), y
598
599 for p, l2 in self.screeninfo:
600 l = len(l2) - l2.count(0)
601 in_wrapped_line = p + sum(l2) >= self.console.width
602 offset = l - 1 if in_wrapped_line else l # need to remove backslash
603 if offset >= pos:
604 break
605
606 if p + sum(l2) >= self.console.width:
607 pos -= l - 1 # -1 cause backslash is not in buffer
608 else:
609 pos -= l + 1 # +1 cause newline is in buffer
610 y += 1
611 return p + sum(l2[:pos]), y
612
613 def insert(self, text: str | list[str]) -> None:
614 """Insert 'text' at the insertion point."""

Callers 5

__post_init__Method · 0.95
calc_screenMethod · 0.95
update_cursorMethod · 0.95
doMethod · 0.80
doMethod · 0.80

Calls 3

lenFunction · 0.85
sumFunction · 0.50
countMethod · 0.45

Tested by

no test coverage detected