Choose/LineFold/PP.pm

filter: Statementstmt 85.0% 57 / 67 Branchbran 73.3% 22 / 30 Conditioncond 66.6% 8 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 74.6% 97 / 130 CC 23 SCAR 18.0
CC
23
Cov
80%
cut_to_printwidth
8.5
BEGIN
8.3
char_width
6.1
CRAP 6.0
« Choose/ValidateOptions.pm ↑ Summary Choose/LineFold/PP/CharWidthAmbiguousWide.pm »
1 package Term::Choose::LineFold::PP;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '1.783';
8  
9 5 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our @EXPORT_OK = qw( char_width print_columns cut_to_printwidth adjust_to_printwidth );
12  
13 5 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=21.2
14 5 ▶ if ( exists $ENV{TC_AMBIGUOUS_WIDTH_IS_WIDE} ) { # 24.03.2025
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'TC_AMBIGUOUS_WIDTH_IS_WIDE'}) { } 0 5 5
15 0 ▶ if ( $ENV{TC_AMBIGUOUS_WIDTH_IS_WIDE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'TC_AMBIGUOUS_WIDTH_IS_WIDE'}) { } 0 0 0
16 0 require Term::Choose::LineFold::PP::CharWidthAmbiguousWide;
17 0 Term::Choose::LineFold::PP::CharWidthAmbiguousWide->import( 'table_char_width' );
18 }
19 else {
20 0 require Term::Choose::LineFold::PP::CharWidthDefault;
21 0 Term::Choose::LineFold::PP::CharWidthDefault->import( 'table_char_width' );
22 }
23 } #
24 else { #
25 5 ▶ if ( $ENV{TC_AMBIGUOUS_WIDE} ) { #
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'TC_AMBIGUOUS_WIDE'}) { } 0 5 5
26 0 require Term::Choose::LineFold::PP::CharWidthAmbiguousWide; #
27 0 Term::Choose::LineFold::PP::CharWidthAmbiguousWide->import( 'table_char_width' ); #
28 } #
29 else { #
30 5 require Term::Choose::LineFold::PP::CharWidthDefault; #
31 5 Term::Choose::LineFold::PP::CharWidthDefault->import( 'table_char_width' ); #
32 } #
33 } #
34 }
35  
36  
37 5 my $table = table_char_width();
38  
39 5 my $cache = {};
40  
41  
42 80 ▶sub char_width {
Subroutine char_width: called CC=6 SCAR=18.0
Pod undocumented
43 #my $c = $_[0];
44 80 my $min = 0;
45 80 my $mid;
46 80 my $max = $#$table;
47 80 ▶ if ( $_[0] < $table->[0][0] || $_[0] > $table->[$max][1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] < $table->[0][0] or $_[0] > $table->[$max][1]) 30 50 80
Condition66%2/3
$_[0] < $table->[0][0] or $_[0] > $table->[$max][1]
l!l&&r!l&&!r
30050
MC/DC50%1/2
$_[0] < $table->[0][0] or $_[0] > $table->[$max][1]
$_[0] < $table->[0][0] $_[0] > $table->[$max][1]
Truth table66%2/3
A or B
A= $_[0] < $table->[0][0]B= $_[0] > $table->[$max][1]
ABresult
1X1
011
000
48 30 return 1;
49 }
50 50 ▶ while ( $max >= $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max >= $min) 428 0 428
51 428 $mid = int( ( $min + $max ) / 2 );
52 428 ▶ if ( $_[0] > $table->[$mid][1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] > $table->[$mid][1]) { } 208 220 428
elsif ($_[0] < $table->[$mid][0]) { } 170 50 220
53 208 $min = $mid + 1;
54 }
55 elsif ( $_[0] < $table->[$mid][0] ) {
56 170 $max = $mid - 1;
57 }
58 else {
59 50 return $table->[$mid][2];
60 }
61 }
62 0 return 1;
63 }
64  
65  
66 12 ▶sub print_columns {
Subroutine print_columns: called CC=3 SCAR=11.0
Pod undocumented
67 #my $str = $_[0];
68 12 my $width = 0;
69 12 my $c;
70 12 for my $i ( 0 .. ( length( $_[0] ) - 1 ) ) {
71 68 $c = ord substr $_[0], $i, 1;
72 68 ▶ $width += ( $cache->{$c} //= char_width( $c ) );
Condition66%2/3
$cache->{$c} //= char_width($c)
l!l&&r!l&&!r
21470
MC/DC0%0/2
$cache->{$c} //= char_width($c)
$cache->{$c} char_width($c)
Truth table66%2/3
A //= B
A= $cache->{$c}B= char_width($c)
ABresult
1X1
011
000
73 }
74 12 return $width;
75 }
76  
77  
78 20 ▶sub cut_to_printwidth {
Subroutine cut_to_printwidth: called CC=8 SCAR=21.4
Pod undocumented
79 #my ( $str, $avail_width ) = @_;
80 20 my $str_w = 0;
81 20 my $c;
82 20 for my $i ( 0 .. ( length( $_[0] ) - 1 ) ) {
83 66 $c = ord substr $_[0], $i, 1;
84 66 ▶ if ( ( $str_w += ( $cache->{$c} //= char_width( $c ) ) ) > $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($str_w += ($cache->{$c} //= char_width($c))) > $_[1]) 20 46 66
Condition66%2/3
$cache->{$c} //= char_width($c)
l!l&&r!l&&!r
50160
MC/DC0%0/2
$cache->{$c} //= char_width($c)
$cache->{$c} char_width($c)
Truth table66%2/3
A //= B
A= $cache->{$c}B= char_width($c)
ABresult
1X1
011
000
85 20 ▶ if ( ( $str_w - $cache->{$c} ) < $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_w - $cache->{$c} < $_[1]) 6 14 20
86 6 ▶ return substr( $_[0], 0, $i ) . ' ', substr( $_[0], $i ) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 3 6
87 3 return substr( $_[0], 0, $i ) . ' ';
88 }
89 14 ▶ return substr( $_[0], 0, $i ), substr( $_[0], $i ) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 7 7 14
90 7 return substr( $_[0], 0, $i );
91 }
92 }
93 0 ▶ return $_[0], '' if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
94 0 return $_[0];
95 }
96  
97  
98 17 ▶sub adjust_to_printwidth {
Subroutine adjust_to_printwidth: called CC=6 SCAR=18.0
Pod undocumented
99 # my ( $str, $width ) = @_;
100 17 my $str_w = 0;
101 17 my $c;
102 17 for my $i ( 0 .. ( length( $_[0] ) - 1 ) ) {
103 63 $c = ord substr $_[0], $i, 1;
104 63 ▶ if ( ( $str_w += ( $cache->{$c} //= char_width( $c ) ) ) > $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($str_w += ($cache->{$c} //= char_width($c))) > $_[1]) 10 53 63
Condition66%2/3
$cache->{$c} //= char_width($c)
l!l&&r!l&&!r
5850
MC/DC0%0/2
$cache->{$c} //= char_width($c)
$cache->{$c} char_width($c)
Truth table66%2/3
A //= B
A= $cache->{$c}B= char_width($c)
ABresult
1X1
011
000
105 10 ▶ if ( ( $str_w - $cache->{$c} ) < $_[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_w - $cache->{$c} < $_[1]) 3 7 10
106 3 return substr( $_[0], 0, $i ) . ' ';
107 }
108 7 return substr( $_[0], 0, $i );
109 }
110 }
111 7 ▶ return $_[0] if $str_w == $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $str_w == $_[1] 0 7 7
112 7 return $_[0] . ' ' x ( $_[1] - $str_w );
113 }
114  
115  
116  
117 5 1;
118  
119 __END__
« Choose/ValidateOptions.pm ↑ Summary Choose/LineFold/PP/CharWidthAmbiguousWide.pm »