views:

987

answers:

5

Hi all,

I am creating a small console app that needs a progress bar. Something like...

Conversion: 175/348 Seconds   |==========          |  50%

My question is, how do you erase characters already printed to the console? When I reach the 51st percentage, I have to erase this line from the console and insert a new line. In my current solution, this is what happens...

Conversion: 175/348 Seconds   |==========          |  50%
Conversion: 179/348 Seconds   |==========          |  52%
Conversion: 183/348 Seconds   |==========          |  54%
Conversion: 187/348 Seconds   |===========         |  56%

Code I use is...

print "Conversion: $converted_seconds/$total_time Seconds   $progress_bar  $converted_percentage%\n";

I am doing this in Linux using PHP(only I will use the app - so please excuse the language choice). So, the solution should work on the Linux platform - but if you have a solution that's cross platform, that would be preferable.

+1  A: 

to erase a previously printed character, I print a backspace after it: print "a" print "\b"

will print nothing (actually it will print and then a backspace, but you probably won't notice it)

+3  A: 

I'm not sure if it's the same in Linux but in Windows console apps you can print \r and the cursor will return to the first left position of the line allowing you to overwrite all the characters to the right.

You can use \b to move back a single character but since you're going to be updating your progress bar \r would be simpler to use than printing \b x number of times.

Spencer Ruport
+4  A: 

I don't think you need to apologize for the language choice. PHP is a great language for console applications.

Try this out:

<?php
for( $i=0;$i<10;$i++){
  print "$i \r";
  sleep(1);
}
?>

The "\r" will over-right the line with the new text. To make a new line you can just use "\n", but I'm guessing you already knew that.

Hope this helps! I know this works in Linux, but I don't know if it works in Windows or other operating systems.

GnomeCubed
Have you ever run a PHP console app through valigrind? Its not pretty ...
Tim Post
Also, many dumb terminals (or terminal emulators, or brain dead frame buffer modes) will be confused by this, as will various SSH clients. A literal backspace '\b' is the safest way to go.
Tim Post
I totally forget you could do this on the shell.
David
+2  A: 

\r did the trick.

For future reference, \b does not work in PHP in Linux. I was curious - so I did a couple of experiments in other languages as well(I did this in Linux - I don't know if the result will be the same in Windows/Mac)..

\b Works in...

  • Perl
  • Ruby
  • Tcl - with code puts -nonewline "Hello\b"

\b Doesn't work in

  • PHP - the code print "Hello\b"; prints out Hello\b
  • Python - code print "Hello\b" prints out Hello<new line> . Same result with print "Hello\b",
Binny V A
A: 

To erase a previously printed character, you can either, if already on the correct line, use the return character(13) (or \r) to return to the start of the line, then print spaces over, or use \b ( Character number 8, or 7 can't remember) then, print a space over the character.

If you are not already on the same line use:

function movecursor($line, $column){ echo "\033[{$line};{$column}H"; }

It will place the cursor on the correct line. You will need to return it to the line it is suipposed to be on after the deletion.

\b does not work with php, as \b is not an accepted character after the escape. You must use chr(8); (might be 7... one of them is the bell character, one is backspace)

nallar