File Coverage

blib/lib/App/bif/Pager.pm
Criterion Covered Total %
statement 26 45 57.7
branch 3 18 16.6
condition 2 6 33.3
subroutine 10 11 90.9
pod 2 3 66.6
total 43 83 51.8


line stmt bran cond sub pod time code
1             package App::bif::Pager;
2 1     1   448 use strict;
  1         1  
  1         23  
3 1     1   4 use warnings;
  1         1  
  1         18  
4 1     1   312 use Bif::Mo;
  1         2  
  1         4  
5 1     1   4 use Carp ();
  1         1  
  1         17  
6 1     1   568 use File::Which;
  1         777  
  1         53  
7 1     1   597 use IO::Handle;
  1         6513  
  1         436  
8              
9             our @CARP_NOT = (__PACKAGE__);
10              
11             has auto => (
12             is => 'ro',
13             default => 1,
14             );
15              
16             has encoding => (
17             is => 'ro',
18             default => ':utf8',
19             );
20              
21             has pager => (
22             is => 'ro',
23             default => \&_build_pager,
24             );
25              
26             has fh => (
27             is => 'rw',
28             default => sub { IO::Handle->new },
29             );
30              
31             has pid => ( is => 'rw' );
32              
33             has orig_fh => (
34             is => 'ro',
35             default => sub { select },
36             );
37              
38             sub _build_pager {
39 0     0   0 my $self = shift;
40              
41 0 0       0 if ( exists $ENV{PAGER} ) {
42              
43             # Explicit pager defined
44 0         0 my ( $pager, @options ) = split ' ', $ENV{PAGER};
45 0         0 my $path = File::Which::which($pager);
46 0 0       0 Carp::croak("pager not found: $pager") unless $path;
47 0         0 return join( ' ', $path, @options );
48             }
49              
50             # Otherwise take the first from our own list
51 0         0 foreach my $pager (qw/pager less most w3m lv pg more/) {
52 0         0 my $path = File::Which::which($pager);
53 0 0       0 return $path if $path;
54             }
55              
56 0         0 Carp::croak("no suitable pager found");
57             }
58              
59             sub BUILD {
60 1     1 0 3 my $self = shift;
61 1 50       4 $self->open if $self->auto;
62             }
63              
64             sub open {
65 1     1 1 2 my $self = shift;
66 1 50 33     3 return unless -t $self->orig_fh and !$self->fh->opened;
67              
68 0         0 my $pager = $self->pager;
69              
70 0 0       0 my $pid = CORE::open( $self->fh, '|-', $pager )
71             or Carp::croak "Could not pipe to PAGER ('$pager'): $!\n";
72              
73 0 0       0 binmode( $self->fh, $self->encoding ? $self->encoding : () )
    0          
74             or Carp::cluck "Could not set bindmode: $!";
75              
76 0         0 select $self->fh;
77 0         0 $| = 1;
78              
79 0         0 $self->pid($pid);
80              
81 0         0 return;
82             }
83              
84             sub close {
85 1     1 1 2 my $self = shift;
86 1 50 33     3 return unless $self->fh && $self->fh->opened;
87              
88 0         0 select $self->orig_fh;
89 0         0 $self->fh->close;
90             }
91              
92             sub DESTROY {
93 1     1   292 my $self = shift;
94 1         2 $self->close;
95             }
96              
97             1;
98              
99             __END__