Indent/Word.pm

filter: Statementstmt 96.6% 86 / 89 Branchbran 90.6% 29 / 32 Conditioncond 91.6% 11 / 12 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 94.7% 144 / 152 CC 21 SCAR 19.9
CC
21
Cov
95%
indent
13.0
new
6.3
_parse_to_indent_length
3.0
CRAP 7.3
« Indent/Block.pm ↑ Summary Indent/Data.pm »
1 package Indent::Word;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Indent::Utils qw(line_size_check);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Constants.
13 4 Readonly::Scalar my $EMPTY_STR => q{};
14 4 Readonly::Scalar my $LINE_SIZE => 79;
15  
16 4 our $VERSION = 0.10;
17  
18 # Constructor.
19 sub new {
20 16 ▶ my ($class, @params) = @_;
Subroutine new: called CC=6 SCAR=18.4
Pod documented
21 16 my $self = bless {}, $class;
22  
23 # Use with ANSI sequences.
24 16 $self->{'ansi'} = undef;
25  
26 # Options.
27 16 $self->{'line_size'} = $LINE_SIZE;
28 16 $self->{'next_indent'} = "\t";
29  
30 # Output.
31 16 $self->{'output_separator'} = "\n";
32  
33 # Process params.
34 16 set_params($self, @params);
35  
36 # 'line_size' check.
37 14 line_size_check($self);
38  
39 14 ▶ if (! defined $self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ansi"}) 1 13 14
40 1 ▶ if (exists $ENV{'NO_COLOR'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'NO_COLOR'}) { } 0 1 1
elsif (defined $ENV{'COLOR'}) { } 0 1 1
41 0 $self->{'ansi'} = 0;
42 } elsif (defined $ENV{'COLOR'}) {
43 0 $self->{'ansi'} = 1;
44 } else {
45 1 $self->{'ansi'} = 0;
46 }
47 }
48  
49 # Check rutine for removing ANSI sequences.
50 14 ▶ if ($self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ansi"}) 4 10 14
51 4 eval {
52 4 require Text::ANSI::Util;
53 };
54 4 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Indent::Word::EVAL_ERROR) 0 4 4
55 0 err "Cannot load 'Text::ANSI::Util' module.";
56 }
57 }
58  
59 # Object.
60 14 return $self;
61 }
62  
63 # Indent text by words to line_size block size.
64 sub indent {
65 16 ▶ my ($self, $data, $indent, $non_indent) = @_;
Subroutine indent: called CC=13 SCAR=25.7
Pod documented
66  
67 # 'indent' initialization.
68 16 ▶ if (! defined $indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $indent) 4 12 16
69 4 $indent = $EMPTY_STR;
70 }
71  
72 # If non_indent data, than return.
73 16 ▶ if ($non_indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_indent) 1 15 16
74 1 return $indent.$data;
75 }
76  
77 # Normalize whitespace: collapse all whitespace (including newlines) to single spaces.
78 15 $data =~ s/\s+/ /g;
79  
80 # Remove leading and trailing whitespace.
81 15 $data =~ s/^\s+//;
82 15 $data =~ s/\s+$//;
83  
84 15 my ($first, $second) = (undef, $indent.$data);
85 15 my $last_second_length = 0;
86 15 my @data;
87 15 my $one = 1;
88 15 ▶ while ($self->_length($second) >= $self->{'line_size'}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_length($second) >= $self->{"line_size"} and $second =~ /^\s*\S+\s+/ms and $last_second_length != $self->_length($second)) 19 15 34
Condition100%6/6
$self->_length($second) >= $self->{"line_size"} and $second =~ /^\s*\S+\s+/ms and $last_second_length != $self->_length($second)
!ll&&!rl&&r
11419
$self->_length($second) >= $self->{"line_size"} and $second =~ /^\s*\S+\s+/ms
!ll&&!rl&&r
6523
MC/DC100%3/3
$self->_length($second) >= $self->{"line_size"} and $second =~ /^\s*\S+\s+/ms and $last_second_length != $self->_length($second)
$self->_length($second) >= $self->{"line_size"} $second =~ /^\s*\S+\s+/ms $last_second_length != $self->_length($second)
Truth table100%4/4
A and B and C
A= $self->_length($second) >= $self->{"line_size"}B= $second =~ /^\s*\S+\s+/msC= $last_second_length != $self->_length($second)
ABCresult
0XX0
10X0
1100
1111
89 && $second =~ /^\s*\S+\s+/ms
90 && $last_second_length != $self->_length($second)) {
91  
92 # Last length of non-parsed part of data.
93 19 $last_second_length = $self->_length($second);
94  
95 # Parse to indent length.
96 19 ($first, my $tmp) = $self->_parse_to_indent_length($second);
97  
98 # If string is non-breakable in indent length, than parse to
99 # blank char.
100 19 ▶ if (! $first
Branch: 1 branch
BranchTrueFalseTotal
if (not $first or $self->_length($first) < $self->_length($indent) or $first =~ /^$indent\s*$/ms) 4 9 13
Condition83%5/6
not $first or $self->_length($first) < $self->_length($indent) or $first =~ /^$indent\s*$/ms
l!l&&r!l&&!r
1009
not $first or $self->_length($first) < $self->_length($indent)
l!l&&r!l&&!r
649
MC/DC66%2/3
not $first or $self->_length($first) < $self->_length($indent) or $first =~ /^$indent\s*$/ms
not $first $self->_length($first) < $self->_length($indent) $first =~ /^$indent\s*$/ms
Truth table75%3/4
not(A) or B or C
A= not $firstB= $self->_length($first) < $self->_length($indent)C= $first =~ /^$indent\s*$/ms
ABCresult
1XX1
01X1
0011
0000
101 || $self->_length($first) < $self->_length($indent)
102 || $first =~ /^$indent\s*$/ms) {
103  
104 10 ($first, $tmp) = $second
105 =~ /^($indent\s*[^\s]+?)\s(.*)$/msx;
106 }
107  
108 # If parsing is right.
109 19 ▶ if ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp) 15 4 19
110  
111 # Non-parsed part of data.
112 15 $second = $tmp;
113  
114 # Add next_indent to string.
115 15 ▶ if ($one == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one == 1) 7 8 15
116 7 $indent .= $self->{'next_indent'};
117 }
118 15 $one = 0;
119 15 $second = $indent.$second;
120  
121 # Parsed part of data to @data array.
122 15 push @data, $first;
123 }
124 }
125  
126 # Add other data to @data array.
127 15 $second =~ s/\s+$//ms;
128 15 ▶ if ($second) {
Branch: 1 branch
BranchTrueFalseTotal
if ($second) 14 1 15
129 14 push @data, $second;
130 }
131  
132 # Return as array or one line with output separator between its.
133 15 ▶ return wantarray ? @data : join($self->{'output_separator'}, @data);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 6 15
134 }
135  
136 # Get length.
137 sub _length {
138 102 ▶ my ($self, $string) = @_;
Subroutine _length: called CC=2 SCAR=6.9
139 102 ▶ if ($self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ansi'}) { } 46 56 102
140 46 return length Text::ANSI::Util::ta_strip($string);
141 } else {
142 56 return length $string;
143 }
144 }
145  
146 # Parse to indent length.
147 sub _parse_to_indent_length {
148 19 ▶ my ($self, $string) = @_;
Subroutine _parse_to_indent_length: called CC=3 SCAR=11.0
149 19 my @ret;
150 19 ▶ if ($self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ansi'}) { } 9 10 19
151 9 my $string_wo_ansi = Text::ANSI::Util::ta_strip($string);
152  
153 # First part.
154 9 my ($first_wo_ansi) = $string_wo_ansi
155 =~ m/^(.{0,$self->{'line_size'}})\s+(.*)$/msx;
156 9 ▶ if (defined $first_wo_ansi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_wo_ansi) 6 3 9
157 6 push @ret, Text::ANSI::Util::ta_trunc($string, length $first_wo_ansi);
158  
159 # Second part. (Remove first part + whitespace from string.)
160 6 my $other_string_wo_ansi = Text::ANSI::Util::ta_strip(
161 Text::ANSI::Util::ta_substr($string, length $first_wo_ansi,
162 Text::ANSI::Util::ta_length($string))
163 );
164 6 $other_string_wo_ansi =~ m/^(\s*)/ms;
165 6 my $count_of_spaces = length $1;
166 6 push @ret, Text::ANSI::Util::ta_substr($string, 0, (length $first_wo_ansi)
167 + $count_of_spaces, '');
168 }
169 } else {
170 10 @ret = $string =~ m/^(.{0,$self->{'line_size'}})\s+(.*)$/msx;
171 }
172 19 return @ret;
173 }
174  
175 4 1;
176  
177 __END__
« Indent/Block.pm ↑ Summary Indent/Data.pm »