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
|
|
2058
|
use Carp; |
|
70
|
|
|
|
|
104
|
|
|
70
|
|
|
|
|
6462
|
|
16
|
|
|
|
|
|
|
#use Badger::Debug ':debug'; |
17
|
|
|
|
|
|
|
use Badger::Class |
18
|
70
|
|
|
|
|
1139
|
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
|
|
31590
|
}; |
|
70
|
|
|
|
|
178
|
|
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
|
|
321
|
my ($class, $target, $symbol, $more_symbols) = @_; |
48
|
142
|
|
|
|
|
193
|
my $ansi = ANSI_colours; |
49
|
142
|
|
33
|
|
|
345
|
my $cols = shift(@$more_symbols) |
50
|
|
|
|
|
|
|
|| croak "You didn't specify any ANSI colours to import"; |
51
|
|
|
|
|
|
|
|
52
|
70
|
|
|
70
|
|
477
|
no strict REFS; |
|
70
|
|
|
|
|
96
|
|
|
70
|
|
|
|
|
26736
|
|
53
|
|
|
|
|
|
|
|
54
|
142
|
100
|
|
|
|
316
|
$cols = [ keys %$ansi ] |
55
|
|
|
|
|
|
|
if $cols eq ALL; |
56
|
|
|
|
|
|
|
|
57
|
142
|
100
|
|
|
|
1372
|
$cols = [ split(DELIMITER, $cols) ] |
58
|
|
|
|
|
|
|
unless ref $cols eq ARRAY; |
59
|
|
|
|
|
|
|
|
60
|
142
|
|
|
|
|
332
|
foreach my $col (@$cols) { |
61
|
|
|
|
|
|
|
# $class->debug("Exporting ANSI clolour $col to $target\n") if $DEBUG; |
62
|
647
|
|
33
|
|
|
1254
|
my $val = $ansi->{ $col } |
63
|
|
|
|
|
|
|
|| croak "Invalid ANSI colour specified to import: $col"; |
64
|
647
|
|
|
|
|
2294
|
*{ $target.PKG.$col } = sub(@) { |
65
|
3
|
|
|
3
|
|
17
|
ANSI_escape($val, @_) |
66
|
647
|
|
|
|
|
1616
|
}; |
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
|
2
|
my $attr = shift; |
79
|
3
|
|
|
|
|
5
|
my $text = join('', grep { defined $_ } @_); |
|
3
|
|
|
|
|
9
|
|
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
|
|
|
|
5
|
s/ \e \[ ([1-9][\d;]*) m/\e[$1;${attr}m/gx |
|
3
|
|
|
|
|
17
|
|
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__ |