File Coverage

blib/lib/Term/TablePrint/ProgressBar.pm
Criterion Covered Total %
statement 17 60 28.3
branch 0 18 0.0
condition 0 18 0.0
subroutine 6 10 60.0
pod 0 4 0.0
total 23 110 20.9


line stmt bran cond sub pod time code
1             package # hide from PAUSE
2             Term::TablePrint::ProgressBar;
3              
4 1     1   7 use strict;
  1         2  
  1         37  
5 1     1   5 use warnings;
  1         2  
  1         25  
6 1     1   10 use 5.10.0;
  1         4  
7              
8 1     1   6 use Term::Choose::Constants qw( WIDTH_CURSOR );
  1         2  
  1         65  
9 1     1   7 use Term::Choose::Screen qw( clear_screen );
  1         2  
  1         49  
10 1     1   5 use Term::Choose::Util qw( get_term_width );
  1         2  
  1         609  
11              
12              
13             sub new {
14 0     0 0   my ( $class, $self ) = @_;
15 0           bless $self, $class;
16 0           my $count_cells = $self->{data_row_count} * $self->{col_count};
17 0 0 0       if ( $self->{threshold} && $self->{threshold} < $count_cells ) {
18 0           print clear_screen;
19 0           print "\rComputing: ";
20 0 0         if ( $count_cells / $self->{threshold} > 50 ) {
21 0           $self->{merge_progress_bars} = 0;
22 0           $self->{total} = $self->{data_row_count};
23             }
24             else {
25 0           $self->{merge_progress_bars} = 1;
26 0           $self->{total} = $self->{data_row_count} * $self->{count_progress_bars};
27             }
28             }
29             else {
30 0           $self->{count_progress_bars} = 0;
31             }
32 0           return $self;
33             }
34              
35              
36             sub set_progress_bar {
37 0     0 0   my ( $self ) = @_;
38 0 0         if ( ! $self->{count_progress_bars} ) {
39 0           return;
40             }
41 0           my $term_w = get_term_width();
42 0 0 0       if ( $^O ne 'MSWin32' && $^O ne 'cygwin' ) {
43 0           $term_w += WIDTH_CURSOR;
44             }
45 0 0         if ( $self->{merge_progress_bars} ) {
46 0           $self->{fmt} = "\rComputing: %3d%% [%s]";
47             }
48             else {
49 0           $self->{fmt} = "\rComputing: (" . $self->{count_progress_bars} . ") %3d%% [%s]";
50             }
51 0 0         $self->{short_print} = $term_w < 25 ? 1 : 0;
52 0           $self->{bar_w} = $term_w - length( sprintf $self->{fmt}, 100, '' ) + 1; # +1: lenght("\r") == 1
53 0   0       $self->{step} = int( $self->{total} / $self->{bar_w} || 1 );
54 0           my $count;
55 0 0         if ( $self->{merge_progress_bars} ) {
56 0   0       $count = $self->{so_far} || 0;
57 0   0       $self->{next_update} ||= $self->{step};
58             }
59             else {
60 0           $count = 0;
61 0           $self->{next_update} = $self->{step};
62             }
63 0           return $count;
64             }
65              
66              
67             sub update_progress_bar {
68 0     0 0   my ( $self, $count ) = @_;
69 0   0       my $multi = int( $count / ( $self->{total} / $self->{bar_w} ) ) || 1;
70 0 0         if ( $self->{short_print} ) {
71 0           print "\r" . ( '=' x $multi ) . ( ' ' x $self->{bar_w} - $multi );
72             }
73             else {
74 0           printf $self->{fmt}, ( $count / $self->{total} * 100 ), ( '=' x $multi ) . ( ' ' x ( $self->{bar_w} - $multi ) );
75             }
76 0           $self->{next_update} = $self->{next_update} + $self->{step};
77             }
78              
79              
80             sub last_update_progress_bar {
81 0     0 0   my ( $self, $count ) = @_;
82 0 0 0       if ( $self->{count_progress_bars} && $self->{merge_progress_bars} ) {
83 0           $self->{so_far} = $count;
84             }
85             else {
86 0           $self->update_progress_bar( $self->{total} );
87             }
88 0           $self->{count_progress_bars}--;
89             }
90              
91              
92              
93              
94              
95              
96              
97              
98              
99             1;
100              
101             __END__