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!

In reply to Re: Re: finding visible length of string? by BrowserUk
in thread finding visible length of string? by ajr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.