blib/lib/Indent/Form.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 85.2% 29 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 92.8% 129 / 139 CC 20 SCAR 19.2
CC
20
Cov
92%
indent
13.0
new
5.4
_length
3.1
CRAP 6.8
↑ Summary
1 package Indent::Form;
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::Word;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use List::Util 1.33 qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Constants.
14 4 Readonly::Scalar my $EMPTY_STR => q{};
15 4 Readonly::Scalar my $LINE_SIZE => 79;
16 4 Readonly::Scalar my $SPACE => q{ };
17  
18 4 our $VERSION = 0.09;
19  
20 # Constructor.
21 16 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
22 16 my ($class, @params) = @_;
23 16 my $self = bless {}, $class;
24  
25 # Use with ANSI sequences.
26 16 $self->{'ansi'} = 0;
27  
28 # Align.
29 16 $self->{'align'} = 'right';
30  
31 # Fill character.
32 16 $self->{'fill_character'} = $SPACE;
33  
34 # Form separator.
35 16 $self->{'form_separator'} = ': ';
36  
37 # Line size.
38 16 $self->{'line_size'} = $LINE_SIZE;
39  
40 # Next indent.
41 16 $self->{'next_indent'} = undef;
42  
43 # Output separator.
44 16 $self->{'output_separator'} = "\n";
45  
46 # Process params.
47 16 set_params($self, @params);
48  
49 # Check align.
50 14 ▶ if (none { $self->{'align'} eq $_ } qw(left right)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $self->{"align"} eq $_; } , "left", "right")) 1 13 14
51 1 err '\'align\' parameter must be a \'left\' or \'right\' '.
52 'string.';
53 }
54  
55 # 'line_size' check.
56 13 ▶ if ($self->{'line_size'} !~ /^\d*$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"line_size"} =~ /^\d*$/ms) 3 10 13
57 err '\'line_size\' parameter must be a number.',
58 3 'line_size', $self->{'line_size'};
59 }
60  
61 # Check rutine for removing ANSI sequences.
62 10 ▶ if ($self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ansi"}) 0 10 10
63 0 eval {
64 0 require Text::ANSI::Util;
65 };
66 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Indent::Form::EVAL_ERROR) 0 0 0
67 0 err "Cannot load 'Text::ANSI::Util' module.";
68 }
69 }
70  
71 # Object.
72 10 return $self;
73 }
74  
75 # Indent form data.
76 9 ▶sub indent {
Subroutine indent: called CC=13 SCAR=25.7
Pod documented
77 9 my ($self, $data_ar, $actual_indent, $non_indent_flag) = @_;
78  
79 # Undef indent.
80 9 ▶ if (! $actual_indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual_indent) 8 1 9
81 8 $actual_indent = $EMPTY_STR;
82 }
83  
84 # Max size of key.
85 9 my $max = 0;
86 9 my @data;
87 9 foreach my $dat (@{$data_ar}) {
88 28 ▶ if ($self->_length($dat->[0]) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_length($dat->[0]) > $max) 13 15 28
89 13 $max = $self->_length($dat->[0]);
90 }
91  
92 # Non-indent.
93 28 ▶ if ($non_indent_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_indent_flag) 8 20 28
94 8 push @data, $self->_value($dat->[0]).$self->{'form_separator'}.
95 $self->_value($dat->[1]);
96 }
97 }
98  
99 # If non-indent.
100 # Return as array or one line with output separator between its.
101 9 ▶ if ($non_indent_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($non_indent_flag) 2 7 9
102 return wantarray ? @data
103 2 ▶ : join $self->{'output_separator'}, @data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
104 }
105  
106 # Indent word.
107 my $next_indent = $self->{'next_indent'} ? $self->{'next_indent'}
108 7 ▶ : $SPACE x ($max + $self->_length($self->{'form_separator'}));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'next_indent'} ? : 0 7 7
109 my $word = Indent::Word->new(
110 'ansi' => $self->{'ansi'},
111 'line_size' => $self->{'line_size'} - $max
112 7 - $self->_length($self->{'form_separator'}),
113 'next_indent' => $next_indent,
114 );
115  
116 7 foreach my $dat_ar (@{$data_ar}) {
117 20 my $output = $actual_indent;
118  
119 # Left side, left align.
120 20 ▶ if ($self->{'align'} eq 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'align'} eq 'left') { } 3 17 20
121 3 $output .= $self->_value($dat_ar->[0]);
122 3 $output .= $self->{'fill_character'}
123 x ($max - $self->_length($dat_ar->[0]));
124  
125 # Left side, right align.
126 } else {
127 17 $output .= $self->{'fill_character'}
128 x ($max - $self->_length($dat_ar->[0]));
129 17 $output .= $self->_value($dat_ar->[0]);
130 }
131  
132 # Right side.
133 20 $output .= $self->{'form_separator'};
134 20 my @tmp = $word->indent($self->_value($dat_ar->[1]));
135 20 ▶ $output .= shift @tmp if @tmp;
Branch: 1 branch
BranchTrueFalseTotal
if @tmp 18 2 20
136 20 push @data, $output;
137 20 ▶ while (@tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 4 20 24
138 4 push @data, $actual_indent.shift @tmp;
139 }
140 }
141  
142 # Return as array or one line with output separator between its.
143 7 ▶ return wantarray ? @data : join $self->{'output_separator'}, @data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 1 7
144 }
145  
146 # Get length.
147 75 ▶sub _length {
Subroutine _length: called CC=3 SCAR=11.2
148 75 my ($self, $string) = @_;
149  
150 75 ▶ if (! defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 10 65 75
151 10 return 0;
152 }
153  
154 65 ▶ if ($self->{'ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ansi'}) { } 0 65 65
155 0 return length Text::ANSI::Util::ta_strip($string);
156 } else {
157 65 return length $string;
158 }
159 }
160  
161 56 ▶sub _value {
Subroutine _value: called CC=2 SCAR=6.9
162 56 my ($self, $string) = @_;
163  
164 56 ▶ if (! defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 13 43 56
165 13 return $EMPTY_STR;
166 }
167  
168 43 return $string;
169 }
170  
171 4 1;
172  
173 __END__
↑ Summary