[Bps-public-commit] Prophet - A disconnected, replicated p2p database branch, master, updated. 2ebe077abfd4c34e84179d7444e9218aad410df8

jesse jesse at bestpractical.com
Wed Feb 11 01:14:35 EST 2009


The branch, master has been updated
       via  2ebe077abfd4c34e84179d7444e9218aad410df8 (commit)
      from  a3b4a924d5764a0ac019541e2982ebb1f2cd11be (commit)

Summary of changes:
 lib/Prophet/CLI.pm               |   37 +++++++++++++++++++++++++++++++++++++
 lib/Prophet/CLI/Command/Shell.pm |    2 ++
 2 files changed, 39 insertions(+), 0 deletions(-)

- Log -----------------------------------------------------------------
commit 2ebe077abfd4c34e84179d7444e9218aad410df8
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Wed Feb 11 01:14:09 2009 -0500

    Initial implementation of Pager support for SD

diff --git a/lib/Prophet/CLI.pm b/lib/Prophet/CLI.pm
index 664a46a..98efc78 100644
--- a/lib/Prophet/CLI.pm
+++ b/lib/Prophet/CLI.pm
@@ -104,7 +104,9 @@ sub run_one_command {
     $self->context->setup_from_args(@args);
     my $dispatcher = $self->dispatcher_class->new( cli => $self );
     my $dispatch = $dispatcher->dispatch( join ' ', @{ $self->context->primary_commands });
+    $self->start_pager();
     $dispatch->run($dispatcher);
+    $self->end_pager();
 }
 
 sub _command_matches_alias {
@@ -155,6 +157,41 @@ sub invoke {
 }
 
 
+sub is_interactive {
+  return -t STDIN && -t STDOUT;
+}
+
+sub get_pager {
+    my $self = shift;
+    return $ENV{'PAGER'} || `which less` || `which more`;
+}
+
+our $ORIGINAL_STDOUT;
+
+sub start_pager {
+    my $self = shift;
+    my $content = shift;
+    if (is_interactive() && !$ORIGINAL_STDOUT) {
+        local  $ENV{'LESS'} = '-FXE';
+        local  $ENV{'MORE'} = '-FXE';
+
+        my $pager = $self->get_pager();
+        return unless $pager;
+        open (my $cmd, "|-", $pager) || return;
+        $|++;
+        $ORIGINAL_STDOUT = *STDOUT;
+        *STDOUT = $cmd;
+    }
+}
+
+sub end_pager {
+    return unless ($ORIGINAL_STDOUT);
+    *STDOUT = $ORIGINAL_STDOUT ;
+    close($pager);
+    $ORIGINAL_STDOUT=undef;
+}
+
+
 __PACKAGE__->meta->make_immutable;
 no Any::Moose;
 
diff --git a/lib/Prophet/CLI/Command/Shell.pm b/lib/Prophet/CLI/Command/Shell.pm
index e4267c7..a203ef6 100644
--- a/lib/Prophet/CLI/Command/Shell.pm
+++ b/lib/Prophet/CLI/Command/Shell.pm
@@ -58,10 +58,12 @@ sub eval {
 
 sub _run {
     my $self = shift;
+    Prophet::CLI->end_pager;
 
     local $| = 1;
 
     print $self->preamble . "\n";
+    # we don't want to run the pager for the shell
 
     $self->cli->interactive_shell(1);
     while ( defined(my $cmd = $self->read)) {

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



More information about the Bps-public-commit mailing list