How to remove printable characters in a console application (Linux)?

I am creating a small console application that requires a progress bar. Something like...

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

My question is: how do you delete characters already printed on the console? When I reach the 51st percent, I need 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% 

The code I use is ...

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

I do this on Linux using PHP (I will only use the application - so please excuse me for choosing the language). Thus, the solution should work on the Linux platform, but if you have a solution that crosses the platform, that would be preferable.

+13
linux console character erase
Jan 10 '09 at 7:42
source share
6 answers

I donโ€™t think you need to apologize for choosing a language. PHP is a great language for console applications.

Try the following:

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

"\ r" will flip the line with the new text. To create a new line, you can simply use "\ n", but I assume you already knew that.

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

+17
Jan 10 '09 at 7:50
source share

To erase a previously printed character, you have three options:

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

  • echo "\r"; will return the cursor to the beginning of the current line. Now you can replace the string with new text.

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

  function movecursor($line, $column){ echo "\033[{$line};{$column}H"; } 
+9
May 08 '10 at 17:47
source share

\ r did the trick.

For future reference, \ b does not work in PHP on Linux. I was curious, so I did some experiments in other languages โ€‹โ€‹(I did this on Linux - I don't know if the result on Windows / Mac will be the same) ..

\ b Works in ...

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

\ b Doesn't work in

  • PHP - print "Hello\b"; code print "Hello\b"; prints Hello\b
  • Python - print "Hello\b" code print "Hello\b" prints Hello<new line> . Same result with print "Hello\b",
+8
Jan 10 '09 at 8:37
source share

I'm not sure what is the same in Linux, but in Windows console applications you can print \ r and the cursor will return to the first left position of the line, which allows you to rewrite all characters to the right.

You can use \ b to move one character, but since you are going to update the progress bar, it will be easier to use than printing \ bx times.

+4
Jan 10 '09 at 7:45
source share

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

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

I discovered this on the Internet some time ago, unfortunately, I do not remember where. Thus, all loans belong to the original author.

+2
Oct. 14
source share

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

won't print anything (actually it will print and then back, but you probably won't notice it)

0
Jan 10 '09 at 7:44
source share



All Articles