File Coverage

blib/lib/Data/Printer/Filter/PDL.pm
Criterion Covered Total %
statement 64 65 98.4
branch 6 8 75.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 3 0.0
total 81 88 92.0


line stmt bran cond sub pod time code
1             package Data::Printer::Filter::PDL;
2             $Data::Printer::Filter::PDL::VERSION = '1.001';
3 1     1   1705 use strict;
  1         3  
  1         29  
4 1     1   7 use warnings;
  1         1  
  1         27  
5              
6 1     1   6 use Data::Printer::Filter;
  1         3  
  1         9  
7              
8             filter 'PDL', \&pdl_filter;
9             filter 'PDL::Char', \&pdl_filter;
10             filter 'PDL::Complex', \&pdl_filter;
11              
12 1     1   99 use constant MSG_TOOLONG => 'too long to print';
  1         2  
  1         155  
13              
14 1         953 use constant COLORS => {
15             filter_pdl_data_number => { fallback => 'number', color => 'magenta' },
16             filter_pdl_data_brackets => { fallback => 'brackets', color => 'bright_green' },
17              
18             filter_pdl_data_message => { fallback => 'string', color => 'cyan' },
19              
20             filter_pdl_type => { fallback => 'class', color => 'black on_red' },
21              
22             filter_pdl_shape_number => { fallback => 'array', color => 'cyan' },
23             filter_pdl_shape_brackets => { fallback => 'brackets', color => 'bright_green' },
24              
25             filter_pdl_nelem => { color => 'bright_yellow' },
26              
27             filter_pdl_min => { color => 'bright_red' },
28             filter_pdl_max => { color => 'bright_blue' },
29              
30             filter_pdl_bad_true => { color => 'red' },
31             filter_pdl_bad_false => { color => 'green' },
32 1     1   8 };
  1         9  
33              
34             # _get_color_pair
35             sub _gcp {
36 51     51   178 my ($ddp, $name) = @_;
37 51         82 my $color_spec = COLORS()->{$name};
38 51         75 my $default_color = $color_spec->{color};
39 51 100 66     134 if( exists $color_spec->{fallback} && ( my $fallback_color = $ddp->theme->color_for($color_spec->{fallback}) ) ) {
40 41         415 $default_color = $fallback_color;
41             }
42 51         155 ( $name, $default_color );
43             }
44              
45             sub pdl_filter {
46 2     2 0 13753 my ($self, $ddp) = @_;
47              
48             ################################################
49             # Get Data, Build Structure #
50             # add new things as [ tag => data ] to @data #
51             ################################################
52 2         5 my @data;
53 2 100       16 if($self->nelem <= $PDL::toolongtoprint) { # NOTE this logic is already in PDL, so this may be superfluous
54 1         6 (my $string = $self->string) =~ s,^\n|\n$,,gs;
55             # TODO if PDL::Char also show $p->PDL::string()
56 1         1563 push @data, [ 'Data' => color_pdl_string($ddp, $string, 'data') ];
57             } else {
58 1         4 push @data, [ 'Data' => $ddp->maybe_colorize(MSG_TOOLONG, _gcp($ddp, 'filter_pdl_data_message') ) ];
59             }
60              
61             # type
62 2         17 push @data, [ Type => $ddp->maybe_colorize($self->type->realctype, _gcp($ddp, 'filter_pdl_type') ) ];
63              
64             # shape
65 2         22 push @data, [ Shape => color_pdl_string($ddp, $self->shape->string, 'shape') ];
66              
67             # elements
68 2         21 push @data, [ Nelem => $ddp->maybe_colorize($self->nelem, _gcp($ddp, 'filter_pdl_nelem')) ];
69              
70             # min and max
71 2         20 my ($min, $max) = $self->minmax;
72 2         238 push @data, [ Min => $ddp->maybe_colorize($min, _gcp($ddp, 'filter_pdl_min') ) ];
73 2         18 push @data, [ Max => $ddp->maybe_colorize($max, _gcp($ddp, 'filter_pdl_max') ) ];
74              
75             # bad?
76 2         17 my $bad_flag = $self->badflag;
77 2         21 $self->check_badflag;
78 2         22 push @data, [ Badflag => color_bad_bool($ddp, $bad_flag) ];
79 2         24 push @data, [ 'Has Bads' => color_bad_bool($ddp, $self->badflag) ];
80 2         19 $self->badflag($bad_flag);
81              
82             #####################
83             # Format the Output #
84             #####################
85 2         6 my $data = $ddp->maybe_colorize( ref($self), 'class' );
86 2         15 $data .= $ddp->maybe_colorize( " {", 'brackets' );
87              
88 2         19 $ddp->indent;
89 2         13 $data .= $ddp->newline();
90              
91 2         19 my $max_tag_length = List::Util::max map { length $_->[0] } @data;
  16         31  
92 2         10 my $tag_format = $ddp->maybe_colorize( '%-' . $max_tag_length . 's' , 'hash') . ' : ';
93 2         25 (my $empty_tag = sprintf($tag_format, "")) =~ s,:, ,;
94             my @formatted =
95             map {
96 2         6 $_->[1] =~ s,\n,@{[$ddp->newline()]}$empty_tag,gs;
  16         43  
  11         89  
97 16         57 sprintf($tag_format, $_->[0]) . $_->[1]
98             }
99             @data;
100              
101 2         19 $data .= join $ddp->newline(), @formatted;
102              
103 2         25 $ddp->outdent;
104 2         7 $data .= $ddp->newline();
105 2         14 $data .= $ddp->maybe_colorize( "}", 'brackets' );
106              
107 2         21 return $data;
108             };
109              
110             sub color_pdl_string {
111 3     3 0 316 my ($ddp, $pdl, $type) = @_;
112 3         20 $pdl =~ s/\[(.*)\]/"[".$ddp->maybe_colorize($1, _gcp($ddp, "filter_pdl_${type}_number"))."]"/eg;
  12         101  
113 3         35 $pdl =~ s/^([^\[]+)$/$ddp->maybe_colorize($1, _gcp($ddp, "filter_pdl_${type}_number"))/eg;
  0         0  
114 3         21 $pdl =~ s/^\s*\[|\]$/$ddp->maybe_colorize($&, _gcp($ddp, "filter_pdl_${type}_brackets"))/emg;
  26         256  
115 3         31 return $pdl;
116             }
117              
118             sub color_bad_bool {
119 4     4 0 7 my ($ddp, $bool) = @_;
120 4 50       34 $ddp->maybe_colorize(
    50          
121             $bool ? "Yes" : "No",
122             _gcp($ddp, $bool ? 'filter_pdl_bad_true' : 'filter_pdl_bad_false' )
123             )
124             }
125              
126              
127             1;
128              
129             __END__