Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I erase printed characters in a console application(Linux)?

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.

like image 504
Binny V A Avatar asked Jan 10 '09 07:01

Binny V A


People also ask

How do I delete a character in Linux terminal?

Using the 'delete' key code to do a backspace is weird; you'd need to use backspace '\b' under most systems. You also need to fflush(stdout) to get the abc string onto the screen before the write() overwrites part of it. Or use write() to print the abc string too. I freed buffer and used write instead if printf.

How do you delete old lines in C++?

At the end of the line, use "\r" instead of "\n"; the next line you std::cout will write over the old one. Keep in mind it will only overwrite the part that is printed; you'll need spaces at the end to erase the whole line.


2 Answers

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";
  }
like image 26
Luna Avatar answered Oct 04 '22 19:10

Luna


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.

like image 81
GnomeCubed Avatar answered Oct 04 '22 21:10

GnomeCubed