EditorEdit/Edit.pm

filter: Statementstmt 68.4% 91 / 133 Branchbran 30.3% 20 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 12 / 18 Podpod 12.5% 1 / 8 totaltotal 55.1% 124 / 225 CC 35 SCAR 39.1
CC
35
Cov
56%
edit
105.8
__ANON__
90.0
__ANON__
6.0
CRAP 49.8
↑ Summary EditorEdit.pm »
1 package Term::EditorEdit::Edit;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Text::Clip;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $EDITOR = 'Term::EditorEdit';
12 2 our $RETRY = "__Term_EditorEdit_retry__\n";
13 2 our $Test_edit;
14  
15 #has editor => qw/ is ro required 1 weak_ref 1 /;
16 2 has process => qw/ is ro isa Maybe[CodeRef] /;
17 2 has separator => qw/ is rw /;
18 2 has file => qw/ is ro required 1 /;
19  
20 2 has document => qw/ is rw isa Str required 1 /;
21 2 has $_ => reader => $_, writer => "_$_", isa => 'Str' for qw/ initial_document /;
22  
23 2 has preamble => qw/ is rw isa Maybe[Str] /;
24 2 has $_ => reader => $_, writer => "_$_", isa => 'Maybe[Str]' for qw/ initial_preamble /;
25  
26 2 has content => qw/ is rw isa Str /;
27 2 has $_ => reader => $_, writer => "_$_", isa => 'Str' for qw/ initial_content /;
28  
29 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
30 2 my $self = shift;
31  
32 2 my $document = $self->document;
33 2 $self->_initial_document( $document );
34  
35 2 my ( $preamble, $content ) = $self->split( $document );
36  
37 2 $self->preamble( $preamble );
38 2 $self->_initial_preamble( $preamble );
39  
40 2 $self->content( $content );
41 2 $self->_initial_content( $content );
42 }
43  
44 1 ▶sub edit {
Subroutine edit: called CC=16 SCAR=46.6
Pod undocumented
45 1 my $self = shift;
46  
47 1 my $file = $self->file;
48 1 my $tmp;
49 1 ▶ if ( blessed $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($file)) { } 1 0 1
50 1 ▶ if ( $file->isa( 'IO::Handle' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->isa('IO::Handle')) { } 1 0 1
elsif ($file->isa('Path::Class::File')) { } 0 0 0
51 1 $tmp = $file;
52 }
53 elsif ( $file->isa( 'Path::Class::File' ) ) {
54 0 ▶ $tmp = $file->open( 'w' ) or die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp = $file->open("w") or die "Unable to open ${file}: $!" 0 0 0
55 }
56 else {
57 0 die "Invalid file: $file";
58 }
59 }
60 else {
61 0 ▶ $file = '' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
62 0 ▶ if ( ref $file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
elsif (length $file) { } 0 0 0
63 0 die "Invalid file: $file";
64 }
65 elsif ( length $file ) {
66 0 ▶ $tmp = IO::File->new( $file, 'w' ) or die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp = "IO::File"->new($file, "w") or die "Unable to open ${file}: $!" 0 0 0
67 }
68 else {
69 0 die "Missing file";
70 }
71 }
72 1 $tmp->autoflush( 1 );
73
74 1 while ( 1 ) {
75 1 ▶ $tmp->seek( 0, 0 ) or die "Unable to seek on tmp ($tmp): $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->seek(0, 0) or die "Unable to seek on tmp ($tmp): $!" 1 0 1
76 1 ▶ $tmp->truncate( 0 ) or die "Unable to truncate on tmp ($tmp): $!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp->truncate(0) or die "Unable to truncate on tmp ($tmp): $!" 1 0 1
77 1 $tmp->print( $self->join( $self->preamble, $self->content ) );
78  
79 1 ▶ if ( $Test_edit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Term::EditorEdit::Edit::Test_edit) { } 1 0 1
80 1 $Test_edit->( $tmp );
81 }
82 else {
83 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 $EDITOR->edit_file( $tmp->filename );
85 }
86 0 ▶ catch {
Subroutine __ANON__: not called CC=9 SCAR=45.0
87 0 my $error = $_[0];
88 0 warn "$error";
89 0 warn "*** There was an error editing ", $tmp->filename, "\n";
90 0 while ( 1 ) {
91 0 print STDERR "Do you want to (c)ontinue, (a)bort, or (s)ave? ";
92 0 my $input = <STDIN>;
93 0 chomp $input;
94 0 ▶ die $error unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
95 0 ▶ if ( 0 ) { }
Branch: 3 branches
BranchTrueFalseTotal
if (do { $input eq 'c' }) { } 0 0 0
elsif ($input eq 'a') { } 0 0 0
elsif ($input eq 's') { } 0 0 0
96 elsif ( $input eq 'c' ) {
97 0 last;
98 }
99 elsif ( $input eq 'a' ) {
100 0 die $error;
101 }
102 elsif ( $input eq 's' ) {
103 0 my $save;
104 0 ▶ unless ( $save = File::Temp->new( dir => '.', template => 'TermEditorEdit.XXXXXX', unlink => 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($save = "File::Temp"->new("dir", ".", "template", "TermEditorEdit.XXXXXX", "unlink", 0)) 0 0 0
105 0 ▶ warn "Unable to create temporary file: $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
warn "Unable to create temporary file: $!" and next 0 0 0
106 }
107 0 my $tmp_filename = $tmp->filename;
108 0 my $tmpr;
109 0 ▶ unless ( $tmpr = IO::File->new( $tmp_filename, 'r' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmpr = "IO::File"->new($tmp_filename, "r")) 0 0 0
110 0 ▶ warn "Unable to open ($tmp_filename): $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
warn "Unable to open ($tmp_filename): $!" and next 0 0 0
111 }
112 0 $save->print( join '', <$tmpr> );
113 0 $save->close;
114 0 warn "Saved to: ", $save->filename, " ", ( -s $save->filename ), "\n";
115 }
116 else {
117 0 warn "I don't understand ($input)\n";
118 }
119 }
120  
121 0 };
122 }
123  
124 1 my $document;
125 {
126 1 my $filename = $tmp->filename;
127 1 ▶ my $tmpr = IO::File->new( $filename, 'r' ) or die "Unable to open ($filename): $!";
Branch: 1 branch
BranchTrueFalseTotal
my $tmpr = "IO::File"->new($filename, "r") or die "Unable to open ($filename): $!" 1 0 1
128 1 $document = join '', <$tmpr>;
129 1 $tmpr->close;
130 1 undef $tmpr;
131 }
132  
133 1 $self->document( $document );
134 1 my ( $preamble, $content ) = $self->split( $document );
135 1 $self->preamble( $preamble );
136 1 $self->content( $content );
137  
138 1 ▶ if ( my $process = $self->process ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $process = $self->process) 0 1 1
139 0 my ( @result, $retry );
140 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 @result = $process->( $self );
142 }
143 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
144 0 ▶ die $_ unless $_ eq $RETRY;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq $Term::EditorEdit::Edit::RETRY 0 0 0
145 0 $retry = 1;
146 0 };
147  
148 0 ▶ next if $retry;
Branch: 1 branch
BranchTrueFalseTotal
if $retry 0 0 0
149  
150 0 ▶ return $result[0] if defined $result[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $result[0] 0 0 0
151 }
152  
153 1 return $content;
154 }
155
156 }
157  
158 0 ▶sub first_line_blank {
Subroutine first_line_blank: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my $self = shift;
160 0 return $self->document =~ m/\A\s*$/m;
161 }
162 0 ▶sub line0_blank { return $_[0]->first_line_blank }
Subroutine line0_blank: not called CC=1 SCAR=6.9
Pod undocumented
163  
164 1 ▶sub preamble_from_initial {
Subroutine preamble_from_initial: called CC=4 SCAR=14.1
Pod undocumented
165 1 my $self = shift;
166 1 my @preamble;
167 1 for my $part ( "$_[0]", $self->initial_preamble ) {
168 2 ▶ next unless defined $part;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 2 2
169 2 chomp $part;
170 2 push @preamble, $part;
171 }
172 1 ▶ $self->preamble( join "\n", @preamble, '' ) if @preamble;
Branch: 1 branch
BranchTrueFalseTotal
if @preamble 1 0 1
173 }
174  
175 1 ▶sub retry {
Subroutine retry: called CC=1 SCAR=0.0
Pod undocumented
176 1 my $self = shift;
177 1 die $RETRY;
178 }
179  
180 4 ▶sub split {
Subroutine split: called CC=4 SCAR=14.3
Pod undocumented
181 4 my $self = shift;
182 4 my $document = shift;
183  
184 4 ▶ return ( undef, $document ) unless my $separator = $self->separator;
Branch: 1 branch
BranchTrueFalseTotal
unless my $separator = $self->separator 3 1 4
185  
186 1 ▶ die "Invalid separator ($separator)" if ref $separator;
Branch: 1 branch
BranchTrueFalseTotal
if ref $separator 0 1 1
187  
188 1 ▶ if ( my $mark = Text::Clip->new( data => $document )->find( qr/^\s*$separator\s*$/m ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mark = "Text::Clip"->new("data", $document)->find(qr/^\s*$separator\s*$/m)) 1 0 1
189 1 return ( $mark->preceding, $mark->remaining );
190 }
191  
192 0 return ( undef, $document );
193 }
194  
195 5 ▶sub join {
Subroutine join: called CC=5 SCAR=16.1
Pod undocumented
196 5 my $self = shift;
197 5 my $preamble = shift;
198 5 my $content = shift;
199  
200 5 ▶ return $content unless defined $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $preamble 2 3 5
201 3 chomp $preamble;
202  
203 3 my $separator = $self->separator;
204 3 ▶ unless ( defined $separator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $separator) 1 2 3
205 1 ▶ return $content unless length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless length $preamble 0 1 1
206 1 return join "\n", $preamble, $content;
207 }
208 2 ▶ return join "\n", $separator, $content unless length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless length $preamble 1 1 2
209 1 return join "\n", $preamble, $separator, $content;
210 }
211  
212 2 1;
↑ Summary EditorEdit.pm »