Skip to content

Instantly share code, notes, and snippets.

@4d47
Last active December 2, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4d47/bdc6a859f3628fb65e3d8648e074e62d to your computer and use it in GitHub Desktop.
Save 4d47/bdc6a859f3628fb65e3d8648e074e62d to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
my $cmd;
my $filter = RlwrapFilter->new;
$filter->help_text("Usage: rlwrap -z setpipe <command>\n"
."Intercept line starting with | and pipe further output thru it.\n"
."Use |<ret> to remove previously set pipe commands.\n");
$filter->input_handler(\&input);
$filter->output_handler(\&output);
$filter->run;
sub input {
if (/^\|\s*$/) {
undef $cmd;
}
elsif (/^\|/ ) {
$cmd = substr($_, 1);
return undef;
}
return $_;
}
sub output {
if (defined $cmd) {
# not reusing $fh because of buffring issues,
# that open/close should really be in &input.
open(my $fh, '|-', 'sh', ('-c', $cmd));
print $fh $_;
close $fh;
return undef;
}
return $_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment