File Coverage

blib/lib/App/Tailor.pm
Criterion Covered Total %
statement 61 73 83.5
branch 15 24 62.5
condition 2 14 14.2
subroutine 15 17 88.2
pod 6 8 75.0
total 99 136 72.7


line stmt bran cond sub pod time code
1             package App::Tailor;
2             # ABSTRACT: easily tailor terminal output to meet your needs
3             $App::Tailor::VERSION = '0.01';
4              
5              
6 4     4   992108 use strict;
  4         39  
  4         131  
7 4     4   21 use warnings;
  4         7  
  4         160  
8              
9 4     4   22 use Term::ANSIColor qw(color RESET);
  4         7  
  4         248  
10              
11 4     4   2318 use parent 'Exporter';
  4         1371  
  4         22  
12              
13             our @EXPORT = qw(
14             ignore
15             modify
16             colorize
17             tail
18             itail
19             reset_rules
20             );
21              
22 4     4   316 use constant IGNORE => 1;
  4         8  
  4         313  
23 4     4   21 use constant MODIFY => 2;
  4         7  
  4         168  
24 4     4   21 use constant COLORIZE => 3;
  4         8  
  4         3480  
25              
26             our @RULES;
27             our $RESET = RESET;
28             our $DEBUG;
29              
30             sub debug (&) {
31 57 50 33 57 0 217 if ($DEBUG || $ENV{APP_TAILOR_DEBUG}) {
32 0   0     0 my $msg = $_[0]->() || return;
33 0         0 warn __PACKAGE__.": $msg\n";
34             }
35             }
36              
37             sub reset_rules () {
38 4     4 1 3588 undef @RULES;
39             }
40              
41             sub itail (;$) {
42 4   33 4 1 28 my $fh = shift || *STDIN;
43 4         10 my $closed;
44              
45             return sub{
46 18 50   18   15884 return if $closed;
47              
48 18         63 LINE: until ($closed) {
49 20         90 my $line = <$fh>;
50              
51 20 100       53 unless (defined $line) {
52 4         13 $closed = 1;
53 4         24 return;
54             }
55              
56 16         54 chomp $line;
57              
58 16         98 debug{ "Input=[[$line]]" };
  0         0  
59              
60 16         73 for (@RULES) {
61 41         76 $line = apply_rule($line, $_);
62              
63 41 100       99 unless (defined $line) {
64 2         7 next LINE;
65             }
66             }
67              
68 14         93 return $line."\n";
69             }
70              
71 0         0 debug{ 'end of input' };
  0         0  
72 4         31 };
73             }
74              
75             sub tail (;$$) {
76 0   0 0 1 0 my $in = shift || *STDIN;
77 0   0     0 my $out = shift || *STDOUT;
78 0         0 my $iter = itail $in;
79 0         0 while ( defined( my $line = $iter->() ) ) {
80 0         0 print $out $line;
81             }
82             }
83              
84             sub apply_rule {
85 41     41 0 79 my ($line, $rule) = @_;
86 41         85 my ($type, @rule) = @$rule;
87              
88             debug{
89 0 0   0   0 my $label = $type == IGNORE ? 'ignore'
    0          
    0          
90             : $type == MODIFY ? 'modify'
91             : $type == COLORIZE ? 'colorize'
92             : $type;
93              
94 0         0 "applying rule <$label>: [@rule]";
95 41         149 };
96              
97 41 100       167 if ($type == IGNORE) {
    100          
    50          
98 7         12 my ($regex) = @rule;
99 7 100       41 return if $line =~ /$regex/;
100             }
101             elsif ($type == MODIFY) {
102 9         19 my ($regex, $replace) = @rule;
103              
104 9 100       20 if (ref $replace eq 'CODE') {
105 3         17 $line =~ s/$regex/
106 1         4 local $_ = $line;
107 1         3 $replace->($line);
108             /xe;
109             }
110             else {
111 6         22 $line =~ s/$regex/$replace/;
112             }
113             }
114             elsif ($type == COLORIZE) {
115 25         45 my ($regex, $color) = @rule;
116 25         294 $line =~ s/($regex)/$color$1$RESET/;
117             }
118              
119 39         107 return $line;
120             }
121              
122             sub ignore ($) {
123 2     2 1 16 my ($regex) = @_;
124 2         6 push @RULES, [IGNORE, $regex];
125             }
126              
127             sub modify ($$) {
128 3     3 1 25 my ($regex, $replacement) = @_;
129 3         9 push @RULES, [MODIFY, $regex, $replacement];
130             }
131              
132             sub colorize ($@) {
133 5     5 1 26 my ($regex, @colors) = @_;
134 5         12 my $color = color @colors;
135 5         115 push @RULES, [COLORIZE, $regex, $color];
136             }
137              
138             1;
139              
140             __END__