File Coverage

blib/lib/Data/HexDump/Range/Format.pm
Criterion Covered Total %
statement 27 66 40.9
branch 0 18 0.0
condition 0 15 0.0
subroutine 9 13 69.2
pod 1 3 33.3
total 37 115 32.1


line stmt bran cond sub pod time code
1              
2             package Data::HexDump::Range ; ## no critic (Modules::RequireFilenameMatchesPackage)
3              
4 2     2   7 use strict;
  2         1  
  2         45  
5 2     2   6 use warnings ;
  2         2  
  2         36  
6 2     2   5 use Carp ;
  2         2  
  2         123  
7              
8             BEGIN
9 0         0 {
10              
11 2         18 use Sub::Exporter -setup =>
12             {
13             exports => [ qw() ],
14             groups =>
15             {
16             all => [ qw() ],
17             }
18 2     2   7 };
  2         2  
19            
20 2     2   622 use vars qw ($VERSION);
  2     0   3  
  2         74  
21             }
22              
23             #-------------------------------------------------------------------------------
24              
25 2     2   6 use English qw( -no_match_vars ) ;
  2         3  
  2         6  
26              
27 2     2   504 use Readonly ;
  2         2  
  2         100  
28             Readonly my $EMPTY_STRING => q{} ;
29              
30 2     2   8 use Carp qw(carp croak confess) ;
  2         2  
  2         61  
31              
32 2     2   864 use Text::Colorizer ;
  2         5773  
  2         13  
