Skip to content

Commit

Permalink
Make debug mode faster
Browse files Browse the repository at this point in the history
  • Loading branch information
titsuki committed Jan 2, 2020
1 parent 4ad6782 commit dfcc86c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 1 addition & 3 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
"resources" : [ ],
"source-url" : "git://github.com/titsuki/raku-Chart-Gnuplot.git",
"tags" : [ ],
"test-depends" : [
"IO::Capture::Simple"
],
"test-depends" : [ ],
"version" : "0.0.16"
}
19 changes: 8 additions & 11 deletions lib/Chart/Gnuplot.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ has $.debug;
has %.options;
has Int $!num-plot;
has $!promise;
has @!msg-pool;
has $!msg-channel;
has $!msg-supply;
has &!writer;

has Chart::Gnuplot::Arrow $!arrow handles <arrow>;
Expand All @@ -38,7 +39,7 @@ has Chart::Gnuplot::Tics $!tics handles <xtics ytics ztics x2tics y2tics cbtics>
has Chart::Gnuplot::Timestamp $!timestamp handles <timestamp>;
has Chart::Gnuplot::Title $!title handles <title>;

submethod BUILD(:$terminal!, Str :$filename, :$!persist = True, :$!debug = False, :&!writer? = -> $msg { self.command: $msg }) {
submethod BUILD(:$terminal!, Str :$filename, :$!persist = True, :$!debug = False, :&!writer? = -> $msg { self.command: $msg }, :$stderr = $*ERR) {
my $HOME = qq:x/echo \$HOME/.subst(/\s*/,"",:g);
my $prefix = "$HOME/.p6chart-gnuplot";

Expand All @@ -47,8 +48,11 @@ submethod BUILD(:$terminal!, Str :$filename, :$!persist = True, :$!debug = False
$!gnuplot = Proc::Async.new(:w, "$prefix/bin/gnuplot", @opts.join(" "));

if $!debug {
$!gnuplot.stderr.act(-> $v { @!msg-pool.push($v); });
$!gnuplot.stdout.act(-> $v { @!msg-pool.push($v); });
$!msg-channel = Channel.new;
$!msg-supply = $!msg-channel.Supply;
$!gnuplot.stderr.act(-> $v { $!msg-channel.send($v); });
$!gnuplot.stdout.act(-> $v { $!msg-channel.send($v); });
$!msg-supply.tap(-> $v { $stderr.print: $v } );
} else {
$!gnuplot.stderr.act({;});
$!gnuplot.stdout.act({;});
Expand Down Expand Up @@ -313,13 +317,6 @@ method dispose {

method command(Str $command) {
try sink await $!gnuplot.say: $command;

if $!debug {
sleep .5 unless @!msg-pool;
$*ERR.print: $command ~ "\n"; # for IO::Capture::Simple
$*ERR.print: @!msg-pool.join;
@!msg-pool = ();
}
}

=begin pod
Expand Down
13 changes: 8 additions & 5 deletions t/02-terminal.t
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use v6;
use Test;
use Chart::Gnuplot;
use IO::Capture::Simple;

{
my $gnu = Chart::Gnuplot.new(:terminal("svg"), :filename("sample.svg"), :debug);
my $msg = capture_stderr({ $gnu.terminal("svg"); });
my $msg = "";
my $gnu = Chart::Gnuplot.new(:terminal("svg"), :filename("sample.svg"), :debug, :stderr(class { method print(*@args){ $msg ~= @args.join; }; method flush {} }));
$gnu.terminal("svg");
nok $msg ~~ /unknown/, "Chart::Gnuplot.terminal should accept \"svg\" as an argument.";
}

{
my $gnu = Chart::Gnuplot.new(:terminal("png"), :filename("sample.png"), :debug);
my $msg = capture_stderr({ $gnu.terminal("Perl6isFun"); });
my $msg;
my $gnu = Chart::Gnuplot.new(:terminal("png"), :filename("sample.png"), :debug, :stderr(class { method print(*@args){ $msg ~= @args.join; }; method flush{}}));
$gnu.terminal("Perl6isFun");
sleep .5;
say $msg;
ok $msg ~~ /unknown/, "Chart::Gnuplot.terminal shouldn't accept \"Perl6isFun\" as an argument.";
}

Expand Down

0 comments on commit dfcc86c

Please sign in to comment.