[GTALUG] Finding current cursor position in the terminal

Giles Orr gilesorr at gmail.com
Mon Feb 10 14:54:43 EST 2020


I'd like to be able to get the current x,y (or row, column) position
of the cursor in a terminal.  This is for a Bash or ZSH prompt, and my
intention is to calculate how far across the terminal we've printed
and then decide if we should wrap the prompt from one to two lines.

There's a terminal escape sequence to do this:

    echo -e '\033[6n'

or:

    tput u7

These output the desired information in some form of extremely
difficult-to-parse code (works on Mac or any Linux, I think).  The
output looks like this:

    ^[[24;1R

The numbers are line and column respectively.

If you search the web, you can find various code (usually using the
builtin 'read' command) to parse this.  And some of the answers work
fine ... in this context, ie. running it as a stand-alone command.
But wrap it in a prompt and things go straight to hell.

    export PS1="--\$(echo -en '\e[6n')-- "

You'll find that some of the output appears after the prompt, as part
of the input line.  Any attempt to 'read' and parse the value within
the prompt hangs or returns empty, or does something else weird.  My
guess (uneducated and wild as it is) is that the escape sequence
doesn't complete until AFTER the prompt is displayed.  Because it's
waiting for some magical code the prompt provides?  I have no clue.
One or two of the pieces of code I've found do parse the result and
return properly ... but then they always return "1" for the column ...
(they usually get the row right).  And I'm primarily looking for the
column.

I've spent a lot of time searching for a solution, and trying all
kinds of weird Bash coding tricks, with no particular luck.  Does
anyone have any idea how this might be handled?

This is just one of many, many half-complete examples I've tracked
down.  The questioner eventually provided his own answer:

https://stackoverflow.com/questions/43911175/get-current-cursor-position-while-drawing-zsh-prompt

but what I found was that it always returns 1 for the column ...  The
author didn't care because they were only interested in the line
number.

Any help appreciated.

-- 
Giles
https://www.gilesorr.com/
gilesorr at gmail.com


More information about the talk mailing list