Re: Terminal output with escape sequences: Any way to get the flags at a given position?
2007-10-23, 16:23(+02), Markus Mayer:
[...][color=blue]
> I use sequences like \033[4m (underscore) to pimp up my program's
> output. However I am wondering if there is any method to actually to
> actually GET the flags set for a given character on the terminal. In
> other words: If I know the character's position (i.e. the location the
> cursor is), how do I determine whether the character is underscored or not?[/color]
[...]
In short you can't.
IF you want to do that, you'll need to do as the ncurses or
slang libraries do, that is maintain an internal representation
of the content of the screen.
There's no standard terminal API to retrieve what's at a
specific position on the screen, though some terminals may
provide with one.
GNU screen stores the content of the screen internally, so you
might be able to query it, and if not, to patch it to add that
functionality if you're really desperate.
--
Stéphane
Terminal output with escape sequences: Any way to get the flags ata given position?
Hi.
I use sequences like \033[4m (underscore) to pimp up my program's
output. However I am wondering if there is any method to actually to
actually GET the flags set for a given character on the terminal. In
other words: If I know the character's position (i.e. the location the
cursor is), how do I determine whether the character is underscored or not?
Best regards,
Markus
Re: Terminal output with escape sequences: Any way to get the flagsat a given position?
Am 23.10.2007 16:22 postulierte Stephane CHAZELAS folgendes:[color=blue]
> 2007-10-23, 16:23(+02), Markus Mayer:
> [...][color=green]
>> I use sequences like \033[4m (underscore) to pimp up my program's
>> output. However I am wondering if there is any method to actually to
>> actually GET the flags set for a given character on the terminal. In
>> other words: If I know the character's position (i.e. the location the
>> cursor is), how do I determine whether the character is underscored or not?[/color]
> [...]
> [...]
> GNU screen stores the content of the screen internally, so you
> might be able to query it, and if not, to patch it to add that
> functionality if you're really desperate.
>[/color]
Nah, I'm not going to dig /that/ deep. :)
Okay - thanks very much for the quick response!
best regards,
Markus
Get current screen location (was: Re: Terminal output with escape
By the way: Is there any function that allows me to retrieve the curremt
cursor coordinates?
Regdars,
Markus
Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])
2007-10-24, 07:17(+02), Markus Mayer:[color=blue]
> By the way: Is there any function that allows me to retrieve the curremt
> cursor coordinates?[/color]
[...]
On many terminals, you can send:
printf '\033[6n'
or
tput u7
Then, the terminal will reply back with:
^[[y;xR
That you can read like that:
old_settings=$(stty -g)
stty -icanon min 1 time 2 -echo
printf '\033[6n'
IFS='[;R'
set -f
set -- $(dd count=1 2> /dev/null)
stty "$old_settings"
echo "x: $3, y: $2"
--
Stéphane
Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])
On 2007-10-24, Markus Mayer wrote:[color=blue]
>
>
> By the way: Is there any function that allows me to retrieve the curremt
> cursor coordinates?[/color]
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
Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])
2007-10-24, 03:37(-04), Chris F.A. Johnson:[color=blue]
> On 2007-10-24, Markus Mayer wrote:[color=green]
>>
>>
>> By the way: Is there any function that allows me to retrieve the curremt
>> cursor coordinates?[/color]
>
> I use this shell function:
>
> _curpos()
> {
> stty -echo;
> printf '\e[6n';
> read -d R _CURPOS;
> stty echo;
> _CURPOS=${_CURPOS#??};
> _CURX=${_CURPOS#*;};
> _CURY=${_CURPOS%;*}
> }[/color]
[...]
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
Re: Get current screen location
Am 24.10.2007 09:52 postulierte Stephane CHAZELAS folgendes:[color=blue]
> 2007-10-24, 03:37(-04), Chris F.A. Johnson:[color=green]
>> On 2007-10-24, Markus Mayer wrote:[color=darkred]
>>>
>>> By the way: Is there any function that allows me to retrieve the curremt
>>> cursor coordinates?[/color]
>> I use this shell function:
>>
>> _curpos()
>> {
>> stty -echo;
>> printf '\e[6n';
>> read -d R _CURPOS;
>> stty echo;
>> _CURPOS=${_CURPOS#??};
>> _CURX=${_CURPOS#*;};
>> _CURY=${_CURPOS%;*}
>> }[/color]
> [...]
>
> 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'
>
>[/color]
How would I do this programmatically? (I have to say that I'm absolutely
new to *nix programming and shell usage)
Markus
Re: Get current screen location
2007-10-24, 16:23(+02), Markus Mayer:
[...][color=blue]
> How would I do this programmatically? (I have to say that I'm absolutely
> new to *nix programming and shell usage)[/color]
[...]
tcgetattr(3) to fetch the previous terminal attributes.
tcsetattr(3) to clear the ICANON, ISIG and ECHO bits and set the VMIN
and VTIME and to restore the previous settings afterwards.
write(1, "\033[6n", 4); to send the escape sequence.
read(0, &c, 1); to read characters one at a time until you find
'R'
sscanf to extract the coordinates from \e[y;xR string.
But it looks like what you really want is to use ncurses that
provides with all the functions to create visual applications on
a terminal.
--
Stéphane
Re: Get current screen location
Stephane CHAZELAS <this.address@is.invalid> wrote:[color=blue]
> 2007-10-24, 07:17(+02), Markus Mayer:[color=green]
>> By the way: Is there any function that allows me to retrieve the curremt
>> cursor coordinates?[/color]
> [...][/color]
[color=blue]
> On many terminals, you can send:[/color]
[color=blue]
> printf '\033[6n'[/color]
many, but not all (this is a vt100 device status report, which iirc
is not in ISO-6429(*)). For instance, the console emulator for Sun's doesn't
have any comparable feature.
(*) noted for the benefit of the auto-responder who will insist that
hardcoding everything is Good Practice...
--
Thomas E. Dickey
[url]http://invisible-island.net[/url]
[url]ftp://invisible-island.net[/url]
Re: Get current screen location
Am 24.10.2007 18:01 postulierte Stephane CHAZELAS folgendes:[color=blue]
> 2007-10-24, 16:23(+02), Markus Mayer:
> [...][color=green]
>> How would I do this programmatically? (I have to say that I'm absolutely
>> new to *nix programming and shell usage)[/color]
> [...]
>
> tcgetattr(3) to fetch the previous terminal attributes.
> tcsetattr(3) to clear the ICANON, ISIG and ECHO bits and set the VMIN
> and VTIME and to restore the previous settings afterwards.
> write(1, "\033[6n", 4); to send the escape sequence.
> read(0, &c, 1); to read characters one at a time until you find
> 'R'
> sscanf to extract the coordinates from \e[y;xR string.
>
> But it looks like what you really want is to use ncurses that
> provides with all the functions to createt visual applications on
> a terminal.
>[/color]
Already heard about ncurses, unfortunately I'm currently not allowed to
use it. It's also more interesting to do it manually. :)
Thank you very much!
Markus
Re: Get current screen location
2007-10-25, 11:47(-00), Thomas Dickey:[color=blue]
> Stephane CHAZELAS <this.address@is.invalid> wrote:[color=green]
>> 2007-10-24, 07:17(+02), Markus Mayer:[color=darkred]
>>> By the way: Is there any function that allows me to retrieve the curremt
>>> cursor coordinates?[/color]
>> [...][/color]
>[color=green]
>> On many terminals, you can send:[/color]
>[color=green]
>> printf '\033[6n'[/color]
>
> many, but not all (this is a vt100 device status report, which iirc
> is not in ISO-6429(*)). For instance, the console emulator for Sun's doesn't
> have any comparable feature.
>
> (*) noted for the benefit of the auto-responder who will insist that
> hardcoding everything is Good Practice...[/color]
Thanks Thomas,
So, how much can you rely on tput u7, then?
I can see u7 is often set to \e[6n, but terminfo(5) manpage,
only describes u7 as "user string #7". Is there any sort of
standard that says that u7 should contain the "request cursor
position" escape sequence when available?
--
Stéphane
Re: Get current screen location
Stephane CHAZELAS <this.address@is.invalid> wrote:[color=blue]
> 2007-10-25, 11:47(-00), Thomas Dickey:[color=green]
>> Stephane CHAZELAS <this.address@is.invalid> wrote:[color=darkred]
>>> 2007-10-24, 07:17(+02), Markus Mayer:
>>>> By the way: Is there any function that allows me to retrieve the curremt
>>>> cursor coordinates?
>>> [...][/color]
>>[color=darkred]
>>> On many terminals, you can send:[/color]
>>[color=darkred]
>>> printf '\033[6n'[/color]
>>
>> many, but not all (this is a vt100 device status report, which iirc
>> is not in ISO-6429(*)). For instance, the console emulator for Sun's doesn't
>> have any comparable feature.
>>
>> (*) noted for the benefit of the auto-responder who will insist that
>> hardcoding everything is Good Practice...[/color][/color]
[color=blue]
> Thanks Thomas,[/color]
[color=blue]
> So, how much can you rely on tput u7, then?[/color]
It's in ncurses - but not in other implementations.
[color=blue]
> I can see u7 is often set to \e[6n, but terminfo(5) manpage,
> only describes u7 as "user string #7". Is there any sort of
> standard that says that u7 should contain the "request cursor
> position" escape sequence when available?[/color]
There is no _standard_, but the comments in ncurses' terminfo.src note
this use. The tack program (which I understand to be the origin of
this) assumes these strings are set up, and will use them. Since the
example is from vt100, then most of the currently-used terminals should
support this.
Some other applications may use them, but I do not have a list.
--
Thomas E. Dickey
[url]http://invisible-island.net[/url]
[url]ftp://invisible-island.net[/url]