File Coverage

blib/lib/Indent/Form.pm
Criterion Covered Total %
statement 80 85 94.1
branch 27 32 84.3
condition n/a
subroutine 13 13 100.0
pod 2 2 100.0
total 122 132 92.4


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