33              
34             #-------------------------------------------------------------------------------
35              
36             =head1 NAME
37              
38             Data::HexDump::Range::Format - Handles formating for Data::HexDump::Range
39              
40             =head1 SUBROUTINES/METHODS
41              
42             Subroutines prefixed with B<[P]> are not part of the public API and shall not be used directly.
43              
44             =cut
45              
46             #-------------------------------------------------------------------------------
47              
48             sub get_bg_color
49             {
50              
51             =head2 [P] get_bg_color()
52              
53             Returns the default bg color
54              
55             I - None
56              
57             I - A string - a color according to the COLOR option and FORMAT
58              
59             I - None
60              
61             =cut
62              
63 0     0 0   my ($self) = @_ ;
64              
65 0           return $self->{COLORS}{$self->{FORMAT}}[-1] ;
66             }
67              
68             #-------------------------------------------------------------------------------
69              
70             sub get_default_color
71             {
72              
73             =head2 [P] get_default_color($color)
74              
75             Returns a color to use with a range that has none, only if $color is not defined
76              
77             my $default_color = $self->get_default_color($color) ;
78              
79             I - $color - scalar - a color name or undef
80              
81             I - A string - a color according to the COLOR option and FORMAT
82              
83             I - None
84              
85             =cut
86              
87 0     0 0   my ($self, $color) = @_ ;
88              
89 0           my $default_color ;
90              
91 0 0         if($self->{COLOR} eq 'cycle')
    0          
    0          
92             {
93 0   0       $default_color = $color || $self->{COLORS}{$self->{FORMAT}}[$self->{CURRENT_COLOR_INDEX}] ;
94            
95 0           $self->{CURRENT_COLOR_INDEX}++ ;
96 0 0         $self->{CURRENT_COLOR_INDEX} = 0 if $self->{CURRENT_COLOR_INDEX} >= @{$self->{COLORS}{$self->{FORMAT}}} ;
  0            
97             }
98             elsif($self->{COLOR} eq 'no_cycle')
99             {
100 0   0       $default_color = $color || $self->{COLORS}{$self->{FORMAT}}[-1] ;
101             }
102             elsif($self->{COLOR} eq 'bw')
103             {
104 0           $default_color = $self->{COLORS}{$self->{FORMAT}}[-1] ;
105             }
106             else
107             {
108 0           $self->{INTERACTION}{DIE}("Error: Invalid COLOR format.\n") ;
109             }
110            
111 0           return $default_color ;
112             }
113              
114             #-------------------------------------------------------------------------------
115              
116             sub format ## no critic (Subroutines::ProhibitBuiltinHomonyms)
117             {
118            
119             =head2 [P] format($line_data)
120              
121             Transform the line data into ANSI, ASCII or HTML
122              
123             I -
124              
125             =over 2
126              
127             =item * \%line_data - See L
128              
129             =back
130              
131             I - A dump in ANSI, ASCII or HTML.
132              
133             =cut
134              
135 0     0 1   my ($self, $line_data) = @_ ;
136              
137             #load user colors from file
138 0 0 0       if(defined $self->{COLOR_NAMES} && 'HASH' ne ref $self->{COLOR_NAMES})
139             {
140             my $colors = do $self->{COLOR_NAMES}
141 0 0         or $self->{INTERACTION}{DIE}("Error: Can't load color file '$self->{COLOR_NAMES}'.\n") ;
142              
143             'HASH' eq ref $colors
144 0 0         or $self->{INTERACTION}{DIE}("Error: Data not a Hash in '$self->{COLOR_NAMES}'.\n") ;
145              
146 0           $self->{COLOR_NAMES} = $colors ;
147             }
148              
149 0           for my $definition
150             (
151             #aliases looked up by colorizer module
152             ['ruler', 'lookup:white', 'lookup:white'],
153             ['offset', 'lookup:white', 'lookup:white'],
154             ['cumulative_offset', 'lookup:bright_black', 'lookup:bright_black'],
155             )
156             {
157 0           my ($name, $ansi_color, $html_color) = @{$definition} ;
  0            
158 0   0       $self->{COLOR_NAMES}{ANSI}{$name} //= $ansi_color ;
159 0   0       $self->{COLOR_NAMES}{HTML}{$name} //= $html_color ;
160             }
161              
162             my $colorizer = Text::Colorizer->new
163             (
164             COLORS => $self->{COLOR_NAMES},
165             FORMAT => $self->{FORMAT},
166 0           ) ;
167              
168 0           my @colored_lines ;
169              
170 0           my @fields = @{$self->{FIELDS_TO_DISPLAY}} ;
  0            
171 0           unshift @fields, 'INFORMATION', 'RULER' ;
172              
173 0           for my $line (@{$line_data})
  0            
174             {
175 0           for my $field (@fields)
176             {
177 0 0         if(exists $line->{$field})
178             {
179 0           for my $range (@{$line->{$field}})
  0            
180             {
181 0           push @colored_lines, $range->{"${field}_COLOR"}, $range->{$field} ,
182             }
183            
184 0           push @colored_lines, $EMPTY_STRING, q{ } ;
185             }
186             }
187            
188 0 0         push @colored_lines, $EMPTY_STRING, "\n" if $line->{NEW_LINE} ;
189             }
190              
191 0           return $colorizer->color(@colored_lines) ;
192             }
193              
194             #-------------------------------------------------------------------------------
195              
196             1 ;
197              
198             =head1 BUGS AND LIMITATIONS
199              
200             None so far.
201              
202             =head1 AUTHOR
203              
204             Nadim ibn hamouda el Khemir
205             CPAN ID: NKH
206             mailto: nadim@cpan.org
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             Copyright Nadim Khemir 2010-2012.
211              
212             This program is free software; you can redistribute it and/or
213             modify it under the terms of either:
214              
215             =over 4
216              
217             =item * the GNU General Public License as published by the Free
218             Software Foundation; either version 1, or (at your option) any
219             later version, or
220              
221             =item * the Artistic License version 2.0.
222              
223             =back
224              
225             =head1 SUPPORT
226              
227             You can find documentation for this module with the perldoc command.
228              
229             perldoc Data::HexDump::Range
230              
231             You can also look for information at:
232              
233             =over 4
234              
235             =item * AnnoCPAN: Annotated CPAN documentation
236              
237             L
238              
239             =item * RT: CPAN's request tracker
240              
241             Please report any bugs or feature requests to L .
242              
243             We will be notified, and then you'll automatically be notified of progress on
244             your bug as we make changes.
245              
246             =item * Search CPAN
247              
248             L
249              
250             =back
251              
252             =head1 SEE ALSO
253              
254             L
255              
256             =cut