File Coverage

lib/App/Tel/Color/CiscoPingRainbow.pm
Criterion Covered Total %
statement 30 30 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 45 48 93.7


line stmt bran cond sub pod time code
1             package App::Tel::Color::CiscoPingRainbow;
2 2     2   897 use parent 'App::Tel::Color::Base';
  2         2  
  2         7  
3 2     2   94 use Term::ANSIColor;
  2         2  
  2         74  
4 2     2   6 use strict;
  2         2  
  2         25  
5 2     2   5 use warnings;
  2         1  
  2         434  
6              
7             =head2 new
8              
9             my $color = App::Tel::Color::CiscoPingRainbow->new;
10              
11             New color object..
12              
13             =cut
14              
15             sub new {
16 1     1 1 427 my $proto = shift;
17 1   33     6 my $class = ref($proto) || $proto;
18              
19 1         5 my $self = bless( {
20             'current_color' => 0,
21             'ping' => 0,
22             }, $class);
23              
24 1         7 $self->{'color_count'} = scalar $self->get_colors();
25 1         8 return $self;
26             }
27              
28             sub _next_color {
29 16     16   16 my $self = shift;
30 16         35 return colored(@_, $self->get_colors($self->{current_color}++ % $self->{color_count}));
31             }
32              
33             =head2 colorize
34              
35             my $output = $self->colorize($input);
36              
37             Given a line of text from a cisco router, this will try to colorize it.
38              
39             =cut
40              
41              
42             sub colorize {
43 3     3 1 5 my ($self, $text) = @_;
44              
45             # We need to define a start point in the buffer that the dot regex isn't
46             # allowed to search before so that this line and ones before it with
47             # dots don't get red dots.
48 3 100       16 if ($text =~ /Sending \d+, \d+-byte ICMP Echos to/) {
    50          
49 1         3 $self->{ping}=1;
50             } elsif ($self->{ping}) {
51 2         9 $text =~ s/(\!)/$self->_next_color($1)/eg;
  16         327  
52 2         21 $text =~ s/(\.)/colored('.', 'red')/eg;
  4         47  
53 2 100       20 if ($text =~ /Success rate is/) {
54 1         1 $self->{ping}=0;
55 1         2 $self->{current_color}=0;
56             }
57             }
58 3         10 return $text;
59             }
60              
61             1;