-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDLGraphicsGnuplot.pm
More file actions
120 lines (98 loc) · 3.01 KB
/
PDLGraphicsGnuplot.pm
File metadata and controls
120 lines (98 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Devel::IPerl::Plugin::PDLGraphicsGnuplot;
# ABSTRACT: IPerl plugin to make PDL::Graphics::Gnuplot plots displayable
use strict;
use warnings;
use PDL::Graphics::Gnuplot;
use Role::Tiny;
use Devel::IPerl::Display::SVG;
use Devel::IPerl::Display::PNG;
our $IPerl_compat = 1;
our $IPerl_format = 'SVG';
our $IPerl_format_info = {
'SVG' => { suffix => '.svg', displayable => 'Devel::IPerl::Display::SVG' },
'PNG' => { suffix => '.png', displayable => 'Devel::IPerl::Display::PNG' },
};
sub register {
Role::Tiny->apply_roles_to_package( 'PDL::Graphics::Gnuplot', q(Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerlRole) );
}
{
package
Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerlRole;
use Moo::Role;
use Capture::Tiny qw(capture_stderr capture_stdout);
use Path::Tiny;
around new => sub {
my $orig = shift;
my $gpwin = $orig->(@_);
if( $Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerl_compat ) {
# We turn on dumping so that the plot does not go to an actual
# terminal (a "dry-run"). This is so that we can actually have
# the output go to a terminal later when
# C<iperl_data_representations> is called.
capture_stderr(sub {
# capture to avoid printing out the dumping warning
local $SIG{__WARN__} = sub {
warn $_[0] unless $_[0] =~ m{
WARNING\s+-\s+dumping\s+ON
}xs;
};
$gpwin->option( dump => 1 );
});
}
return $gpwin;
};
around _printGnuplotPipe => sub {
my $orig = shift;
if( $Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerl_compat ) {
my ($dump_stdout, $dump_stderr);
local *STDOUT;
#local *STDERR;
open STDOUT, '>', \$dump_stdout or die "Can't open STDOUT: $!";
#open STDERR, '>', \$dump_stderr or die "Can't open STDERR $!";
local $SIG{__WARN__} = sub {
warn $_[0] unless $_[0] =~ m{
\(killed\s+gnuplot\)
}xs;
};
return $orig->(@_);
} else {
return $orig->(@_);
}
};
sub iperl_data_representations {
my ($gpwin) = @_;
return unless $Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerl_compat;
return unless $gpwin->{replottable};
capture_stderr(sub {
# capture to avoid printing out the dumping warning
local $SIG{__WARN__} = sub {
warn $_[0] unless $_[0] =~ m{
WARNING\s+-\s+dumping\s+OFF
}xs;
};
$gpwin->option( dump => 0 );
});
my $format = $Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerl_format;
my $format_info = $Devel::IPerl::Plugin::PDLGraphicsGnuplot::IPerl_format_info;
die "Format $format not supported" unless exists $format_info->{$format};
my $suffix = $format_info->{$format}{suffix};
my $displayable = $format_info->{$format}{displayable};
my $tmp_filename = Path::Tiny->tempfile( SUFFIX => $suffix );
capture_stderr( sub {
$gpwin->option( hardcopy => "$tmp_filename" );
$gpwin->replot();
$gpwin->close;
});
capture_stderr( sub {
# capture to avoid printing out the dumping warning
local $SIG{__WARN__} = sub {
warn $_[0] unless $_[0] =~ m{
WARNING\s+-\s+dumping\s+OFF
}xs;
};
$gpwin->option( dump => 0 );
} );
return $displayable->new( filename => $tmp_filename )->iperl_data_representations;
}
}
1;