While the question here on Stack Overflow is old, it's certainly not outdated, and as such I wrote a complete example of how to do this.
The basic approach is:
- Enable processing of ANSI escape sequences on stdout.
- Disable ECHO and line mode on stdin.
- Send the ANSI sequence to query cursor position on stdout.
- Read the reply on stdin.
- Restore the settings for stdin and stdout.
For step 1, under Linux handling of ANSI escape sequences on stdout should be enabled by default, but under Windows they aren't, at least at the moment, which is why the example below uses SetConsoleMode to enable those. With regards to the kernel32.GetStdHandle() - calls, the Windows standard handle for stdin is -10 and for stdout it's -11, and we are just getting the file descriptors for those. These are Windows-only functions.
As for Linux, we can use termios to disable/enable ECHO and line mode. Of note is that termios isn't available under Windows.
For step 2, any input on stdin is buffered and only sent forward line-by-line, but we want to read all input on stdin as soon as possible. We also want to disable ECHO, so the reply to step 3 doesn't get printed out to the console.
Just for a good measure, the example below will give a result of (-1, -1) if something went wrong, so your code could e.g. try again.
import sys, re
if(sys.platform == "win32"):
import ctypes
from ctypes import wintypes
else:
import termios
def cursorPos():
if(sys.platform == "win32"):
OldStdinMode = ctypes.wintypes.DWORD()
OldStdoutMode = ctypes.wintypes.DWORD()
kernel32 = ctypes.windll.kernel32
kernel32.GetConsoleMode(kernel32.GetStdHandle(-10), ctypes.byref(OldStdinMode))
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 0)
kernel32.GetConsoleMode(kernel32.GetStdHandle(-11), ctypes.byref(OldStdoutMode))
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
OldStdinMode = termios.tcgetattr(sys.stdin)
_ = termios.tcgetattr(sys.stdin)
_[3] = _[3] & ~(termios.ECHO | termios.ICANON)
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, _)
try:
_ = ""
sys.stdout.write("\x1b[6n")
sys.stdout.flush()
while not (_ := _ + sys.stdin.read(1)).endswith('R'):
True
res = re.match(r".*\[(?P<y>\d*);(?P<x>\d*)R", _)
finally:
if(sys.platform == "win32"):
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), OldStdinMode)
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), OldStdoutMode)
else:
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, OldStdinMode)
if(res):
return (res.group("x"), res.group("y"))
return (-1, -1)
x, y = cursorPos()
print(f"Cursor x: {x}, y: {y}")
The resulting output should be akin to this:
Cursor x: 1, y: 30
Additional links that may be of use, if one wishes to dig deeper into all this and e.g. expand on the functionality here: Man-page for Linux's termios, Windows SetConsoleMode, Windows GetConsoleMode, Wikipedia-entry for ANSI escape sequences