My/Color.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 96 CC 5 SCAR 24.3
CC
5
Cov
0%
apply_color
30.0
_get_is_escape_or_data
2.0
_get_background_colors
2.0
CRAP 11.3
1 package My::Color;
2  
3 use strict;
4 use warnings;
5 use feature qw/ say /;
6 use parent qw/ Exporter /;
7  
8 # Text Color
9 our $RESTORE = "\e[0m";
10 our $RED = "\e[31m";
11 our $GREEN = "\e[32m";
12 our $YELLOW = "\e[33m";
13 our $VIOLET = "\e[35m";
14 our $TEAL = "\e[36m";
15 our $RED_BOLD = "\e[91m";
16 our $GREEN_BOLD = "\e[92m";
17 our $YELLOW_BOLD = "\e[93m";
18 our $VIOLET_BOLD = "\e[95m";
19 our $TEAL_BOLD = "\e[96m";
20 our $GREY = "\e[90m";
21  
22 # Background Color
23 our $BG_RED = "\e[41m";
24 our $BG_GREEN = "\e[42m";
25 our $BG_YELLOW = "\e[43m";
26 our $BG_BLUE = "\e[105m";
27 our $BG_VIOLET = "\e[45m";
28 our $BG_TEAL = "\e[46m";
29 our $BG_GREY = "\e[100m";
30  
31 # Properties
32 our $BOLD = "\e[1m";
33 our $UNDERLINE = "\e[4m";
34 our $BLINK = "\e[5m";
35  
36 # Combinations
37 our $WARN = "$BLINK$YELLOW";
38 our $DIE = "$BLINK$RED";
39  
40 # Export
41 our @EXPORT = qw/ /;
42 our @EXPORT_OK = qw/
43 apply_color
44 $RESTORE
45 $RED
46 $GREEN
47 $YELLOW
48 $VIOLET
49 $TEAL
50 $RED_BOLD
51 $GREEN_BOLD
52 $YELLOW_BOLD
53 $VIOLET_BOLD
54 $TEAL_BOLD
55 $GREY
56 $BG_RED
57 $BG_GREEN
58 $BG_YELLOW
59 $BG_BLUE
60 $BG_VILET
61 $BG_TEAL
62 $BG_GREY
63 $BOLD
64 $UNDERLINE
65 $BLINK
66 $WARN
67 $DIE
68 /;
69 our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], );
70  
71 =head2 apply_color
72  
73 Apply a spefic color to a string.
74  
75 =cut
76  
77 sub apply_color {
78 my ( $whole, $parts, $opts ) = @_;
79 my $is_escape_or_data = _get_is_escape_or_data();
80 my @colors = _get_custom_colors();
81 my $default_color = pop @colors; # Last color is default
82 my $case =
83 $opts->{case_sensitive} ? "(?-i)" : "(?i)"; # Case Insensitive by default
84 my @keys =
85 map { ref() ? $_ : ( $opts->{regex} ? qr/$case$_/ : qr/$case\Q$_/ ) }
86 @$parts; # No regex by default
87 #@colors = _normalize_color_codes( @colors );
88  
89 our $last_esc = "";
90  
91 for my $index ( 0 .. $#keys ) {
92 my $key = $keys[$index];
93 my $color = ( $index < @colors ) ? $colors[$index] : $default_color;
94  
95 $whole =~ s{$is_escape_or_data}
96 {
97 local $_ = $1 // "";
98 $last_esc = $2 // $last_esc;
99 s/($key)/$color$1$RESTORE$last_esc/g;
100 $_;
101 }gex;
102 }
103  
104 return $whole;
105 }
106  
107 sub _get_is_escape_or_data {
108 my $esc = qr/ \033\[ [\d;]+m /x;
109 my $data = qr/ (?: (?!$esc) . )+ /x; # Not escape
110 my $or = qr/ ($data)|($esc\K) /x; # Must therefore match one of them
111  
112 $or;
113 }
114  
115 sub _get_background_colors {
116 ( $BG_RED, $BG_GREEN, $BG_YELLOW, $BG_BLUE, $BG_VIOLET, $BG_TEAL, );
117 }
118  
119 sub _get_foreground_colors {
120 (
121 $RED, $GREEN, $YELLOW, $VIOLET, $TEAL,
122 $RED_BOLD, $GREEN_BOLD, $YELLOW_BOLD, $VIOLET_BOLD, $TEAL_BOLD,
123 );
124 }
125  
126 sub _get_custom_colors {
127 ( $BG_GREY, $RED, $GREEN, $YELLOW, $VIOLET, );
128 }
129  
130 sub _init {
131  
132 system ''; # Enable escape code ?!
133 }
134  
135  
136 _init();
137  
138  
139 sub _normalize_color_codes {
140 my @raw_colors = @_;
141  
142 my $front = qr/ \\033\[ /x;
143 my $end = qr/ m /x;
144 my @colors = map {
145 s/$front//g;
146 s/$end//g;
147 "\033[${_}m";
148 } split " ", shift @raw_colors;
149  
150 @colors;
151 }
152  
153  
154 1;