| 1 | package OptArgs2::Pager; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Exporter::Tidy other => [qw/page start_pager stop_pager/]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Which; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | use OptArgs2::Pager_CI { | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # User provided arguments | ||||||||||||||||||||||||||||
| 11 | auto => { default => 1, }, | ||||||||||||||||||||||||||||
| 12 | encoding => { default => ':utf8', }, | ||||||||||||||||||||||||||||
| 13 | pager => { default => \&_build_pager, }, | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Attributes | ||||||||||||||||||||||||||||
| 16 | fh => { | ||||||||||||||||||||||||||||
| 17 | init_arg => undef, | ||||||||||||||||||||||||||||
| 18 | is => 'rw', | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | default => sub { IO::Handle->new }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||
| 21 | orig_fh => { | ||||||||||||||||||||||||||||
| 22 | init_arg => undef, | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | default => sub { select }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||
| 25 | 1 | pid => { | |||||||||||||||||||||||||||
| 26 | init_arg => undef, | ||||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | init_arg => undef, | ||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | our @CARP_NOT = (__PACKAGE__); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _build_pager { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_pager: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ( exists $ENV{PAGER} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return unless length( $ENV{PAGER} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Explicit pager defined | ||||||||||||||||||||||||||||
| 41 | 0 | my ( $pager, @options ) = split ' ', $ENV{PAGER}; | |||||||||||||||||||||||||||
| 42 | 0 | my $path = File::Which::which($pager); | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | Carp::croak("pager not found: $pager") unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | return join( ' ', $path, @options ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Otherwise take the first from our own list | ||||||||||||||||||||||||||||
| 48 | 0 | foreach my $pager (qw/pager less most w3m lv pg more/) { | |||||||||||||||||||||||||||
| 49 | 0 | my $path = File::Which::which($pager); | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | return $path if $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | Carp::croak("no suitable pager found"); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub BUILD { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->open; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | select $self->fh if $self->auto; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub open { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
open: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return $self->fh if $self->fh->opened; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( not -t $self->orig_fh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $self->fh( $self->orig_fh ); | |||||||||||||||||||||||||||
| 69 | 0 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $pager = $self->pager || return; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->pager || (return)
MC/DC0%0/1
$self->pager || (return)
$self->pager
Truth table0%0/2
A
A= $self->pager
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | local $ENV{LESS} = $ENV{LESS} // '-FSXeR'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"LESS"} // "-FSXeR"
MC/DC0%0/1
$ENV{"LESS"} // "-FSXeR"
$ENV{"LESS"}
Truth table0%0/2
A
A= $ENV{"LESS"}
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | local $ENV{MORE} = $ENV{MORE} // '-FXer' unless $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ENV{"MORE"} // "-FXer"
MC/DC0%0/1
$ENV{"MORE"} // "-FXer"
$ENV{"MORE"}
Truth table0%0/2
A
A= $ENV{"MORE"}
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $self->pid( CORE::open( $self->fh, '|-', $pager ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | or Carp::croak "Could not pipe to PAGER ('$pager'): $!\n"; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | binmode( $self->fh, $self->encoding ? $self->encoding : () ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | or Carp::cluck "Could not set bindmode: $!"; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | $self->fh->autoflush(1); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub close { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | return unless $self->fh && $self->fh->opened; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->fh and $self->fh->opened
MC/DC0%0/2
$self->fh and $self->fh->opened
$self->fh
$self->fh->opened
Truth table0%0/3
A and B
A= $self->fhB= $self->fh->opened
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | select $self->orig_fh; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $self->fh->close if $self->fh ne $self->orig_fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub DESTROY { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->close; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Functions | ||||||||||||||||||||||||||||
| 100 | 1 | my $pager; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub start_pager { | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $pager //= __PACKAGE__->new(@_); | ||||||||||||||||||||||||||
|
Subroutine
start_pager: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
$pager //= __PACKAGE__->new(@_)
MC/DC0%0/2
$pager //= __PACKAGE__->new(@_)
$pager
__PACKAGE__->new(@_)
Truth table0%0/3
A //= B
A= $pagerB= __PACKAGE__->new(@_)
| |||||||||||||||||||||||||||||
| 104 | 0 | $pager->open; | |||||||||||||||||||||||||||
| 105 | 0 | select $pager->fh; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub stop_pager { | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $pager // return; | ||||||||||||||||||||||||||
|
Subroutine
stop_pager: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$pager // (return)
MC/DC0%0/1
$pager // (return)
$pager
Truth table0%0/2
A
A= $pager
| |||||||||||||||||||||||||||||
| 110 | 0 | $pager->close; | |||||||||||||||||||||||||||
| 111 | 0 | select( $pager->orig_fh ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub page { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $text = shift; | ||||||||||||||||||||||||||
|
Subroutine
page: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my $close = not $pager; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $pager //= __PACKAGE__->new( @_, auto => 0 ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$pager //= __PACKAGE__->new(@_, "auto", 0)
MC/DC0%0/2
$pager //= __PACKAGE__->new(@_, "auto", 0)
$pager
__PACKAGE__->new(@_, "auto", 0)
Truth table0%0/3
A //= B
A= $pagerB= __PACKAGE__->new(@_, "auto", 0)
| |||||||||||||||||||||||||||||
| 119 | 0 | $pager->open; | |||||||||||||||||||||||||||
| 120 | 0 | my $ok = $pager->fh->printflush($text); | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | $pager->close if $close; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $ok; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||