Nicodemus has asked for the wisdom of the Perl Monks concerning the following question:

I know that printing a \r will put the cursor at the beginning of the line, from which I can then type over the existing text. However, I want to clear that text first so that there isn't any stray text left at the end. I could output a bunch of spaces, but in this case I don't know how long the previous line was, nor how wide the terminal is. I have seen a 'ce' type in termcap, but I would rather avoid using termcap as it is no longer used on newer systems. Thanks, Nicodemus

Replies are listed 'Best First'.
Re: How do you clear the current line?
by mr.nick (Chaplain) on May 14, 2001 at 22:22 UTC
    print "^[[K";
    (that's "ESC[K") will clear from the current curson position until end of line.
    Update: Yes, tye, directly cut and pasting the above will not work (hence the added description of "that's 'ESC[K'") (it was embedded as an ESCAPE using my text editor). To get an ESCAPE in Perl, you'll need to use "\e".
      print "^[[K"; gives me: ^[[K perhaps you meant:
      print "\c[[K"; # or print "\e[K";
      but be aware that those will only work for ANSI-ish terminals (which are pretty common, though).

              - tye (but my friends call me "Tye")
      That's exactly what I'm looking for. I guess that explains all the weird ^[[D type output when pressing the cursor keys without anything tied to stdin.

      I have a couple questions though... how compatible is this escape sequence? I am targeting as many POSIX compatible machines as possible. I would use Term::Cap, but I know that many systems are phasing out termcap completely, including Debian GNU/Linux and AIX. Obviously it would be better to find out the exact character that the term expects based on it's TERM type, but termcap is the only mechanism I know for doing as such.

      Also, is there a full list of all the possible escape characters like this one?

      Thanks,
      Nicodemus

        That's a fairly basic ANSI sequence: existing long before Termcap or Termlib (way back to pre-1980 machines).

        I used this for the lookup of the sequence you were looking for. Normally, I just consult a very old Z80A programming manual I have lying around someplace.

Re: How do you clear the current line?
by Masem (Monsignor) on May 14, 2001 at 22:13 UTC
    Have you looked into using Term::Readline? This is a platform independent way of playing with input text.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Actually I'm already using Term::ReadLine::Gnu. My whole app is based around it actually. However, my problem is with output, not input.

      Thanks,
      Nicodemus