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   466 use 5.010;
  1         6  
4 1     1   4 use strict;
  1         3  
  1         22  
5 1     1   417 use syntax 'each_on_array'; # to support perl < 5.12
  1         23010  
  1         5  
6 1     1   3457 use warnings;
  1         3  
  1         27  
7             #use Log::Any '$log';
8              
9 1     1   442 use Data::Unixish::Util qw(%common_args);
  1         3  
  1         375  
10              
11             our $VERSION = '1.570'; # 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 9 my %args = @_;
39 3         7 my ($in, $out) = ($args{in}, $args{out});
40              
41 3         9 _indent_begin(\%args);
42 3         16 while (my ($index, $item) = each @$in) {
43 12         24 push @$out, _indent_item($item, \%args);
44             }
45              
46 3         12 [200, "OK"];
47             }
48              
49             sub _indent_begin {
50 6     6   10 my $args = shift;
51              
52             # args abused to store state
53 6 100 100     36 $args->{_indent} = ($args->{tab} ? "\t" : " ") x ($args->{num} // 4);
54             }
55              
56             sub _indent_item {
57 24     24   43 my ($item, $args) = @_;
58              
59 24 100 66     83 if (defined($item) && !ref($item)) {
60 18         90 $item =~ s/^/$args->{_indent}/mg;
61             }
62 24         80 return $item;
63             }
64              
65             1;
66             # ABSTRACT: Add spaces or tabs to the beginnning of each line of text
67              
68             __END__