How do I erase printed characters in a console app

2020-02-08 16:18发布

问题:

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:

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 overwrite 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.



回答2:

To erase a previously printed character you have three options:

  • echo chr(8) . " "; echoes the back character, and will move the cursor back one place, and the space then overwrites the character. You can use chr(8) multiple times in a row to move back multiple characters.

  • echo "\r"; will return the cursor to the start of the current line. You can now replace the line with new text.

  • The third option is to set the line and column of the cursor position using ANSI escape codes, then print the replacement characters. It might not work with all terminals:

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


回答3:

\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",


回答4:

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.



回答5:

This seems to be pretty old topic but I will drop my 5 into.

for ($i; $i<_POSITION_; $i--) {
   echo "\010"; //issue backspace 
}

Found this on the internet some time ago, unfortunately don't remember where. So all credits goes to original author.



回答6:

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)