File Coverage

blib/lib/String/Indent.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 6 100.0
condition 17 22 77.2
subroutine 5 5 100.0
pod 1 1 100.0
total 55 60 91.6


line stmt bran cond sub pod time code
1             package String::Indent;
2              
3 2     2   606121 use 5.010001;
  2         10  
4 2     2   13 use strict;
  2         9  
  2         61  
5 2     2   156 use warnings;
  2         6  
  2         269  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-01-25'; # DATE
9             our $DIST = 'String-Indent'; # DIST
10             our $VERSION = '0.040'; # VERSION
11              
12 2     2   12 use Exporter qw(import);
  2         7  
  2         769  
13             our @EXPORT_OK = qw(
14             indent
15             );
16              
17             sub indent {
18 6     6 1 426429 my ($indent, $str, $opts) = @_;
19 6   100     27 $opts //= {};
20              
21 6   100     30 my $ibl = $opts->{indent_blank_lines} // 1;
22 6   66     28 my $fli = $opts->{first_line_indent} // $indent;
23 6   66     23 my $sli = $opts->{subsequent_lines_indent} // $indent;
24 6   66     24 my $flopi = $opts->{first_line_of_para_indent} // $sli // $indent;
      33        
25             #say "D:ibl=<$ibl>, fli=<$fli>, flopi=<$flopi>, sli=<$sli>";
26              
27 6         9 my $i = 0;
28 6         10 my $prev_blank;
29 6 100 100     48 $str =~ s/^([^\r\n]?)/$i++; my $blank = !$1; my $start_para = $i==1 || $prev_blank; $prev_blank = $blank; !$ibl && $blank ? "$1" : $i==1 ? "$fli$1" : $start_para ? "$flopi$1" : "$sli$1"/egm;
  19 100 100     33  
  19 100       44  
  19         60  
  19         28  
  19         184  
30 6         52 $str;
31             }
32              
33             1;
34             # ABSTRACT: String indenting routines
35              
36             __END__