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