Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Shell Command plugin for Padre

by gsiems (Deacon)
on Oct 09, 2009 at 22:21 UTC ( [id://800386]=CUFP: print w/replies, xml ) Need Help??

This Padre plugin allows users to type shell commands/scripts into the active document and run them. The output from the command is then inserted into the document after the command. See the POD for examples.

Caveat emptor: I've only tested this on Linux, but it *should* work on pretty much any *nix like system.

Update: After working more on this module and polishing things up a bit I've uploaded the new-and-improved Padre::Plugin::ShellCommand to CPAN.

--gsiems

package Padre::Plugin::ShellCommand; use 5.008; use strict; use warnings; use Padre::Constant (); use Padre::Current (); use Padre::Plugin (); use Padre::Wx (); use File::Temp qw/ tempfile /; our $VERSION = '0.10'; our @ISA = 'Padre::Plugin'; ##################################################################### # Padre::Plugin Methods sub plugin_name { 'ShellCommand'; } sub padre_interfaces { 'Padre::Plugin' => 0.43; } sub menu_plugins_simple { my ($self) = @_; ShellCommand => [ Wx::gettext("Run Command\tAlt+Shift+R") => sub { + $self->run_command() }, ]; } ##################################################################### # Custom Methods sub run_command { my ($self) = @_; my $editor = $self->current->editor; my $document = $editor->{Document}; $editor->Freeze; my $cmd = $editor->GetSelectedText(); my @cmd_out; my $insert_start = 0; if ($cmd) { my $cstart = $editor->GetSelectionStart(); my $cend = $editor->GetSelectionEnd(); my $start_line = $editor->LineFromPosition($cstart); my $end_line = $editor->LineFromPosition($cend); $insert_start = $editor->GetLineEndPosition($end_line); if ( $start_line == $end_line ) { @cmd_out = `$cmd 2>&1`; } else { my ( $fh, $filename ) = tempfile('PD_XXXXXXXX'); if ($fh) { print $fh $cmd; close $fh; `chmod u+x $filename`; # do we have a shebang? if ( $cmd =~ m/^\s*#!/ ) { @cmd_out = `./$filename 2>&1`; } else { @cmd_out = `sh $filename 2>&1`; } # In case of errors, we don't want the temp filename # showing up in the output as it is pretty ugly and # doesn't add anything to the conversation. if ($?) { $_ =~ s/^$filename/ERR/ for @cmd_out; } # We could use "tempfile (..., UNLINK => 1)" above but # then the temporary files hang around until Padre exi +ts. # Therefore we explicity delete the temporary file as +soon # as we are done with them. ( -f $filename ) && unlink $filename; } } } else { my $pos = $editor->GetCurrentPos; my $line = $editor->LineFromPosition($pos); my $first = $editor->PositionFromLine($line); my $last = $editor->GetLineEndPosition($line); $cmd = $editor->GetTextRange( $first, $last ); $insert_start = $last; @cmd_out = `$cmd 2>&1`; } if (@cmd_out) { my $text = "\n" . join '', @cmd_out; $editor->GotoPos($insert_start); $editor->insert_text($text); } $editor->Thaw; } 1; __END__ =pod =head1 NAME Padre::Plugin::ShellCommand - A Shell Command plugin =head1 DESCRIPTION This plugin takes shell commands from the active document and inserts +the ouput of the command into the document. If text is selected then the plugin will attemp to execute the selecte +d text. If no text is selected the the plugin will attempt to execute the curr +ent line as a command. =head1 EXAMPLES =head4 With no selected text Typing `echo $USER` on an otherwise blank line and invoking 'Run Comma +nd' without selecting anything would insert your username on the next line + down. echo $USER gsiems =head4 Selecting one or more words on a line By typing, on an otherwise blank line, `The date is:` then selecting t +he word `date` and invoking 'Run Command' results in the date being inserted o +n the next line down. The date is: Fri Oct 9 16:12:11 CDT 2009 =head4 Multi-line scripts Typing a multi-line script, selecting the entire script and invoking 'Run Command' will run the entire selection as a shell script: So: for I in 1 2 3 ; do echo " and a $I" done Inserts: and a 1 and a 2 and a 3 after the script block. =head4 The whole shebang Shebangs are supported so the scripts aren't limited to shell commands +/scripts. For example, typing (and selecting) the following #!/usr/bin/env perl print " and a $_\n" for (qw(one two three)); and invoking 'Run Command' inserts: and a one and a two and a three after the script block. =head1 AUTHOR Gregory Siems E<lt>gsiems@gmail.comE<gt> =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by Gregory Siems This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://800386]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found