File Coverage

blib/lib/Getopt/EX/Colormap.pm
Criterion Covered Total %
statement 52 81 64.2
branch 3 18 16.6
condition 1 4 25.0
subroutine 15 19 78.9
pod 4 4 100.0
total 75 126 59.5


line stmt bran cond sub pod time code
1             package Getopt::EX::Colormap;
2              
3             our $VERSION = "3.03";
4              
5 9     9   413599 use v5.14;
  9         27  
6 9     9   1669 use utf8;
  9         272  
  9         61  
7              
8 9     9   295 use Exporter 'import';
  9         27  
  9         732  
9             our @EXPORT_OK = (
10             qw( colorize colorize24 ansi_code ansi_pair csi_code ),
11             qw( colortable colortable6 colortable12 colortable24 ),
12             );
13             our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
14              
15 9     9   42 use Carp;
  9         33  
  9         658  
16 9     9   2577 use Data::Dumper;
  9         34643  
  9         634  
17             $Data::Dumper::Sortkeys = 1;
18              
19 9     9   56 use List::Util qw(min max first);
  9         22  
  9         655  
20              
21 9     9   1988 use parent 'Getopt::EX::LabeledParam';
  9         1392  
  9         46  
22 9     9   3824 use Getopt::EX::Util;
  9         22  
  9         488  
23 9     9   46 use Getopt::EX::Func qw(callable);
  9         13  
  9         363  
24              
25 9     9   5183 use Term::ANSIColor::Concise qw(:all);
  9         544500  
  9         1754  
26             {
27 9     9   103 no strict 'refs';
  9         16  
  9         321  
28 9     9   41 no warnings 'once';
  9         19  
  9         9184  
29             *colorize = \&ansi_color;
30             *colorize24 = \&ansi_color_24;
31             for my $name (
32             qw( NO_NO_COLOR
33             NO_COLOR
34             RGB24
35             LINEAR_256
36             LINEAR_GRAY
37             NO_RESET_EL
38             SPLIT_ANSI
39             )) {
40             *{$name} = *{"Term::ANSIColor::Concise::$name"};
41             }
42             }
43              
44             #use Term::ANSIColor::Concise::Table qw(:all);
45             sub AUTOLOAD {
46 2     2   3082 my $sub = our $AUTOLOAD =~ s/.*:://r;
47 2 50       227 return if $sub eq 'DESTROY';
48 0 0       0 if ($sub =~ /^colortable(?:|6|12|24)$/) {
49             require Term::ANSIColor::Concise::Table
50 0 0       0 and Term::ANSIColor::Concise::Table->import(':all');
51 0         0 goto &$sub;
52             }
53 0         0 die "Invalid call for $sub().\n";
54             }
55              
56             our $NO_NO_COLOR //= $ENV{GETOPTEX_NO_NO_COLOR};
57             our $NO_COLOR //= !$NO_NO_COLOR && defined $ENV{NO_COLOR};
58             our $RGB24 //= $ENV{COLORTERM}//'' eq 'truecolor' || $ENV{GETOPTEX_RGB24};
59             our $LINEAR_256 //= $ENV{GETOPTEX_LINEAR_256};
60             our $LINEAR_GRAY //= $ENV{GETOPTEX_LINEAR_GRAY};
61             our $NO_RESET_EL //= $ENV{GETOPTEX_NO_RESET_EL};
62             our $SPLIT_ANSI //= $ENV{GETOPTEX_SPLIT_ANSI};
63              
64             sub new {
65 2     2 1 466684 my $class = shift;
66 2         72 my $obj = $class->SUPER::new;
67 2         9 my %opt = @_;
68              
69 2         13 $obj->{CACHE} = {};
70 2   50     17 $opt{CONCAT} //= "^"; # Reset character for LabeledParam object
71 2         4 $opt{RESET} = '@';
72 2         15 $obj->configure(%opt);
73              
74 2         6 $obj;
75             }
76              
77             sub index_color {
78 0     0 1 0 my $obj = shift;
79 0         0 my $index = shift;
80 0         0 my $text = shift;
81              
82 0         0 my $list = $obj->{LIST};
83 0 0       0 if (@$list) {
84 0         0 $text = $obj->color($list->[$index % @$list], $text, $index);
85             }
86 0         0 $text;
87             }
88              
89             sub color {
90 5     5 1 34 my $obj = shift;
91 5         8 my $color = shift;
92 5         9 my $text = shift;
93              
94 5         10 my $map = $obj->{HASH};
95 5 50       16 my $c = exists $map->{$color} ? $map->{$color} : $color;
96              
97 5 50       13 return $text unless $c;
98              
99 5         21 cached_ansi_color($obj->{CACHE}, $c, $text);
100             }
101              
102             sub colormap {
103 0     0 1   my $obj = shift;
104 0           my %opt = (
105             name => "--newopt",
106             option => "--colormap",
107             sort => "length",
108             @_
109             );
110              
111 0           my $hash = $obj->{HASH};
112             join "\n", (
113             "option $opt{name} \\",
114 0           do {
115 0 0         my $maxlen = $opt{noalign} ? "" : do {
116 0           max map { length } keys %{$hash};
  0            
  0            
117             };
118 0           my $format = "\t%s %${maxlen}s=%s \\";
119 0           my $compare = do {
120 0 0         if ($opt{sort} eq "length") {
121 0 0   0     sub { length $a <=> length $b or $a cmp $b };
  0            
122             } else {
123 0     0     sub { $a cmp $b };
  0            
124             }
125             };
126             map {
127 0   0       sprintf $format, $opt{option}, $_, $hash->{$_} // "";
128 0           } sort $compare keys %{$hash};
  0            
129             },
130             "\t\$\n",
131             );
132             }
133              
134             1;
135              
136             __END__