Toolkit/Colors.pm

filter: Statementstmt 92.8% 26 / 28 Branchbran 50.0% 2 / 4 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 77.5% 38 / 49 CC 5 SCAR 15.2
CC
5
Cov
78%
color
8.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.6
« Toolkit/Masks.pm ↑ Summary Toolkit/Parser.pm »
1 package IRC::Toolkit::Colors;
2 3 $IRC::Toolkit::Colors::VERSION = '0.092002';
3 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT = qw/
8 color
9 has_color
10 strip_color
11 /;
12  
13 3 our %COLORS = (
14 NORMAL => "\x0f",
15  
16 BOLD => "\x02",
17 UNDERLINE => "\x1f",
18 REVERSE => "\x16",
19 ITALIC => "\x1d",
20  
21 WHITE => "\x0300",
22 BLACK => "\x0301",
23 BLUE => "\x0302",
24 GREEN => "\x0303",
25 RED => "\x0304",
26 BROWN => "\x0305",
27 PURPLE => "\x0306",
28 ORANGE => "\x0307",
29 YELLOW => "\x0308",
30 TEAL => "\x0310",
31 PINK => "\x0313",
32 GREY => "\x0314",
33 GRAY => "\x0314",
34  
35 LIGHT_BLUE => "\x0312",
36 LIGHT_CYAN => "\x0311",
37 CYAN => "\x0311",
38 LIGHT_GREEN => "\x0309",
39 LIGHT_GRAY => "\x0315",
40 LIGHT_GREY => "\x0315",
41 );
42  
43 2 ▶sub color {
Subroutine color: called CC=5 SCAR=20.9
Pod documented
44 2 my ($fmt, $str) = @_;
45 2 ▶ $fmt = uc($fmt || 'normal');
Condition50%1/2
$fmt || 'normal'
l!l
20
MC/DC0%0/1
$fmt || 'normal'
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
46 2 my $slct = $COLORS{$fmt};
47 2 ▶ unless (defined $slct) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slct) 0 2 2
48 0 carp "Invalid format $fmt passed to color()";
49 return $str || $COLORS{NORMAL}
50 0 ▶ }
Condition0%0/3
$str || $IRC::Toolkit::Colors::COLORS{'NORMAL'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$str || $IRC::Toolkit::Colors::COLORS{'NORMAL'}
$str $IRC::Toolkit::Colors::COLORS{'NORMAL'}
Truth table0%0/3
A || B
A= $strB= $IRC::Toolkit::Colors::COLORS{'NORMAL'}
ABresult
1X1
011
000
51 2 ▶ $str ? join('', $slct, $str, $COLORS{NORMAL}) : $slct
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 2 0 2
52 }
53  
54 2 ▶sub has_color {
Subroutine has_color: called CC=1 SCAR=0.0
Pod documented
55 2 !! ( $_[0] =~ /[\x02\x03\x04\x1B\x1f\x16\x1d\x11\x06]/ )
56 }
57  
58 1 ▶sub strip_color {
Subroutine strip_color: called CC=1 SCAR=0.0
Pod documented
59 1 my ($str) = @_;
60 # Borrowed from IRC::Utils;
61 # mIRC:
62 1 $str =~ s/\x03(?:,\d{1,2}|\d{1,2}(?:,\d{1,2})?)?//g;
63 # RGB:
64 1 $str =~ s/\x04[0-9a-fA-F]{0,6}//ig;
65 # ECMA-48:
66 1 $str =~ s/\x1B\[.*?[\x00-\x1F\x40-\x7E]//g;
67 # Formatting codes:
68 1 $str =~ s/[\x02\x1f\x16\x1d\x11\x06]//g;
69 # Cancellation code:
70 1 $str =~ s/\x0f//g;
71 1 $str
72 }
73  
74  
75 3 1;
76  
77 =pod
78  
79 =head1 NAME
80  
81 IRC::Toolkit::Colors - IRC color code utilities
82  
83 =head1 SYNOPSIS
84  
85 my $str = color('red', "red text") ." other text";
86  
87 if (has_color($str)) {
88 # ...
89 }
90  
91 my $stripped = strip_color($str);
92  
93 =head1 DESCRIPTION
94  
95 IRC utilities for adding color/formatting codes to a string.
96  
97 =head2 color
98  
99 my $code = color('red');
100 my $str = color('bold') . "bold text" . color() . "normal text";
101 my $str = color('bold', "bold text");
102  
103 Add mIRC formatting/color codes to a string.
104  
105 Valid formatting codes are:
106  
107 normal
108 bold
109 underline
110 reverse
111 italic
112  
113 Valid color codes are:
114  
115 white
116 black
117 blue
118 light_blue
119 cyan
120 green
121 light_green
122 red
123 brown
124 purple
125 orange
126 yellow
127 teal
128 pink
129 gray
130 light_gray
131  
132 =head2 has_color
133  
134 Returns true if the given string contains color or formatting codes.
135  
136 =head2 strip_color
137  
138 Strips all color and formatting codes from the string.
139  
140 =head1 AUTHOR
141  
142 Jon Portnoy <avenj@cobaltirc.org>
143  
144 Much of this code is primarily derived from L<IRC::Utils>, authored by HINRIK &
145 BINGOS.
146  
147 Licensed under the same terms as Perl.
148  
149 =cut
« Toolkit/Masks.pm ↑ Summary Toolkit/Parser.pm »