termenv
termenv copied to clipboard
Added GetCursorPosition()
Added GetCursorPosition which returns row
, column
or error
for the current position of the cursor on the terminal.
This is especially useful when users print unpredictable amount of output to terminal and want to clear lines but don't know how many lines have been printed.
The function uses the sequence "\033[6n" to get cursor's position and then parses the output. The method to get the output from terminal is a bit unorthodox but it had to be because interacting with terminal and reading its output is not straightforward.
This implementation was heavily inspired by this conversation but open to any suggestions or improvements so that getting cursor's position gets added to termenv.
Since we already do similar things (writing to / reading from terminal), I feel like we should solve this without relying on /bin/stty
.
@muesli I'm also not a huge fan of that implementation. Can you point me to an example for reading from terminal and/or writing to it in the source code so that I can try re-implementing the function?
We could probably reuse / amend the code in https://github.com/muesli/termenv/blob/master/termenv_unix.go#L213 as it's pretty much doing the same thing.