1 package CXGN
::Tools
::Terminal
::ETA
;
3 =head1 CXGN::Tools::Terminal::ETA
5 A nice little package for displaying the ETA for a process in a terminal
10 my $eta = CXGN::Tools::Terminal::ETA->new();
11 $eta->interval(1.2) #only allow updates 1.2 seconds after the last update
13 $eta->begin(); #sets start time
14 $eta->target(3000); #total # of iterations expected
19 $eta->update_and_print($i);
22 #Prints something like:
25 Don't print any newlines if you want the ETA to be displayed in-place!
32 my $self = bless {}, $class;
33 $self->{increment
} = 1;
34 $self->{begin
} = time();
35 $self->{last_update
} = time();
37 $self->{interval
} = 0;
43 $self->{begin
} = time();
49 $self->{target
} = $target if $target;
50 return $self->{target
};
56 $self->{interval
} = $interval if $interval;
57 return $self->{interval
};
63 if($self->{interval
} > 0){
64 $diff = time() - $self->{last_update
};
65 return if $diff < $self->{interval
};
67 $self->{current
} = $current;
68 $self->{elapsed
} = time() - $self->{begin
};
69 $self->{elapsed
} ||= 1; #round up to 1 second, at least
70 $self->{remaining
} = $self->{target
} - $self->{current
};
71 $self->{rate
} = ($self->{current
})/$self->{elapsed
};
72 $self->{eta
} = $self->{remaining
}/$self->{rate
};
73 $self->{last_update
} = time();
76 sub update_and_print
{
78 if($self->{interval
} > 0) {
79 $diff = time() - $self->{last_update
};
80 return if $diff < $self->{interval
};
82 $self->update( shift );
88 print ( " " x
80 . "\r" x
200 );
89 print "ETA: " . $self->format_secs($self->{eta
});
94 my $sec = int( shift );
98 $min = int($sec / 60);
99 $sec = $sec - $min*60;
101 $hour = int($min / 60);
102 $min = $min - $hour*60;
105 foreach($sec, $hour, $min){
109 return "$hour:$min:$sec";