From f4775be782ef0cd1003190169b539b3ba2367292 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 8 Jun 2013 12:03:31 -0500 Subject: allow running single commands through the repl --- lib/Reply.pm | 43 ++++++++++++++++++++++++++++++----------- lib/Reply/Plugin/FancyPrompt.pm | 2 ++ lib/Reply/Plugin/Nopaste.pm | 3 +++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/Reply.pm b/lib/Reply.pm index 22f14d5..dbb9b24 100644 --- a/lib/Reply.pm +++ b/lib/Reply.pm @@ -107,19 +107,33 @@ undef. sub run { my $self = shift; - while (defined(my $line = $self->_read)) { - try { - my @result = $self->_eval($line); - $self->_print_result(@result); - } - catch { - $self->_print_error($_); - }; - $self->_loop; - } + while ($self->run_one) { } print "\n"; } +sub run_one { + my $self = shift; + my ($line) = @_; + + if (defined $line) { + $line = $self->_premangle_line($line); + } + else { + $line = $self->_read; + } + + return unless defined $line; + + try { + my @result = $self->_eval($line); + $self->_print_result(@result); + } + catch { + $self->_print_error($_); + }; + $self->_loop; +} + sub _load_config { my $self = shift; my ($file) = @_; @@ -182,6 +196,13 @@ sub _read { my ($line) = $self->_wrapped_plugin('read_line', $prompt); return if !defined $line; + return $self->_premangle_line($line); +} + +sub _premangle_line { + my $self = shift; + my ($line) = @_; + if ($line =~ s/^#(\w+)(?:\s+|$)//) { ($line) = $self->_chained_plugin("command_\L$1", $line); } @@ -219,7 +240,7 @@ sub _print_result { sub _loop { my $self = shift; - $self->_chained_plugin('loop'); + $self->_chained_plugin('loop', 1); } sub _wrapped_plugin { diff --git a/lib/Reply/Plugin/FancyPrompt.pm b/lib/Reply/Plugin/FancyPrompt.pm index d0afc38..1f36d45 100644 --- a/lib/Reply/Plugin/FancyPrompt.pm +++ b/lib/Reply/Plugin/FancyPrompt.pm @@ -33,7 +33,9 @@ sub prompt { sub loop { my $self = shift; + my ($continue) = @_; $self->{counter}++; + $continue; } 1; diff --git a/lib/Reply/Plugin/Nopaste.pm b/lib/Reply/Plugin/Nopaste.pm index dec0940..566c668 100644 --- a/lib/Reply/Plugin/Nopaste.pm +++ b/lib/Reply/Plugin/Nopaste.pm @@ -70,12 +70,15 @@ sub print_result { sub loop { my $self = shift; + my ($continue) = @_; my $prompt = delete $self->{prompt}; my $line = delete $self->{line}; my $result = delete $self->{result}; $self->{history} .= "$prompt$line$result"; + + $continue; } sub command_nopaste { -- cgit v1.2.3-54-g00ecf