Terminal output with escape sequences: Any way to get the flags ata given position? - Unix

This is a discussion on Terminal output with escape sequences: Any way to get the flags ata given position? - Unix ; 2007-10-23, 16:23(+02), Markus Mayer: [...] > 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 ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: Terminal output with escape sequences: Any way to get the flags ata given position?

  1. Re: Terminal output with escape sequences: Any way to get the flags at a given position?

    2007-10-23, 16:23(+02), Markus Mayer:
    [...]
    > 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?

    [...]

    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

  2. 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

  3. 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:
    > 2007-10-23, 16:23(+02), Markus Mayer:
    > [...]
    >> 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?

    > [...]
    > [...]
    > 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.
    >


    Nah, I'm not going to dig /that/ deep.

    Okay - thanks very much for the quick response!
    best regards,
    Markus

  4. 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

  5. Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])

    2007-10-24, 07:17(+02), Markus Mayer:
    > By the way: Is there any function that allows me to retrieve the curremt
    > cursor coordinates?

    [...]

    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

  6. Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])

    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 |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  7. Re: Get current screen location (was: Re: Terminal output with escape sequences: Any [...])

    2007-10-24, 03:37(-04), Chris F.A. Johnson:
    > 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%;*}
    > }

    [...]

    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 !), 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

  8. Re: Get current screen location

    Am 24.10.2007 09:52 postulierte Stephane CHAZELAS folgendes:
    > 2007-10-24, 03:37(-04), Chris F.A. Johnson:
    >> 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%;*}
    >> }

    > [...]
    >
    > 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 !), 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'
    >
    >


    How would I do this programmatically? (I have to say that I'm absolutely
    new to *nix programming and shell usage)

    Markus

  9. Re: Get current screen location

    2007-10-24, 16:23(+02), Markus Mayer:
    [...]
    > How would I do this programmatically? (I have to say that I'm absolutely
    > new to *nix programming and shell usage)

    [...]

    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

  10. Re: Get current screen location

    Stephane CHAZELAS wrote:
    > 2007-10-24, 07:17(+02), Markus Mayer:
    >> By the way: Is there any function that allows me to retrieve the curremt
    >> cursor coordinates?

    > [...]


    > On many terminals, you can send:


    > printf '\033[6n'


    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
    http://invisible-island.net
    ftp://invisible-island.net

  11. Re: Get current screen location

    Am 24.10.2007 18:01 postulierte Stephane CHAZELAS folgendes:
    > 2007-10-24, 16:23(+02), Markus Mayer:
    > [...]
    >> How would I do this programmatically? (I have to say that I'm absolutely
    >> new to *nix programming and shell usage)

    > [...]
    >
    > 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.
    >


    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

  12. Re: Get current screen location

    2007-10-25, 11:47(-00), Thomas Dickey:
    > Stephane CHAZELAS wrote:
    >> 2007-10-24, 07:17(+02), Markus Mayer:
    >>> By the way: Is there any function that allows me to retrieve the curremt
    >>> cursor coordinates?

    >> [...]

    >
    >> On many terminals, you can send:

    >
    >> printf '\033[6n'

    >
    > 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...


    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

  13. Re: Get current screen location

    Stephane CHAZELAS wrote:
    > 2007-10-25, 11:47(-00), Thomas Dickey:
    >> Stephane CHAZELAS wrote:
    >>> 2007-10-24, 07:17(+02), Markus Mayer:
    >>>> By the way: Is there any function that allows me to retrieve the curremt
    >>>> cursor coordinates?
    >>> [...]

    >>
    >>> On many terminals, you can send:

    >>
    >>> printf '\033[6n'

    >>
    >> 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...


    > Thanks Thomas,


    > So, how much can you rely on tput u7, then?


    It's in ncurses - but not in other implementations.

    > 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?


    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
    http://invisible-island.net
    ftp://invisible-island.net

+ Reply to Thread