For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2007 > Re: Get current screen location (was: Re: Terminal output with escape sequences: Any









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Get current screen location (was: Re: Terminal output with escape sequences: Any
Chris F.A. Johnson

2007-10-24, 4:32 am

On 2007-10-24, Markus Mayer wrote:
>
>
> By the way: Is there any function that allows me to retrieve the curremt
> cursor coordinates?


I use this shell function:

_curpos()
{
stty -echo;
printf '\e[6n';
read -d R _CURPOS;
stty echo;
_CURPOS=${_CURPOS#??};
_CURX=${_CURPOS#*;};
_CURY=${_CURPOS%;*}
}


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Stephane CHAZELAS

2007-10-24, 4:32 am

2007-10-24, 03:37(-04), Chris F.A. Johnson:
> On 2007-10-24, Markus Mayer wrote:
>
> I use this shell function:
>
> _curpos()
> {
> stty -echo;
> printf '\e[6n';
> read -d R _CURPOS;
> stty echo;
> _CURPOS=${_CURPOS#??};
> _CURX=${_CURPOS#*;};
> _CURY=${_CURPOS%;*}
> }

[...]

Note that read -d is zsh/bash/ksh93 specific (and you need
recent versions). With ksh93 (at least the ksh93s+ I have access
to), it doesn't work, it does seem to disable the cooked mode like
the other shells, but you still need to enter a newline
character (via <Ctrl-J>!), for read to return.

Portably, you can use dd, either by reading one character at a
time until you find "R" or using the terminal timeout as in my
suggestion.

Also, the local echo might already be disabled before the call
to _curpos, so you may want to not do a stty echo afterwards but
instead to save the settings and restore them afterwards.

In zsh:

STTY=-echo read -dR $'_CURPOS?\e[6n'


--
Stéphane
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com