File Coverage

blib/lib/Data/Coloured.pm
Criterion Covered Total %
statement 13 26 50.0
branch 0 4 0.0
condition n/a
subroutine 5 9 55.5
pod 0 4 0.0
total 18 43 41.8


line stmt bran cond sub pod time code
1             package Data::Coloured;
2             BEGIN {
3 1     1   583 $Data::Coloured::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: Visualize random ASCII data streams
6             $Data::Coloured::VERSION = '0.001';
7 1     1   6 use strict;
  1         1  
  1         30  
8 1     1   3 use warnings;
  1         1  
  1         38  
9 1     1   3 use Exporter 'import';
  1         2  
  1         27  
10 1     1   611 use Term::ANSIColor qw( colored );
  1         5800  
  1         630  
11              
12             our @EXPORT_OK = qw( pc c );
13             our @EXPORT = qw( coloured poloured );
14              
15             our %control = (qw(
16             0 NUL
17             1 SOH
18             2 STX
19             3 ETX
20             4 EOT
21             5 ENQ
22             6 ACK
23             7 BEL
24             8 BS
25             9 TAB
26             10 LF
27             11 VT
28             12 FF
29             13 CR
30             14 SO
31             15 SI
32             16 DLE
33             17 DC1
34             18 DC2
35             19 DC3
36             20 DC4
37             21 NAK
38             22 SYN
39             23 ETB
40             24 CAN
41             25 EM
42             26 SUB
43             27 ESC
44             28 FS
45             29 GS
46             30 RS
47             31 US
48             127 DEL
49             ));
50              
51             our %colours = (qw(
52             control bright_red
53             print bright_yellow
54             binary bright_cyan
55             ));
56              
57             our @control_delimiter = qw( [ ] );
58             our @binary_delimiter = qw( < > );
59              
60             sub poloured {
61 0     0 0   print coloured(@_);
62             }
63 0     0 0   sub pc { poloured(@_) }
64              
65             sub coloured {
66 0     0 0   my $data = join('',@_);
67 0           my @chars = split(//,$_[0]);
68 0           my $output = "";
69 0           for my $char (@chars) {
70 0           my $chr = ord($char);
71 0 0         if (defined $control{$chr}) {
    0          
72 0           $output .= colored($control_delimiter[0].$control{$chr}.$control_delimiter[1],$colours{control});
73             } elsif ($char =~ /[ -~]/) {
74 0           $output .= colored($char,$colours{print});
75             } else {
76 0           $output .= colored($binary_delimiter[0].unpack('H*',$char).$binary_delimiter[1], $colours{binary});
77             }
78             }
79 0           return $output;
80             }
81 0     0 0   sub c { coloured(@_) }
82              
83             1;
84              
85             __END__