Choose/LineFold.pm

filter: Statementstmt 28.4% 35 / 123 Branchbran 2.0% 1 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 22.8% 46 / 201 CC 35 SCAR 67.4
CC
35
Cov
20%
line_fold
1122.0
BEGIN
3.1
BEGIN
1.0
CRAP 841.9
« 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.780';
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 BEGIN {
19 3 ▶ my $module;
Subroutine BEGIN: called CC=3 SCAR=11.4
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" } 3 0 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 sub line_fold {
53 0 ▶ my ( $str, $opt ) = @_; # copy $str
Subroutine line_fold: not called CC=33 SCAR=70.2
Pod documented
54 0 ▶ if ( ! length $str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $str) 0 0 0
55 0 return $str;
56 }
57 ################################### 24.03.2025
58 0 ▶ if ( defined $opt && ! ref $opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt and not ref $opt) 0 0 0
Condition0%0/3
defined $opt and not ref $opt
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt and not ref $opt
defined $opt not ref $opt
Truth table0%0/3
A and not(B)
A= defined $optB= not ref $opt
ABresult
0X0
100
111
59 0 my $width = $opt;
60 0 ▶ $opt = $_[2] // {};
Condition0%0/2
$_[2] // {}
l!l
00
MC/DC0%0/1
$_[2] // {}
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
61 0 $opt->{width} = $width;
62 }
63 ###################################
64 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
65 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
66 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
67 0 my ( $term_width, undef ) = get_term_size();
68 0 $opt->{width} = $term_width + EXTRA_W;
69 }
70 elsif ( $opt->{width} !~ /^[1-9][0-9]*\z/ ) {
71 0 croak "Option 'width': '$opt->{width}' is not an Integer 1 or greater!";
72 }
73 0 my $max_tab_width = int( $opt->{width} / 2 );
74 0 for ( $opt->{init_tab}, $opt->{subseq_tab} ) {
75 0 ▶ if ( length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) { } 0 0 0
76 0 ▶ if ( /^[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[0-9]+\z/) { } 0 0 0
77 0 $_ = ' ' x $_;
78 }
79 else {
80 0 s/\t/ /g;
81 0 s/\v+/\ \ /g; ##
82 0 s/[\p{Cc}\p{Noncharacter_Code_Point}\p{Cs}]//g;
83 }
84 0 ▶ if ( length > $max_tab_width ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $max_tab_width) 0 0 0
85 0 $_ = cut_to_printwidth( $_, $max_tab_width );
86 }
87 }
88 else {
89 0 $_ = '';
90 }
91 }
92 0 my @color;
93 0 ▶ if ( $opt->{color} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"color"}) 0 0 0
94 0 $str =~ s/${\PH}//g;
95 0 ▶ $str =~ s/(${\SGR_ES})/push( @color, $1 ) && ${\PH}/ge;
Branch: 1 branch
BranchTrueFalseTotal
push @color, $1 and ${\"\x{feff}";} 0 0 0
96 }
97 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
98 #$str = $self->{binary_filter} == 2 ? sprintf("%v02X", $_[0]) =~ tr/./ /r : 'BNRY'; # perl 5.14
99 0 ▶ if ( $opt->{binary_filter} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'binary_filter'} == 2) { } 0 0 0
100 0 ( $str = sprintf( "%v02X", $_[0] ) ) =~ tr/./ /; # use unmodified string
101 }
102 else {
103 0 $str = 'BNRY';
104 }
105 }
106 0 $str =~ s/\t/ /g;
107 0 $str =~ s/[^\v\P{Cc}]//g; # remove control chars but keep vertical spaces
108 0 $str =~ s/[\p{Noncharacter_Code_Point}\p{Cs}]//g;
109 0 my @paragraphs;
110  
111 0 for my $row ( split /\R/, $str, -1 ) { # -1 to keep trailing empty fields
112 0 my @lines;
113 0 $row =~ s/\s+\z//;
114 0 my @words = split( /(?<=\S)(?=\s)/, $row );
115 0 my $line = $opt->{init_tab};
116  
117 0 for my $i ( 0 .. $#words ) {
118 0 ▶ if ( print_columns( $line . $words[$i] ) <= $opt->{width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (print_columns($line . $words[$i]) <= $opt->{'width'}) { } 0 0 0
119 0 $line .= $words[$i];
120 }
121 else {
122 0 my $tmp;
123 0 ▶ if ( $i == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
124 0 $tmp = $opt->{init_tab} . $words[$i];
125 }
126 else {
127 0 push( @lines, $line );
128 0 $words[$i] =~ s/^\s+//;
129 0 $tmp = $opt->{subseq_tab} . $words[$i];
130 }
131 0 ( $line, my $remainder ) = cut_to_printwidth( $tmp, $opt->{width} );
132 0 ▶ while ( length $remainder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $remainder) 0 0 0
133 0 push( @lines, $line );
134 0 $tmp = $opt->{subseq_tab} . $remainder;
135 0 ( $line, $remainder ) = cut_to_printwidth( $tmp, $opt->{width} );
136 }
137 }
138 0 ▶ if ( $i == $#words ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#words) 0 0 0
139 0 push( @lines, $line );
140 }
141 }
142 0 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'join'}) { } 0 0 0
143 0 push( @paragraphs, join( "\n", @lines ) );
144 }
145 else {
146 0 ▶ if ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) { } 0 0 0
147 0 push( @paragraphs, @lines );
148 }
149 else {
150 0 push( @paragraphs, '' );
151 }
152 }
153 }
154 0 ▶ if ( @color ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@color) 0 0 0
155 0 my $last_color;
156 0 for my $paragraph ( @paragraphs ) {
157 0 ▶ if ( ! $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"join"}) 0 0 0
158 0 ▶ if ( $last_color ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_color) 0 0 0
159 0 $paragraph = $last_color . $paragraph;
160 }
161 0 my $count = () = $paragraph =~ /${\PH}/g;
162 0 ▶ if ( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
163 0 $last_color = $color[$count - 1];
164 }
165 }
166 0 $paragraph =~ s/${\PH}/shift @color/ge;
167 0 ▶ if ( ! @color ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@color) 0 0 0
168 0 last;
169 }
170 }
171 0 $paragraphs[-1] .= "\e[0m";
172 }
173 0 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'join'}) { } 0 0 0
174 0 return join( "\n", @paragraphs );
175 }
176 else {
177 0 return @paragraphs;
178 }
179 }
180  
181  
182  
183 3 1;
184  
185 __END__
« Choose/Linux.pm ↑ Summary Choose/Win32.pm »