4

In a terminal, if I'm outputting a one-line progress indicator of some sort, in-place, \r would do the trick:

while (1) { echo "progress indication\r"; }

However, I have a progress indicator that really should be multi-line. As \r only returns to the start of the current line, I want something that can move up a couple of lines. Is there a control character/function that allows me to step back lines in the terminal?

Edit: in case I wasn't completely clear, I wish to have something roughly the opposite of \v, the vertical tab, which moves the terminal cursor down a line.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210

1 Answers1

2

There is no control character to go back onto the previous line, but depending on the TERM= type a ANSI escape might do the trick.

echo -e "\033[2A"

Here's a list that might be more helpful: http://en.wikipedia.org/wiki/ANSI_escape_code and for usage in the shell http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-6.html

mario
  • 144,265
  • 20
  • 237
  • 291
  • 1
    Initially, I thought your answer was wrong because the output started sliding up rapidly. Thanks to your links however, I realised that `2` wasn't part of the code for 'move up one line', but rather, meant, 'move up two lines'. So I was really moving up twice as much as I wanted. `\033[5A` worked perfectly for me, thanks a lot. By the way, is there a safety function in PHP to test for ANSI escape code support in the terminal (to avoid printing escape codes on unsupported terminals)? – Delan Azabani Dec 31 '10 at 14:41
  • 1
    @DelanAzabani: No, it cannot be determined safely. You can look at `$_ENV['TERM']` to guesstimate if it's supported. But ANSI escape sequences have the most universal support, should even work on Windows. On Linux it works in all xterm variants and the console. As alternative you could look into the PHP ncurses extensions however. – mario Dec 31 '10 at 14:48
  • Thanks for that; your advice is greatly helpful. – Delan Azabani Dec 31 '10 at 14:53