Update command line output

前端 未结 5 1948
逝去的感伤
逝去的感伤 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:05

    Use autoflush with STDOUT:

    local $| = 1; # Or use IO::Handle; STDOUT->autoflush;
    
    print 'Progress: ';
    my $progressString;
    while ...
    {
      # remove prev progress
      print "\b" x length($progressString) if defined $progressString;
      # do lots of processing, update $counter
      $progressString = "$counter / $total"; # No more newline
      print $progressString; # Will print, because auto-flush is on
      # end of processing
    }
    print "\n"; # Don't forget the trailing newline
    

提交回复
热议问题