Indent/Word.pm

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