| 1 | package Reply::Plugin::Pager; | ||
| 2 | our $AUTHORITY = 'cpan:DOY'; | ||
| 3 | $Reply::Plugin::Pager::VERSION = '0.42'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | # ABSTRACT: command to automatically open long results in a pager | ||
| 7 | |||
| 8 | use base 'Reply::Plugin'; | ||
| 9 | |||
| 10 | use Term::ReadKey; | ||
| 11 | |||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my %opts = @_; | ||
| 16 | |||
| 17 | if (defined $opts{pager}) { | ||
| 18 | $ENV{PAGER} = $opts{pager}; | ||
| 19 | } | ||
| 20 | |||
| 21 | # delay this because it checks $ENV{PAGER} at load time | ||
| 22 | require IO::Pager; | ||
| 23 | |||
| 24 | my $self = $class->SUPER::new(@_); | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub print_result { | ||
| 29 | my $self = shift; | ||
| 30 | my ($next, @result) = @_; | ||
| 31 | |||
| 32 | my ($cols, $rows) = GetTerminalSize; | ||
| 33 | |||
| 34 | my @lines = map { split /\n/ } @result; | ||
| 35 | if (@lines >= $rows - 2) { | ||
| 36 | IO::Pager::open(my $fh) or die "Couldn't run pager: $!"; | ||
| 37 | $fh->print(@result, "\n"); | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $next->(@result); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |