blib/lib/IO/EditPrompt.pm

filter: Statementstmt 87.1% 54 / 62 Branchbran 57.6% 15 / 26 Conditioncond 87.5% 14 / 16 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 78.5% 99 / 126 CC 20 SCAR 20.2
CC
20
Cov
80%
_run_editor
30.0
_normalize_editor
5.0
new
5.0
CRAP 7.6
↑ Summary
1 package IO::EditPrompt;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use IO::Prompter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.02';
10  
11 13 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
12 13 my ($class, $opts) = (@_);
13  
14 13 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
85
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
15 13 ▶ die "parameter is not a hashref.\n" unless ref $opts eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq ref {} 2 11 13
16 11 ▶ die "'$opts->{tmpdir}' is not a directory.\n" if defined $opts->{tmpdir} and !-d $opts->{tmpdir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"tmpdir"} and not -d $opts->{"tmpdir"} 1 10 11
Condition100%3/3
defined $opts->{"tmpdir"} and not -d $opts->{"tmpdir"}
!ll&&!rl&&r
821
MC/DC100%2/2
defined $opts->{"tmpdir"} and not -d $opts->{"tmpdir"}
defined $opts->{"tmpdir"} not -d $opts->{"tmpdir"}
Truth table100%3/3
A and not(B)
A= defined $opts->{"tmpdir"}B= not -d $opts->{"tmpdir"}
ABresult
0X0
100
111
17  
18 my $self = bless {
19 dir => $opts->{tmpdir},
20 editor => $opts->{editor},
21 10 editor_args => [],
22 }, $class;
23 10 $self->_normalize_editor( $opts->{default_editor} );
24 10 return $self;
25 }
26  
27 2 ▶sub prompt {
Subroutine prompt: called CC=3 SCAR=11.1
Pod documented
28 2 my ($self, $prompt, $deftext) = @_;
29  
30 2 my $output = '';
31 2 my $fmt_prompt = _format_prompt( $prompt );
32  
33 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
0 == length $output and IO::Prompter::prompt("Content is empty, retry?", "-y") and do { do { () = $self->_create_tmp_file($fmt_prompt, $deftext); $self->_run_editor($filename); $output = $self->_get_output($filename, $fmt_prompt) } } 0 2 2
Condition66%2/3
0 == length $output and IO::Prompter::prompt("Content is empty, retry?", "-y")
!ll&&!rl&&r
110
MC/DC0%0/2
0 == length $output and IO::Prompter::prompt("Content is empty, retry?", "-y")
0 == length $output IO::Prompter::prompt("Content is empty, retry?", "-y")
Truth table66%2/3
A and B
A= 0 == length $outputB= IO::Prompter::prompt("Content is empty, retry?", "-y")
ABresult
0X0
100
111
34 2 my ($tmp, $filename) = $self->_create_tmp_file( $fmt_prompt, $deftext );
35 2 $self->_run_editor( $filename );
36 2 $output = $self->_get_output( $filename, $fmt_prompt );
37 } while( (0 == length $output) && IO::Prompter::prompt( 'Content is empty, retry?', '-y' ) );
38  
39 2 return $output;
40 }
41  
42 18 ▶sub _normalize_editor {
Subroutine _normalize_editor: called CC=5 SCAR=16.1
43 18 my ($self, $def_editor) = @_;
44 18 ▶ $self->{editor} ||= $ENV{EDITOR} || $def_editor || 'vim';
Condition87%7/8
$self->{"editor"} ||= $ENV{"EDITOR"} || $def_editor || "vim"
l!l&&r!l&&!r
5130
$ENV{'EDITOR'} || $def_editor || 'vim'
l!l
310
$ENV{"EDITOR"} or $def_editor
l!l&&r!l&&!r
2110
MC/DC0%0/3
$self->{"editor"} ||= $ENV{"EDITOR"} || $def_editor || "vim"
$self->{"editor"} $ENV{"EDITOR"} $def_editor
Truth table0%0/4
A ||= B or C
A= $self->{"editor"}B= $ENV{"EDITOR"}C= $def_editor
ABCresult
1XX1
01X1
0011
0000
45  
46 # Turn off saving state on vim
47 18 ▶ $self->{editor_args} = [ '-i', 'NONE' ] if $self->{editor} =~ /\bvim$/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"editor"} =~ /\bvim$/ 11 7 18
48 18 return;
49 }
50  
51 7 ▶sub _format_prompt {
Subroutine _format_prompt: called CC=2 SCAR=6.9
52 7 my ($prompt) = @_;
53 7 ▶ return '' unless defined $prompt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt 1 6 7
54 6 return join( q{}, map { "# $_\n" } split /\n/, $prompt );
55 }
56  
57 4 ▶sub _create_tmp_file {
Subroutine _create_tmp_file: called CC=3 SCAR=11.0
58 4 my ($self, @texts) = @_;
59  
60 4 ▶ my $tmp = File::Temp->new( UNLINK => 1, EXLOCK => 1, ($self->{dir} ? (DIR => $self->{dir}) : ()) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dir'} ? : 1 3 4
61 4 my $filename = $tmp->filename;
62 4 print {$tmp} grep { defined $_ } @texts;
63 4 ▶ close( $tmp ) or die "Unable to write '$filename': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $tmp or die "Unable to write '${filename}': $!\n" 4 0 4
64  
65 4 return ($tmp, $filename);
66 }
67  
68 5 ▶sub _read_file {
Subroutine _read_file: called CC=2 SCAR=7.0
69 5 my ($self, $filename) = @_;
70 5 ▶ open my $fh, '<', $filename or die "Unable to re-read '$filename': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Unable to re-read '${filename}': $!\n" 5 0 5
71 5 local $/;
72 5 return scalar <$fh>;
73 }
74  
75 8 ▶sub _get_output {
Subroutine _get_output: called CC=2 SCAR=6.9
76 8 my ($self, $filename, $prompt) = @_;
77 8 ▶ return '' if -s $filename eq length $prompt;
Branch: 1 branch
BranchTrueFalseTotal
if -s $filename eq length $prompt 3 5 8
78 5 my $output = $self->_read_file( $filename );
79 5 $output =~ s/^#[^\n]*(?:\n|\Z)//smg;
80 5 return $output;
81 }
82  
83 0 ▶sub _run_editor {
Subroutine _run_editor: not called CC=5 SCAR=34.0
84 0 my ($self, $file) = @_;
85 0 my $err = system $self->{editor}, @{$self->{editor_args}}, $file;
86 0 ▶ return unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
87 0 ▶ if ($? == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
elsif ($? & 127) { } 0 0 0
88 0 die "failed to execute '$self->{editor}': $!\n";
89 }
90 elsif ($? & 127) {
91 0 ▶ die sprintf "'$self->{editor}' died with signal %d, %s coredump\n",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 0 0
92 ($? & 127), ($? & 128) ? 'with' : 'without';
93 }
94 else {
95 0 die sprintf "'$self->{editor}' exited with value %d\n", $? >> 8;
96 }
97 }
98  
99 7 1;
100 __END__
↑ Summary