clumsyjedi
clumsyjedi

Reputation: 507

Capture the output of an interactive script in vim

I have a an interactive Perl script, which prints prompts to STDERR and reads lines from STDIN. The final output of this script is an IP address, printed to STDOUT. Here's a numpty version of such a script as an example.

my @pieces;
for (1..4) {
    print STDERR "please enter piece $_ of the IP:";        chomp(my $in = <>);
    push @pieces, $in;
}  
print join '.', @pieces;
print "\n";

I use the vim-fireplace vim plugin. This plugin has a feature where I can say:

:Connect nrepl://127.0.0.1:9999

I want to know how to configure vim so that when I issue a particular command, let's say:

:InteractiveConnect

it will do the following:

  1. Run the Perl script, allowing me to enter 4 pieces of the IP address.
  2. Capture the IP address output by the Perl script.
  3. Interpolate the IP address into the :Connect command
  4. Run the :Connect command.

A bit more info based on some of the responses:

If I call this script using:

:!/path/to/myscript.pl

Then it executes fine and I am able to see the result from it printed in the vim window, followed by

Press ENTER or type command to continue

If the output of the script is being saved in some buffer after execution via !, is it possible to get access to that buffer in vimscript and just capture the bit I want (the last line) with a regex?

Upvotes: 1

Views: 790

Answers (2)

joshtch
joshtch

Reputation: 301

Okay, there's probably a more elegant way to do this, but how about this:

function! <SID>InteractiveConnect()
    let tempfile=tempname()
    exe '!/path/to/your/script.pl >' . shellescape(tempfile)
    try
        exe 'Connect nrepl://' . readfile(tempfile, '', -1)[0]
    finally
        call delete(tempfile)
    endtry
endfunction
command! -nargs=0 InteractiveConnect call <SID>InteractiveConnect()

This creates a temporary file, writes to it with the script (using system() doesn't work because it doesn't wait for input), reads the last line in the tempfile to the Connect command, and then finally deletes the tempfile.

Upvotes: 2

Oberon
Oberon

Reputation: 3249

Maybe something like:

exec 'Connect nrepl://' . matchstr(system('your/script.pl'), '^.\+\%$')

(Untested.) This runs the script using system() then matches the output against the regular expression ^.\+\%$, (where \%$ means end-of-file; if your file is terminated with a newline, an additional \n might be neccessary before it) and feeds the matched str to the Connect command. .

Upvotes: 0

Related Questions