Last active
December 2, 2017 16:08
-
-
Save 4d47/bdc6a859f3628fb65e3d8648e074e62d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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