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

前端 未结 6 1600
慢半拍i
慢半拍i 2020-12-14 19:54

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

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

My

相关标签:
6条回答
  • 2020-12-14 20:21

    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.

    0 讨论(0)
  • 2020-12-14 20:26

    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)

    0 讨论(0)
  • 2020-12-14 20:28

    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.

    0 讨论(0)
  • 2020-12-14 20:34

    \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",
    0 讨论(0)
  • 2020-12-14 20:35

    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.

    0 讨论(0)
  • 2020-12-14 20:38

    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";
      }
    
    0 讨论(0)
提交回复
热议问题