File Coverage

blib/lib/Term/TablePrint/ProgressBar.pm
Criterion Covered Total %
statement 17 43 39.5
branch 0 6 0.0
condition 0 9 0.0
subroutine 6 9 66.6
pod 0 3 0.0
total 23 70 32.8


line stmt bran cond sub pod time code
1             package Term::TablePrint::ProgressBar;
2              
3 1     1   8 use strict;
  1         3  
  1         44  
4 1     1   6 use warnings;
  1         13  
  1         58  
5 1     1   15 use 5.16.0;
  1         4  
6              
7             our $VERSION = '0.176';
8              
9 1     1   7 use Term::Choose::Constants qw( EXTRA_W );
  1         2  
  1         89  
10 1     1   23 use Term::Choose::Screen qw( clear_screen clear_to_end_of_line );
  1         2  
  1         72  
11 1     1   7 use Term::Choose::Util qw( get_term_width );
  1         1  
  1         615  
12              
13              
14             sub new {
15 0     0 0   my ( $class, $self ) = @_;
16 0           bless $self, $class;
17 0   0       $self->{show_progress_bar} //= 1;
18 0           return $self;
19             }
20              
21             sub set_progress_bar {
22 0     0 0   my ( $self ) = @_;
23 0           my $term_w = get_term_width() + EXTRA_W;
24 0           $self->{fmt} = "\rComputing: %3d%% [%s]";
25 0 0         if ( $term_w < 25 ) {
26 0           $self->{short_print} = 1;
27 0           $self->{bar_w} = $term_w
28             }
29             else {
30 0           $self->{short_print} = 0;
31 0           $self->{bar_w} = $term_w - length( sprintf $self->{fmt}, 100, '' ) + 1; # +1: lenght("\r") == 1
32             }
33 0   0       $self->{step} = int( $self->{total} / $self->{bar_w} || 1 );
34 0   0       $self->{count} //= 0;
35 0   0       $self->{next_update} ||= $self->{step};
36 0 0         if ( ! $self->{count} ) {
37 0           print clear_screen;
38 0           print "\rComputing: ";
39             }
40 0           return;
41             }
42              
43              
44             sub update_progress_bar {
45 0     0 0   my ( $self ) = @_;
46 0           my $multi = int( $self->{count} / ( $self->{total} / $self->{bar_w} ) );
47 0 0         if ( $self->{short_print} ) {
48 0           print "\r", clear_to_end_of_line;
49 0           print( ( '=' x $multi ) . ( ' ' x ( $self->{bar_w} - $multi ) ) );
50             }
51             else {
52 0           printf $self->{fmt}, ( $self->{count} / $self->{total} * 100 ), ( '=' x $multi ) . ( ' ' x ( $self->{bar_w} - $multi ) );
53             }
54 0           $self->{next_update} += $self->{step};
55             }
56              
57              
58              
59              
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             Term::TablePrint::ProgressBar - Show a progress bar.
68              
69             =head1 VERSION
70              
71             Version 0.176
72              
73             =cut
74              
75             =head1 DESCRIPTION
76              
77             Provides the progress bar used in C.
78              
79             =head1 AUTHOR
80              
81             Matthäus Kiem
82              
83             =head1 LICENSE AND COPYRIGHT
84              
85             Copyright 2013-2026 Matthäus Kiem.
86              
87             This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For
88             details, see the full text of the licenses in the file LICENSE.
89              
90             =cut
91              
92              
93              
94             1;
95              
96             __END__