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