Update command line output

前端 未结 5 1943
逝去的感伤
逝去的感伤 2021-02-05 21:33

My program (which happens to be in Perl, though I don\'t think this question is Perl-specific) outputs status messages at one point in the program of the form Progress: x/

5条回答
  •  攒了一身酷
    2021-02-05 22:19

    I had to tackle something similar to this today. If you don't mind reprinting the entire line, you could do something like this:

    print "\n";
    while (...) {
         print "\rProgress: $counter / $total";
         # do processing work here
         $counter++;
    }
    print "\n";
    

    The "\r" character is a carriage return-- it brings the cursor back to the beginning of the line. That way, anything you print out overwrites the previous progress notification's text.

提交回复
热议问题