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