File Coverage

blib/lib/Data/Unixish/indent.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition 4 5 80.0
subroutine 8 8 100.0
pod 1 1 100.0
total 43 44 97.7


line stmt bran cond sub pod time code
1             package Data::Unixish::indent;
2              
3 1     1   423 use 5.010;
  1         6  
4 1     1   4 use strict;
  1         29  
  1         26  
5 1     1   344 use syntax 'each_on_array'; # to support perl < 5.12
  1         19574  
  1         5  
6 1     1   2902 use warnings;
  1         2  
  1         23  
7             #use Log::Any '$log';
8              
9 1     1   370 use Data::Unixish::Util qw(%common_args);
  1         2  
  1         308  
10              
11             our $VERSION = '1.572'; # VERSION
12              
13             our %SPEC;
14              
15             $SPEC{indent} = {
16             v => 1.1,
17             summary => 'Add spaces or tabs to the beginnning of each line of text',
18             args => {
19             %common_args,
20             num => {
21             summary => 'Number of spaces to add',
22             schema => ['int*', default=>4],
23             cmdline_aliases => {
24             n => {},
25             },
26             },
27             tab => {
28             summary => 'Number of spaces to add',
29             schema => ['bool' => default => 0],
30             cmdline_aliases => {
31             t => {},
32             },
33             },
34             },
35             tags => [qw/text itemfunc/],
36             };
37             sub indent {
38 3     3 1 8 my %args = @_;
39 3         5 my ($in, $out) = ($args{in}, $args{out});
40              
41 3         8 _indent_begin(\%args);
42 3         10 while (my ($index, $item) = each @$in) {
43 12         18 push @$out, _indent_item($item, \%args);
44             }
45              
46 3         10 [200, "OK"];
47             }
48              
49             sub _indent_begin {
50 6     6   9 my $args = shift;
51              
52             # args abused to store state
53 6 100 100     31 $args->{_indent} = ($args->{tab} ? "\t" : " ") x ($args->{num} // 4);
54             }
55              
56             sub _indent_item {
57 24     24   32 my ($item, $args) = @_;
58              
59 24 100 66     74 if (defined($item) && !ref($item)) {
60 18         70 $item =~ s/^/$args->{_indent}/mg;
61             }
62 24         74 return $item;
63             }
64              
65             1;
66             # ABSTRACT: Add spaces or tabs to the beginnning of each line of text
67              
68             __END__