| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 0 | return $str; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $opt //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt //= {}
MC/DC0%0/1
$opt //= {}
$opt
Truth table0%0/2
A
A= $opt
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $opt->{join} //= 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt->{"join"} //= 1
MC/DC0%0/1
$opt->{"join"} //= 1
$opt->{"join"}
Truth table0%0/2
A
A= $opt->{"join"}
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $opt->{truncate_long_tabs} //= 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt->{"truncate_long_tabs"} //= 1
MC/DC0%0/1
$opt->{"truncate_long_tabs"} //= 1
$opt->{"truncate_long_tabs"}
Truth table0%0/2
A
A= $opt->{"truncate_long_tabs"}
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( ! defined $opt->{width} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( /^[0-9]+\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$opt->{"truncate_long_tabs"} and length $_ > $max_tab_width
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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}";}
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}";}
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $opt->{binary_filter} && substr( $str, 0, 100 ) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt->{"binary_filter"} and substr($str, 0, 100) =~ /[\x00-\x08\x0B-\x0C\x0E-\x1F]/
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]/
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( $opt->{binary_filter} == 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 0 | $line .= $words[$i]; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 0 | my $tmp; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( $i == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | 0 | push( @lines, $line ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ( $opt->{join} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | push( @paragraphs, join( "\n", @lines ) ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ( @lines ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | push( @paragraphs, @lines ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 0 | push( @paragraphs, '' ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( @color ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | my $last_color; | |||||||||||||||||||||||||||
| 149 | 0 | for my $paragraph ( @paragraphs ) { | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ( ! $opt->{join} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ( $last_color ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | $paragraph = $last_color . $paragraph; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | my $count = () = $paragraph =~ /${\PH}/g; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ( $count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $last_color = $color[$count - 1]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | $paragraph =~ s/${\PH}/shift @color/ge; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ( ! @color ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | last; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 0 | $paragraphs[-1] .= "\e[0m"; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ( $opt->{join} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return join( "\n", @paragraphs ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | return @paragraphs; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||