OptArgs2/Pager.pm

filter: Statementstmt 34.2% 24 / 70 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 41.1% 7 / 17 Podpod 83.3% 5 / 6 totaltotal 24.3% 36 / 148 CC 23 SCAR 36.6
CC
23
Cov
21%
open
110.0
_build_pager
42.0
close
20.0
CRAP 38.9
« OptArgs2.pm ↑ Summary OptArgs2/Pager_CI.pm »
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
BranchTrueFalseTotal
if (exists $ENV{"PAGER"}) 0 0 0
38 0 ▶ return unless length( $ENV{PAGER} );
Branch: 1 branch
BranchTrueFalseTotal
unless length $ENV{"PAGER"} 0 0 0
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
BranchTrueFalseTotal
unless $path 0 0 0
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
BranchTrueFalseTotal
if $path 0 0 0
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
BranchTrueFalseTotal
if $self->auto 0 0 0
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
BranchTrueFalseTotal
if $self->fh->opened 0 0 0
66  
67 0 ▶ if ( not -t $self->orig_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $self->orig_fh) 0 0 0
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)
l!l
00
MC/DC0%0/1
$self->pager || (return)
$self->pager
Truth table0%0/2
A
A= $self->pager
Aresult
11
00
73  
74 0 ▶ local $ENV{LESS} = $ENV{LESS} // '-FSXeR';
Condition0%0/2
$ENV{"LESS"} // "-FSXeR"
l!l
00
MC/DC0%0/1
$ENV{"LESS"} // "-FSXeR"
$ENV{"LESS"}
Truth table0%0/2
A
A= $ENV{"LESS"}
Aresult
11
00
75 0 ▶ local $ENV{MORE} = $ENV{MORE} // '-FXer' unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 0 0 0
Condition0%0/2
$ENV{"MORE"} // "-FXer"
l!l
00
MC/DC0%0/1
$ENV{"MORE"} // "-FXer"
$ENV{"MORE"}
Truth table0%0/2
A
A= $ENV{"MORE"}
Aresult
11
00
76  
77 0 ▶ $self->pid( CORE::open( $self->fh, '|-', $pager ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->pid(CORE::open($self->fh, "|-", $pager)) or Carp::croak("Could not pipe to PAGER ('${pager}'): $!\n") 0 0 0
78 or Carp::croak "Could not pipe to PAGER ('$pager'): $!\n";
79  
80 0 ▶ binmode( $self->fh, $self->encoding ? $self->encoding : () )
Branch: 2 branches
BranchTrueFalseTotal
binmode $self->fh, $self->encoding ? $self->encoding : () or Carp::cluck("Could not set bindmode: $!") 0 0 0
$self->encoding ? : 0 0 0
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
BranchTrueFalseTotal
unless $self->fh and $self->fh->opened 0 0 0
Condition0%0/3
$self->fh and $self->fh->opened
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
89  
90 0 select $self->orig_fh;
91 0 ▶ $self->fh->close if $self->fh ne $self->orig_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fh ne $self->orig_fh 0 0 0
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(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$pager //= __PACKAGE__->new(@_)
$pager __PACKAGE__->new(@_)
Truth table0%0/3
A //= B
A= $pagerB= __PACKAGE__->new(@_)
ABresult
1X1
011
000
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)
l!l
00
MC/DC0%0/1
$pager // (return)
$pager
Truth table0%0/2
A
A= $pager
Aresult
11
00
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)
l!l&&r!l&&!r
000
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)
ABresult
1X1
011
000
119 0 $pager->open;
120 0 my $ok = $pager->fh->printflush($text);
121 0 ▶ $pager->close if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 0 0 0
122 0 $ok;
123 }
124  
125 1 1;
126  
127 __END__
« OptArgs2.pm ↑ Summary OptArgs2/Pager_CI.pm »