Choose/LineFold.pm

filter: Statementstmt 29.1% 35 / 120 Branchbran 2.2% 1 / 44 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 23.1% 46 / 199 CC 34 SCAR 66.7
CC
34
Cov
20%
line_fold
1056.0
BEGIN
3.1
BEGIN
1.0
CRAP 786.3
« Choose/Linux.pm ↑ Summary Choose/Win32.pm »
1 package Term::Choose::LineFold;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.783';
8  
9 3 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our @EXPORT_OK = qw( char_width print_columns cut_to_printwidth adjust_to_printwidth line_fold );
12  
13 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Term::Choose::Constants qw( PH SGR_ES EXTRA_W );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Term::Choose::Screen qw( get_term_size );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.4
19 3 my $module;
20 eval {
21 3 require Term::Choose::LineFold::XS;
22 0 Term::Choose::LineFold::XS->VERSION( 0.001 );
23 0 $module = 'Term::Choose::LineFold::XS';
24 0 1;
25 3 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::Choose::LineFold::XS; "Term::Choose::LineFold::XS"->VERSION("0.001"); $module = "Term::Choose::LineFold::XS"; 1 } } or do { require Term::Choose::LineFold::PP; $module = "Term::Choose::LineFold::PP" } 0 3 3
26 3 require Term::Choose::LineFold::PP;
27 3 $module = 'Term::Choose::LineFold::PP';
28 };
29 3 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 for my $func ( qw( char_width print_columns cut_to_printwidth adjust_to_printwidth ) ) {
31 12 *{"Term::Choose::LineFold::$func"} = \&{"${module}::$func"};
32 }
33 }
34 #BEGIN {
35 # my $module;
36 # eval {
37 # require Term::Choose::LineFold::XS;
38 # Term::Choose::LineFold::XS->VERSION( 0.001 );
39 # $module = 'Term::Choose::LineFold::XS';
40 # 1;
41 # } or do {
42 # require Term::Choose::LineFold::PP;
43 # $module = 'Term::Choose::LineFold::PP';
44 # };
45 # *Term::Choose::LineFold::char_width = \&{"${module}::char_width"};
46 # *Term::Choose::LineFold::print_columns = \&{"${module}::print_columns"};
47 # *Term::Choose::LineFold::cut_to_printwidth = \&{"${module}::cut_to_printwidth"};
48 # *Term::Choose::LineFold::adjust_to_printwidth = \&{"${module}::adjust_to_printwidth"};
49 #}
50  
51  
52 0 ▶sub line_fold {
Subroutine line_fold: not called CC=32 SCAR=69.6
Pod documented
53 0 my ( $str, $opt ) = @_; # copy $str
54 0 ▶ if ( ! length $str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $str) 0 0 0
55 0 return $str;
56 }
57 0 ▶ $opt //= {};
Condition0%0/2
$opt //= {}
l!l
00
MC/DC0%0/1
$opt //= {}
$opt
Truth table0%0/2
A
A= $opt
Aresult
11
00
58 0 ▶ $opt->{join} //= 1;
Condition0%0/2
$opt->{"join"} //= 1
l!l
00
MC/DC0%0/1
$opt->{"join"} //= 1
$opt->{"join"}
Truth table0%0/2
A
A= $opt->{"join"}
Aresult
11
00
59 0 ▶ $opt->{truncate_long_tabs} //= 1;
Condition0%0/2
$opt->{"truncate_long_tabs"} //= 1
l!l
00
MC/DC0%0/1
$opt->{"truncate_long_tabs"} //= 1
$opt->{"truncate_long_tabs"}
Truth table0%0/2
A
A= $opt->{"truncate_long_tabs"}
Aresult
11
00
60 0 ▶ if ( ! defined $opt->{width} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opt->{'width'}) { } 0 0 0
elsif (not $opt->{'width'} =~ /^[1-9][0-9]*\z/) { } 0 0 0
61 0 my ( $term_width, undef ) = get_term_size();
62 0 $opt->{width} = $term_width + EXTRA_W;
63 }
64 elsif ( $opt->{width} !~ /^[1-9][0-9]*\z/ ) {
65 0 croak "Option 'width': '$opt->{width}' is not an Integer 1 or greater!";
66 }
67 0 my $max_tab_width = int( $opt->{width} / 2 );
68 0 for ( $opt->{init_tab}, $opt->{subseq_tab} ) {
69 0 ▶ if ( length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) { } 0 0 0
70 0 ▶ if ( /^[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[0-9]+\z/) { } 0 0 0
71 0 $_ = ' ' x $_;
72 }
73 else {
74 0 s/\t/ /g;
75 0 s/\v+/\ \ /g; ##
76 0 s/[\p{Cc}\p{Noncharacter_Code_Point}\p{Cs}]//g;
77 }
78 0 ▶ if ( $opt->{truncate_long_tabs} && length > $max_tab_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"truncate_long_tabs"} and length $_ > $max_tab_width) 0 0 0
Condition0%0/3
$opt->{"truncate_long_tabs"} and length $_ > $max_tab_width
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"truncate_long_tabs"} and length $_ > $max_tab_width
$opt->{"truncate_long_tabs"} length $_ > $max_tab_width
Truth table0%0/3
A and B
A= $opt->{"truncate_long_tabs"}B= length $_ > $max_tab_width
ABresult
0X0
100
111
79 0 $_ = cut_to_printwidth( $_, $max_tab_width );
80 }
81 }
82 else {
83 0 $_ = '';
84 }
85 }
86 0 my @color;
87 0 ▶ if ( $opt->{color} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"color"}) 0 0 0
88 0 $str =~ s/${\PH}//g;
89 0 ▶ $str =~ s/(${\SGR_ES})/push( @color, $1 ) && ${\PH}/ge;
Condition0%0/3
push(@color, $1) && ${\"\x{feff}";}
!ll&&!rl&&r
000
MC/DC0%0/2
push(@color, $1) && ${\"\x{feff}";}
push(@color, $1) ${\"\x{feff}";}
Truth table0%0/3
A && B
A= push(@color, $1)B= ${\"\x{feff}";}
ABresult
0X0
100
111
90 }
91 0 ▶ if ( $opt->{binary_filter} && substr( $str, 0, 100 ) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"binary_filter"} and substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/) 0 0 0
Condition0%0/3
$opt->{"binary_filter"} and substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/
!ll&&!rl&&r
000
MC/DC0%0/2
$opt->{"binary_filter"} and substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/
$opt->{"binary_filter"} substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/
Truth table0%0/3
A and B
A= $opt->{"binary_filter"}B= substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/
ABresult
0X0
100
111
92 0 ▶ if ( $opt->{binary_filter} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'binary_filter'} == 2) { } 0 0 0
93 0 $str = sprintf( '%*v02X', ' ', $_[0] ); # use unmodified string
94 }
95 else {
96 0 $str = 'BNRY';
97 }
98 }
99 0 $str =~ s/\t/ /g;
100 0 $str =~ s/[^\v\P{Cc}]//g; # remove control chars but keep vertical spaces
101 0 $str =~ s/[\p{Noncharacter_Code_Point}\p{Cs}]//g;
102 0 my @paragraphs;
103  
104 0 for my $row ( split /\R/, $str, -1 ) { # -1 to keep trailing empty fields
105 0 my @lines;
106 0 $row =~ s/\s+\z//;
107 0 my @words = split( /(?<=\S)(?=\s)/, $row );
108 0 my $line = $opt->{init_tab};
109  
110 0 for my $i ( 0 .. $#words ) {
111 0 ▶ if ( print_columns( $line . $words[$i] ) <= $opt->{width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (print_columns($line . $words[$i]) <= $opt->{'width'}) { } 0 0 0
112 0 $line .= $words[$i];
113 }
114 else {
115 0 my $tmp;
116 0 ▶ if ( $i == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
117 0 $tmp = $opt->{init_tab} . $words[$i];
118 }
119 else {
120 0 push( @lines, $line );
121 0 $words[$i] =~ s/^\s+//;
122 0 $tmp = $opt->{subseq_tab} . $words[$i];
123 }
124 0 ( $line, my $remainder ) = cut_to_printwidth( $tmp, $opt->{width} );
125 0 ▶ while ( length $remainder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $remainder) 0 0 0
126 0 push( @lines, $line );
127 0 $tmp = $opt->{subseq_tab} . $remainder;
128 0 ( $line, $remainder ) = cut_to_printwidth( $tmp, $opt->{width} );
129 }
130 }
131 0 ▶ if ( $i == $#words ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#words) 0 0 0
132 0 push( @lines, $line );
133 }
134 }
135 0 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'join'}) { } 0 0 0
136 0 push( @paragraphs, join( "\n", @lines ) );
137 }
138 else {
139 0 ▶ if ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 0 0 0
140 0 push( @paragraphs, @lines );
141 }
142 else {
143 0 push( @paragraphs, '' );
144 }
145 }
146 }
147 0 ▶ if ( @color ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@color) 0 0 0
148 0 my $last_color;
149 0 for my $paragraph ( @paragraphs ) {
150 0 ▶ if ( ! $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"join"}) 0 0 0
151 0 ▶ if ( $last_color ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_color) 0 0 0
152 0 $paragraph = $last_color . $paragraph;
153 }
154 0 my $count = () = $paragraph =~ /${\PH}/g;
155 0 ▶ if ( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
156 0 $last_color = $color[$count - 1];
157 }
158 }
159 0 $paragraph =~ s/${\PH}/shift @color/ge;
160 0 ▶ if ( ! @color ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@color) 0 0 0
161 0 last;
162 }
163 }
164 0 $paragraphs[-1] .= "\e[0m";
165 }
166 0 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'join'}) { } 0 0 0
167 0 return join( "\n", @paragraphs );
168 }
169 else {
170 0 return @paragraphs;
171 }
172 }
173  
174  
175  
176 3 1;
177  
178 __END__
« Choose/Linux.pm ↑ Summary Choose/Win32.pm »