/[pxelator]/lib/PXElator/progress_bar.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/progress_bar.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 115 - (hide annotations)
Sun Aug 2 12:09:02 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 666 byte(s)
implement common (and correct) progress_bar for tftpd and httpd

1 dpavlin 115 package progress_bar;
2    
3     use warnings;
4     use strict;
5    
6     use Time::HiRes qw/time/;
7     use Data::Dump qw/dump/;
8    
9     our $start_t;
10     our $last_t;
11    
12     sub start {
13     $start_t = $last_t = time();
14     }
15    
16     sub tick {
17     die "arguments wrong: ",dump( @_ ) unless $#_ == 2;
18     my ($file, $pos, $size) = @_;
19    
20     return unless $size;
21    
22     my $t = time();
23     return unless $start_t == $last_t
24     || $pos == $size
25     || $t - $last_t > 0.75 # s
26     ;
27    
28     $last_t = $t;
29     $t -= $start_t;
30     return unless $t;
31    
32     my $speed = $pos / $t;
33     return unless $speed;
34    
35     printf STDERR "%s %d/%d %.2f%% %.2f K/s ETA %.1fs \r"
36     , $file, $pos, $size
37     , $pos * 100 / $size
38     , $speed / 1024
39     , ( $size - $pos ) / $speed
40     ;
41     }
42    
43     1;

  ViewVC Help
Powered by ViewVC 1.1.26