Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: finding visible length of string?

by alien_life_form (Pilgrim)
on Sep 19, 2002 at 07:18 UTC ( [id://199093]=note: print w/replies, xml ) Need Help??


in reply to finding visible length of string?

Greetings,

regardless of the proposed workarounds, the original formulation of the problem is interesting in its own right tho'...
How would one compute the visible length? One obvious way would be by finding the screen position of the cursor before and after printing - something which I hazily remember was possible under curses...
Other solutions?
Cheers,
alf


You can't have everything: where would you put it?

Replies are listed 'Best First'.
Re: Re: finding visible length of string?
by BrowserUk (Patriarch) on Sep 19, 2002 at 16:20 UTC

    As the original questioner was using ANSI/VT100 escape sequences, depending on how faithfully they have been implemented, one way to find out the visible length of a printed string would be to use the Query Cursor Position sequence <ESC>[6n before and then after printing the string. On the original VT100's this used to cause the terminal to generate a sequence that looked like <ESC>[{ROW};{COLUMN}R. This had to be captured and parsed by the program. Issue the sequence, read the reply, parse out COLUMN. Print string and the sequence again, read the reply, parse the new COLUMN and subtract.

    Of course, if the implementation is sufficiently faithful to allow that sequence, then its much easier to simple postion the cursor absolutely using the Set Cursor Position sequence <ESC>[{ROW};{COLUMN}f each time before printing the string. That has the limitation that if the text beng printed is part of a larger body of text, then setting the position absolutely and getting it in the right place is difficult as terminals vary in width and height.

    If the terminal emulation supports it, possibly the very best way to do this would be to use the Save <ESC>[s and Restore <ESC>[u Cursor position sequences as prefix/suffix to the string being printed, but my previous experience of trying these is that they are often not supported.

    If the full ANSI/VT100 escape sequence set is supported, there are many other ways of achieving the original aim as well.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      Note that not all terminal emulators provide this function. My favourite, rxvt, does not f.ex. Update: right, I should read closer..

      Makeshifts last the longest.

        Hence:

        depending on how faithfully they have been implemented

        if the implementation is sufficiently faithful

        If the terminal emulation supports it

        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://199093]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found