[Bps-public-commit] Prophet branch, master, updated. 0.73-19-g2644a7b

sartak at bestpractical.com sartak at bestpractical.com
Fri Dec 18 20:07:49 EST 2009


The branch, master has been updated
       via  2644a7b4459fabc2bfe8530040c4ce8181685ab3 (commit)
      from  d5da4817056a2fb4103c124f76309ed55a3f1730 (commit)

Summary of changes:
 lib/Prophet/CLI/Command/Shell.pm |   57 +++++++++++++++++++++++++++++++++++--
 1 files changed, 53 insertions(+), 4 deletions(-)

- Log -----------------------------------------------------------------
commit 2644a7b4459fabc2bfe8530040c4ce8181685ab3
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Dec 18 20:07:30 2009 -0500

    Add tab completion for prophet shell

diff --git a/lib/Prophet/CLI/Command/Shell.pm b/lib/Prophet/CLI/Command/Shell.pm
index b4041ff..a14c188 100644
--- a/lib/Prophet/CLI/Command/Shell.pm
+++ b/lib/Prophet/CLI/Command/Shell.pm
@@ -5,6 +5,7 @@ extends 'Prophet::CLI::Command';
 use File::Spec;
 use Prophet::Util;
 use Text::ParseWords qw(shellwords);
+use Scalar::Util qw(weaken);
 
 has name => (
     is => 'ro',
@@ -18,14 +19,34 @@ has term => (
     lazy    => 1,
     handles => [qw/readline addhistory/],
     default => sub {
+        my $self = shift;
+        my $weakself = $self;
+        weaken($weakself);
+
         require Term::ReadLine;
-        return Term::ReadLine->new("Prophet shell");
+        my $term = Term::ReadLine->new("Prophet shell");
+        $term->Attribs->{attempted_completion_function} = sub {
+            $weakself->_complete(@_);
+        };
+        return $term;
     },
 );
 
-    our $HIST = $ENV{PROPHET_HISTFILE}
-            || (($ENV{HOME} || (getpwuid($<))[7]) . "/.prophetreplhist");
-    our $LEN = $ENV{PROPHET_HISTLEN} || 500;
+has current_matches => (
+    is      => 'rw',
+    isa     => 'ArrayRef',
+    default => sub { [] },
+);
+
+has match_index => (
+    is      => 'rw',
+    isa     => 'Int',
+    default => 0,
+);
+
+our $HIST = $ENV{PROPHET_HISTFILE}
+        || (($ENV{HOME} || (getpwuid($<))[7]) . "/.prophetreplhist");
+our $LEN = $ENV{PROPHET_HISTLEN} || 500;
 
 
 sub usage_msg {
@@ -86,6 +107,34 @@ sub _run {
     }
 }
 
+sub _complete {
+    my ($self, $text, $line, $start, $end) = @_;
+
+    # we're discarding everything after the cursor for completion purposes. in the
+    # future we may be able to be smarter, but for now it's good enough.
+    # we can't just use $text because we want all the code before the cursor to
+    # matter, not just the current word
+    substr($line, $end) = '';
+
+    my $dispatcher = $self->cli->dispatcher_class->new(cli => $self->cli);
+    my @matches = $dispatcher->complete($text);
+
+    # iterate through the completions
+    return $self->term->completion_matches($text, sub {
+        my ($text, $state) = @_;
+
+        if (!$state) {
+            $self->current_matches(\@matches);
+            $self->match_index(0);
+        }
+        else {
+            $self->match_index($self->match_index + 1);
+        }
+
+        return $self->current_matches->[$self->match_index];
+    });
+}
+
 # make the REPL history persistent
 sub run{
     my $self = shift;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list