| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Text::SimpleTable::AutoWidth; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | $Text::SimpleTable::AutoWidth::VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Text::SimpleTable::AutoWidth - Simple eyecandy ASCII tables with auto-width selection | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | has 'fixed_width' => ( is => 'rw', default => 0 ); # isa => 'Int' | |||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | has 'max_width' => ( is => 'rw', default => 0 ); # isa => 'Int' | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | has 'captions' => ( is => 'rw' ); # isa => 'ArrayRef[Str]' | |||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | has 'rows' => ( is => 'rw' ); # isa => 'ArrayRef[ArrayRef[Str]]' | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | our $WIDTH_LIMIT = 200; # default maximum width | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | sub row { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
row: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | my ( $self, @texts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | if ( $self->rows ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | push( @{ $self->rows }, [@texts] ); | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | $self->rows( [ [@texts] ] ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use List::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use Text::SimpleTable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub draw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw: called CC=22 SCAR=31.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # count of columns will be same as count of captions, or same | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # as count of columns in first row, if there is no captions | ||||||||||||||||||||||||||||||||||||||||||
| 42 | my $columns = | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | ( $self->captions && @{ $self->captions } ) | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->rows and @{$self->rows->[0];}
MC/DC0%0/2
$self->rows and @{$self->rows->[0];}
$self->rows
@{$self->rows->[0];}
Truth table33%1/3
A and B
A= $self->rowsB= @{$self->rows->[0];}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | || ( $self->rows && @{ $self->rows->[0] } ) | ||||||||||||||||||||||||||||||||||||||||
|
Condition62%5/8
$self->captions && @{$self->captions;} || $self->rows && @{$self->rows->[0];} || 0
$self->captions and @{$self->captions;} or $self->rows and @{$self->rows->[0];}
$self->captions and @{$self->captions;}
MC/DC0%0/3
$self->captions && @{$self->captions;} || $self->rows && @{$self->rows->[0];} || 0
$self->captions
@{$self->captions;}
$self->rows and @{$self->rows->[0];}
Truth table40%2/5
A and B or C
A= $self->captionsB= @{$self->captions;}C= $self->rows and @{$self->rows->[0];}
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | || 0; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | return unless $columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # table will not be wider than limits | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | my $limit = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | $self->max_width ? $self->max_width | ||||||||||||||||||||||||||||||||||||||||||
| 52 | : $self->fixed_width ? $self->fixed_width | ||||||||||||||||||||||||||||||||||||||||||
| 53 | : $WIDTH_LIMIT; | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # by default, each column should have at least 2 symbols: | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # one informative and one for '-' (if we'll need to wrap) | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | my @max_width = (2) x $columns; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # calculate max width of each column | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | for my $row ( ( $self->captions ? $self->captions : () ), @{ $self->rows } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | my @row_width = map { length } @$row; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | ▶ | $#row_width = $columns - 1 if $#row_width >= $columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # find new width | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # we will do this in two passes | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | my @new_width = @max_width; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # first pass: | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # find new width for all columns, that we can | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # make wider without need to wrap anything | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | for my $idx ( 0 .. $#row_width ) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 14 | ▶ | if ( $max_width[$idx] < $row_width[$idx] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 12 | $new_width[$idx] = $row_width[$idx]; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # check for limits | ||||||||||||||||||||||||||||||||||||||||||
| 76 | my $total = $columns + 1 # for each '|' | ||||||||||||||||||||||||||||||||||||||||||
| 77 | + $columns * 2 # for spaces around each value | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 12 | ▶ | + List::Util::reduce { $a + $b } @new_width; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # restore old value, if new value will lead to wrap | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 12 | ▶ | $new_width[$idx] = $max_width[$idx] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | if $total > $limit; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # second pass: | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # find new width for all columns, that we can | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # make wider and need to wrap something | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | for my $idx ( 0 .. $#row_width ) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 14 | ▶ | if ( $new_width[$idx] < $row_width[$idx] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | my $total = $columns + 1 # for each '|' | ||||||||||||||||||||||||||||||||||||||||||
| 92 | + $columns * 2 # for spaces around each value | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | + List::Util::reduce { $a + $b } @new_width; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | $new_width[$idx] += $limit - $total; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # save new result | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 7 | @max_width = @new_width; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # check for limits | ||||||||||||||||||||||||||||||||||||||||||
| 104 | my $total = $columns + 1 # for each '|' | ||||||||||||||||||||||||||||||||||||||||||
| 105 | + $columns * 2 # for spaces around each value | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | + List::Util::reduce { $a + $b } @max_width; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 7 | ▶ | last if $total >= $limit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | # check for fixed_width | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | if ( $self->fixed_width ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | my $total = $columns + 1 # for each '|' | ||||||||||||||||||||||||||||||||||||||||||
| 114 | + $columns * 2 # for spaces around each value | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | + List::Util::reduce { $a + $b } @max_width; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | $max_width[-1] += $self->fixed_width - $total | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | unless $total == $self->fixed_width; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # prepare drawer | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | my @params = @max_width; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | if ( $self->captions ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | my $idx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | for (@params) { | |||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | $_ = [ $_, $self->captions->[ $idx++ ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | my $tab = Text::SimpleTable->new(@params); | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # put rows into drawer | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | $tab->row(@$_) for @{ $self->rows }; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 4 | return $tab->draw(); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | 1; # End of Text::SimpleTable::AutoWidth | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||||||||||||