File Coverage

blib/lib/Indent/Word.pm
Criterion Covered Total %
statement 75 76 98.6
branch 21 22 95.4
condition 11 12 91.6
subroutine 11 11 100.0
pod 2 2 100.0
total 120 123 97.5


line stmt bran cond sub pod time code
1             package Indent::Word;
2              
3 4     4   78222 use strict;
  4         38  
  4         115  
4 4     4   21 use warnings;
  4         9  
  4         125  
5              
6 4     4   1877 use Class::Utils qw(set_params);
  4         115950  
  4         90  
7 4     4   313 use English qw(-no_match_vars);
  4         9  
  4         27  
8 4     4   1477 use Error::Pure qw(err);
  4         9  
  4         171  
9 4     4   1868 use Indent::Utils qw(line_size_check);
  4         9  
  4         203  
10 4     4   29 use Readonly;
  4         7  
  4         3116  
11              
12             # Constants.
13             Readonly::Scalar my $EMPTY_STR => q{};
14             Readonly::Scalar my $LINE_SIZE => 79;
15              
16             our $VERSION = 0.07;
17              
18             # Constructor.
19             sub new {
20 10     10 1 9445 my ($class, @params) = @_;
21 10         24 my $self = bless {}, $class;
22              
23             # Use with ANSI sequences.
24 10         30 $self->{'ansi'} = 0;
25              
26             # Options.
27 10         21 $self->{'line_size'} = $LINE_SIZE;
28 10         16 $self->{'next_indent'} = "\t";
29              
30             # Output.
31 10         19 $self->{'output_separator'} = "\n";
32              
33             # Process params.
34 10         38 set_params($self, @params);
35              
36             # 'line_size' check.
37 8         146 line_size_check($self);
38              
39             # Check rutine for removing ANSI sequences.
40 8 100       21 if ($self->{'ansi'}) {
41 1         2 eval {
42 1         9 require Text::ANSI::Util;
43             };
44 1 50       4 if ($EVAL_ERROR) {
45 0         0 err "Cannot load 'Text::ANSI::Util' module.";
46             }
47             }
48              
49             # Object.
50 8         30 return $self;
51             }
52              
53             # Indent text by words to line_size block size.
54             sub indent {
55 10     10 1 2876 my ($self, $data, $indent, $non_indent) = @_;
56              
57             # 'indent' initialization.
58 10 100       23 if (! defined $indent) {
59 4         5 $indent = $EMPTY_STR;
60             }
61              
62             # If non_indent data, than return.
63 10 100       21 if ($non_indent) {
64 1         4 return $indent.$data;
65             }
66              
67 9         26 my ($first, $second) = (undef, $indent.$data);
68 9         13 my $last_second_length = 0;
69 9         13 my @data;
70 9         13 my $one = 1;
71 9   100     20 while ($self->_length($second) >= $self->{'line_size'}
      100        
72             && $second =~ /^\s*\S+\s+/ms
73             && $last_second_length != $self->_length($second)) {
74              
75             # Last length of non-parsed part of data.
76 15         95 $last_second_length = $self->_length($second);
77              
78             # Parse to indent length.
79 15         88 ($first, my $tmp) = $self->_parse_to_indent_length($second);
80              
81             # If string is non-breakable in indent length, than parse to
82             # blank char.
83 15 100 100     43 if (! $first
      66        
84             || $self->_length($first) < $self->_length($indent)
85             || $first =~ /^$indent\s*$/ms) {
86              
87 5         97 ($first, $tmp) = $second
88             =~ /^($indent\s*[^\s]+?)\s(.*)$/msx;
89             }
90              
91             # If parsing is right.
92 15 100       92 if ($tmp) {
93              
94             # Non-parsed part of data.
95 13         20 $second = $tmp;
96              
97             # Add next_indent to string.
98 13 100       25 if ($one == 1) {
99 5         9 $indent .= $self->{'next_indent'};
100             }
101 13         19 $one = 0;
102 13         24 $second = $indent.$second;
103              
104             # Parsed part of data to @data array.
105 13         36 push @data, $first;
106             }
107             }
108              
109             # Add other data to @data array.
110 9         39 $second =~ s/\s+$//ms;
111 9 100       20 if ($second) {
112 7         14 push @data, $second;
113             }
114              
115             # Return as array or one line with output separator between its.
116 9 100       51 return wantarray ? @data : join($self->{'output_separator'}, @data);
117             }
118              
119             # Get length.
120             sub _length {
121 78     78   280 my ($self, $string) = @_;
122 78 100       126 if ($self->{'ansi'}) {
123 16         32 return length Text::ANSI::Util::ta_strip($string);
124             } else {
125 62         284 return length $string;
126             }
127             }
128              
129             # Parse to indent length.
130             sub _parse_to_indent_length {
131 15     15   22 my ($self, $string) = @_;
132 15         19 my @ret;
133 15 100       25 if ($self->{'ansi'}) {
134 3         7 my $string_wo_ansi = Text::ANSI::Util::ta_strip($string);
135              
136             # First part.
137 3         91 my ($first_wo_ansi) = $string_wo_ansi
138             =~ m/^(.{0,$self->{'line_size'}})\s+(.*)$/msx;
139 3         10 push @ret, Text::ANSI::Util::ta_trunc($string, length $first_wo_ansi);
140              
141             # Second part. (Remove first part + whitespace from string.)
142 3         1388 my $other_string_wo_ansi = Text::ANSI::Util::ta_strip(
143             Text::ANSI::Util::ta_substr($string, length $first_wo_ansi,
144             Text::ANSI::Util::ta_length($string))
145             );
146 3         1631 $other_string_wo_ansi =~ m/^(\s*)/ms;
147 3         9 my $count_of_spaces = length $1;
148 3         9 push @ret, Text::ANSI::Util::ta_substr($string, 0, (length $first_wo_ansi)
149             + $count_of_spaces, '');
150             } else {
151 12         143 @ret = $string =~ m/^(.{0,$self->{'line_size'}})\s+(.*)$/msx;
152             }
153 15         2535 return @ret;
154             }
155              
156             1;
157              
158             __END__