I'm trying to find a way to get the terminal cursor position on Python 3, and store it in a variable. There's an escape sequence (\x1b[6n
) which writes the position to the standard input file. I'm struggling on finding a proper way to get this sequence. I tried to override sys.stdin
with my own class temporary, so I could get the sequence, and then use the original sys.stdin
again:
import sys
class Test:
def __init__(self, ogstdin) -> None:
self.ogstdin = ogstdin
self.buff = []
def write(self, text: str) -> None:
self.buff.append(text)
def fileno(self):
return self.ogstdin.fileno()
sys.stdin = Test(sys.stdin)
print("\x1b[6n", end="")
print(sys.stdin.buff)
Unfortunately, buff
is still an empty list, and the sequence is still shown on input. Meaning that the override did nothing useful.
As a side note, I wanted to use this for fixing this issue of my project.