File Coverage

lib/Badger/Rainbow.pm
Criterion Covered Total %
statement 24 28 85.7
branch 5 6 83.3
condition 2 6 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 39 49 79.5


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Rainbow
4             #
5             # DESCRIPTION
6             # Colour-related functionality, used primarily for debugging.
7             #
8             # AUTHOR
9             # Andy Wardley
10             #
11             #========================================================================
12              
13             package Badger::Rainbow;
14              
15 70     70   2426 use Carp;
  70         108  
  70         7245  
16             #use Badger::Debug ':debug';
17             use Badger::Class
18 70         1152 version => 0.01,
19             base => 'Badger::Exporter',
20             constants => 'DELIMITER ARRAY REFS PKG ALL',
21             exports => {
22             any => 'ANSI_escape ANSI_colours strip_ANSI_escapes',
23             hooks => {
24             ANSI => \&_export_ANSI_colours,
25             },
26             },
27             constant => {
28             ANSI_colours => {
29             bold => 1,
30             dark => 2,
31             black => 30,
32             red => 31,
33             green => 32,
34             yellow => 33,
35             blue => 34,
36             magenta => 35,
37             cyan => 36,
38             grey => 37,
39             white => 38,
40             },
41 70     70   37031 };
  70         173  
42              
43             # can't import this via Badger::Debug as it depends on Badger::Rainbow
44             our $DEBUG = 0 unless defined $DEBUG;
45              
46             sub _export_ANSI_colours {
47 142     142   384 my ($class, $target, $symbol, $more_symbols) = @_;
48 142         205 my $ansi = ANSI_colours;
49 142   33     373 my $cols = shift(@$more_symbols)
50             || croak "You didn't specify any ANSI colours to import";
51              
52 70     70   478 no strict REFS;
  70         123  
  70         29375  
53              
54 142 100       388 $cols = [ keys %$ansi ]
55             if $cols eq ALL;
56              
57 142 100       1538 $cols = [ split(DELIMITER, $cols) ]
58             unless ref $cols eq ARRAY;
59              
60 142         370 foreach my $col (@$cols) {
61             # $class->debug("Exporting ANSI clolour $col to $target\n") if $DEBUG;
62 647   33     1335 my $val = $ansi->{ $col }
63             || croak "Invalid ANSI colour specified to import: $col";
64 647         2723 *{ $target.PKG.$col } = sub(@) {
65 3     3   20 ANSI_escape($val, @_)
66 647         1865 };
67             }
68             }
69              
70              
71             #-----------------------------------------------------------------------
72             # ANSI_escape($attr, $text)
73             #
74             # Adds ANSI escape codes to each line of text to colour output.
75             #-----------------------------------------------------------------------
76              
77             sub ANSI_escape {
78 3     3 1 4 my $attr = shift;
79 3         5 my $text = join('', grep { defined $_ } @_);
  3         11  
80             return join("\n",
81             map {
82             # look for an existing escape start sequence and add new
83             # attribute to it, otherwise add escape start/end sequences
84 3 50       7 s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx
  3         22  
85             ? $_
86             : "\e[${attr}m" . $_ . "\e[0m";
87             }
88             split(/\n/, $text, -1) # -1 prevents it from ignoring trailing fields
89             );
90             }
91              
92              
93             sub strip_ANSI_escapes {
94 0     0 1   my $text = join('', grep { defined $_ } @_);
  0            
95 0           $text =~ s/ \e .*? m//gx;
96 0           return $text;
97             }
98              
99              
100              
101              
102             1;
103              
104             __END__