I want to get the cursor's position in a terminal window. I know I can echo -e "33[6n"
and read
the output -s
silently as in this answer, but how can I do this in Python?
I've tried this contextmanager like this:
with Capturing() as output:
sys.stdout.write("e[6n")
print(output)
but it only captures the e[6n
('x1b[6n'
) escape sequence I write, not the ^[[x;yR1
sequence I need.
I've also tried spawning a subprocess
and getting its output, but again, only the escape sequence I write is captured:
output = subprocess.check_output(["echo", "33[6n"], shell=False)
print(output)
shell=True
makes the output be a list of empty strings.
Avoiding curses
(because this is supposed to be a simple, poor man's cursor pos getter), how can I get the escape sequence returned by printing e[6n
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…