File Coverage

lib/App/colourhexdump.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 1     1   764 use 5.006; # our
  1         2  
  1         36  
2 1     1   4 use strict;
  1         1  
  1         31  
3 1     1   3 use warnings;
  1         1  
  1         63  
4              
5             package App::colourhexdump;
6              
7             our $VERSION = '1.000001';
8              
9             # ABSTRACT: HexDump, but with character-class highlighting.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 1     1   179 use Moose qw( has with );
  0            
  0            
14             use MooseX::Getopt::Dashes 0.37;
15             with qw( MooseX::Getopt::Dashes );
16              
17             use Getopt::Long::Descriptive;
18             use Term::ANSIColor 3.00 qw( colorstrip );
19             use App::colourhexdump::Formatter;
20             use namespace::autoclean;
21              
22             has colour_profile => (
23             metaclass => 'Getopt',
24             isa => 'Str',
25             is => 'rw',
26             default => 'DefaultColourProfile',
27             cmd_aliases => [qw/ C color-profile /],
28             documentation => 'Backend to use for colour highlighting (DefaultColourProfile)',
29             );
30              
31             has row_length => (
32             metaclass => 'Getopt',
33             isa => 'Int',
34             is => 'ro',
35             default => 32,
36             cmd_aliases => [qw/ r row /],
37             documentation => 'Number of bytes per display row (32).',
38              
39             );
40              
41             has chunk_length => (
42             metaclass => 'Getopt',
43             isa => 'Int',
44             is => 'rw',
45             default => 4,
46             cmd_aliases => [qw/ x chunk /],
47             documentation => 'Number of bytes per display hex display group (4).',
48             );
49              
50             has _files => (
51             metaclass => 'Getopt',
52             isa => 'ArrayRef[Str]',
53             is => 'rw',
54             default => sub { [] },
55             cmd_flag => 'file',
56             cmd_aliases => [qw/ f /],
57             documentation => 'Add a file to the list of files to process. \'-\' for STDIN.',
58              
59             );
60              
61             has 'show_file_prefix' => (
62             metaclass => 'Getopt',
63             isa => 'Bool',
64             is => 'rw',
65             default => 0,
66             documentation => 'Enable printing the filename on the start of every line ( off ).',
67              
68             );
69             has 'show_file_heading' => (
70             metaclass => 'Getopt',
71             isa => 'Bool',
72             is => 'rw',
73             default => 0,
74             documentation => 'Enable printing the filename before the hexdump output. ( off ).',
75             );
76             has 'colour' => (
77             metaclass => 'Getopt',
78             isa => 'Bool',
79             is => 'rw',
80             default => 1,
81             cmd_aliases => [qw/ c color /],
82             documentation => 'Enable coloured output ( on ). --no-colour to disable.',
83              
84             );
85              
86              
87              
88              
89              
90              
91              
92              
93              
94             sub BUILD {
95             my $self = shift;
96             push @{ $self->_files() }, @{ $self->extra_argv };
97             return $self;
98              
99             }
100              
101              
102              
103              
104              
105              
106              
107              
108              
109             sub get_filehandle {
110             my ( undef, $filename ) = @_;
111             if ( q[-] eq $filename ) {
112             return \*STDIN;
113             }
114             require Carp;
115             open my $fh, '<', $filename or Carp::confess("Cant open $_ , $!");
116             return $fh;
117             }
118              
119              
120              
121              
122              
123              
124              
125              
126              
127             sub run {
128             my $self = shift;
129             if ( not @{ $self->_files } ) {
130             push @{ $self->_files }, q[-];
131             }
132             ## no critic ( Variables::RequireLocalizedPunctuationVars )
133             local $ENV{ANSI_COLORS_DISABLED} = $ENV{ANSI_COLORS_DISABLED};
134             if ( not $self->colour ) {
135             $ENV{ANSI_COLORS_DISABLED} = 1;
136             }
137             for ( @{ $self->_files } ) {
138             my $prefix = q{};
139             if ( $self->show_file_prefix ) {
140             $prefix = $_;
141             }
142             if ( length $prefix ) {
143             $prefix .= q{:};
144             }
145             ## no critic ( RequireCheckedSyscalls );
146             if ( $self->show_file_heading ) {
147             print qq{- Contents of $_ --\n};
148             }
149             my $formatter = App::colourhexdump::Formatter->new(
150             colour_profile => $self->colour_profile,
151             row_length => $self->row_length,
152             chunk_length => $self->chunk_length,
153             );
154              
155             $formatter->format_foreach_in_fh(
156             $self->get_filehandle($_),
157             sub {
158             print $prefix . shift;
159             },
160             );
161             }
162             return 1;
163             }
164              
165             __PACKAGE__->meta->make_immutable;
166             no Moose;
167             1;
168              
169             __END__
170              
171             =pod
172              
173             =encoding UTF-8
174              
175             =head1 NAME
176              
177             App::colourhexdump - HexDump, but with character-class highlighting.
178              
179             =head1 VERSION
180              
181             version 1.000001
182              
183             =head1 SYNOPSIS
184              
185             usage: colourhexdump [-?Ccfrx] [long options...]
186             -? --usage --help Prints this usage information.
187             --color-profile -C --colour-profile Backend to use for colour highlighting (DefaultColourProfile)
188             --row -r --row-length Number of bytes per display row (32).
189             --chunk -x --chunk-length Number of bytes per display hex display group (4).
190             -f --file Add a file to the list of files to process. '-' for STDIN.
191             --show-file-prefix Enable printing the filename on the start of every line ( off ).
192             --show-file-heading Enable printing the filename before the hexdump output. ( off ).
193             --color -c --colour Enable coloured output ( on ). --no-colour to disable.
194              
195             It can be used like so
196              
197             colourhexdump file/a.txt file/b.txt -- --this-is-treated-like-a-file.txt
198              
199             If you are using an HTML-enabled POD viewer, you should see a screenshot of this in action:
200              
201             ( Everyone else can visit L<http://kentnl.github.io/App-colourhexdump/media/Screenshot.png> )
202              
203             =for html <center><img src="http://kentnl.github.io/App-colourhexdump/media/Screenshot.png" alt="Screenshot with explanation of colours" width="826" height="838"/></center>
204              
205             =head1 METHODS
206              
207             =head2 BUILD
208              
209             This just pushes extra_argv from getopt into the files list.
210              
211             B<INTERNAL>
212              
213             =head2 get_filehandle
214              
215             my $fh = $self->get_filehandle( $filename_or_stdindash );
216              
217             B<INTERNAL>
218              
219             =head2 run
220              
221             Run the app.
222              
223             App::colourhexdump->new_with_options()->run();
224              
225             =head1 AUTHOR
226              
227             Kent Fredric <kentnl@cpan.org>
228              
229             =head1 COPYRIGHT AND LICENSE
230              
231             This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>.
232              
233             This is free software; you can redistribute it and/or modify it under
234             the same terms as the Perl 5 programming language system itself.
235              
236             =cut