lib/Badger/Rainbow.pm

filter: Statementstmt 86.6% 26 / 30 Branchbran 75.0% 6 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 73.6% 42 / 57 CC 7 SCAR 12.9
CC
7
Cov
77%
_export_ANSI_colours
6.2
ANSI_escape
2.0
strip_ANSI_escapes
2.0
CRAP 3.6
« lib/Badger/Data/Facet/Text/Pattern.pm ↑ Summary lib/Badger/Log.pm »
1 #========================================================================
2 #
3 # Badger::Rainbow
4 #
5 # DESCRIPTION
6 # Colour-related functionality, used primarily for debugging.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Rainbow;
14  
15 70 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Badger::Debug ':debug';
17 use Badger::Class
18 70 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 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # can't import this via Badger::Debug as it depends on Badger::Rainbow
44 70 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Rainbow::DEBUG 70 0 70
45  
46 sub _export_ANSI_colours {
47 142 ▶ my ($class, $target, $symbol, $more_symbols) = @_;
Subroutine _export_ANSI_colours: called CC=6 SCAR=18.2
48 142 my $ansi = ANSI_colours;
49 142 ▶ my $cols = shift(@$more_symbols)
Condition33%1/3
shift @$more_symbols || croak(q[You didn't specify any ANSI colours to import])
l!l&&r!l&&!r
14200
MC/DC0%0/2
shift @$more_symbols || croak(q[You didn't specify any ANSI colours to import])
shift @$more_symbols croak(q[You didn't specify any ANSI colours to import])
Truth table33%1/3
A || B
A= shift @$more_symbolsB= croak(q[You didn't specify any ANSI colours to import])
ABresult
1X1
011
000
50 || croak "You didn't specify any ANSI colours to import";
51  
52 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 142 ▶ $cols = [ keys %$ansi ]
Branch: 1 branch
BranchTrueFalseTotal
if $cols eq "all" 1 141 142
55 if $cols eq ALL;
56  
57 142 ▶ $cols = [ split(DELIMITER, $cols) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cols eq "ARRAY" 141 1 142
58 unless ref $cols eq ARRAY;
59  
60 142 foreach my $col (@$cols) {
61 # $class->debug("Exporting ANSI clolour $col to $target\n") if $DEBUG;
62 647 ▶ my $val = $ansi->{ $col }
Condition33%1/3
$ansi->{$col} || croak("Invalid ANSI colour specified to import: $col")
l!l&&r!l&&!r
64700
MC/DC0%0/2
$ansi->{$col} || croak("Invalid ANSI colour specified to import: $col")
$ansi->{$col} croak("Invalid ANSI colour specified to import: $col")
Truth table33%1/3
A || B
A= $ansi->{$col}B= croak("Invalid ANSI colour specified to import: $col")
ABresult
1X1
011
000
63 || croak "Invalid ANSI colour specified to import: $col";
64 647 *{ $target.PKG.$col } = sub(@) {
65 3 ▶ ANSI_escape($val, @_)
Subroutine __ANON__: called CC=1 SCAR=0.0
66 647 };
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 ▶ my $attr = shift;
Subroutine ANSI_escape: called CC=2 SCAR=7.0
Pod documented
79 3 my $text = join('', grep { defined $_ } @_);
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 ▶ s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx
Branch: 1 branch
BranchTrueFalseTotal
s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx ? : 0 3 3
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 ▶ my $text = join('', grep { defined $_ } @_);
Subroutine strip_ANSI_escapes: not called CC=1 SCAR=6.9
Pod documented
95 0 $text =~ s/ \e .*? m//gx;
96 0 return $text;
97 }
98  
99  
100  
101  
102 70 1;
103  
104 __END__
« lib/Badger/Data/Facet/Text/Pattern.pm ↑ Summary lib/Badger/Log.pm »