Getting the Current Cursor Position from a Terminal
When using ANSI escape sequences to manipulate a compatible terminal, you may need to know where the text cursor is currently located on the screen, especially if its current location was set by the user or a previous program rather than your code. Fortunately, the ANSI escape sequence standard ECMA-48 provides a sequence for doing just that.