blib/lib/Pinto/Editor/Edit.pm

filter: Statementstmt 20.3% 26 / 128 Branchbran 0.0% 0 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 25.0% 4 / 16 Podpod 0.0% 0 / 8 totaltotal 13.7% 30 / 218 CC 35 SCAR 47.0
CC
35
Cov
13%
edit
272.0
__ANON__
90.0
join
30.0
CRAP 110.0
« /usr/local/bin/cpanm ↑ Summary blib/lib/App/Pinto.pm »
1 # ABSTRACT: Internal class for Pinto::Editor
2  
3 package Pinto::Editor::Edit;
4  
5 65 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 65 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 65 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 65 ▶use Pinto::Editor::Clip;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #-----------------------------------------------------------------------------
12  
13 65 our $VERSION = '0.14'; # VERSION
14  
15 #-----------------------------------------------------------------------------
16  
17 65 our $EDITOR = 'Pinto::Editor';
18 65 our $RETRY = "__Pinto_Editor_retry__\n";
19 65 our $Test_edit;
20  
21 #-----------------------------------------------------------------------------
22  
23 65 has process => qw/ is ro isa Maybe[CodeRef] /;
24 65 has separator => qw/ is rw /;
25 65 has file => qw/ is ro required 1 /;
26  
27 65 has document => qw/ is rw isa Str required 1 /;
28 65 has $_ => reader => $_, writer => "_$_", isa => 'Str' for qw/ initial_document /;
29  
30 65 has preamble => qw/ is rw isa Maybe[Str] /;
31 65 has $_ => reader => $_, writer => "_$_", isa => 'Maybe[Str]' for qw/ initial_preamble /;
32  
33 65 has content => qw/ is rw isa Str /;
34 65 has $_ => reader => $_, writer => "_$_", isa => 'Str' for qw/ initial_content /;
35  
36 #-----------------------------------------------------------------------------
37  
38 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my $self = shift;
40  
41 0 my $document = $self->document;
42 0 $self->_initial_document( $document );
43  
44 0 my ( $preamble, $content ) = $self->split( $document );
45  
46 0 $self->preamble( $preamble );
47 0 $self->_initial_preamble( $preamble );
48  
49 0 $self->content( $content );
50 0 $self->_initial_content( $content );
51 }
52  
53 #-----------------------------------------------------------------------------
54  
55 0 ▶sub edit {
Subroutine edit: not called CC=16 SCAR=56.1
Pod undocumented
56 0 my $self = shift;
57  
58 0 my $file = $self->file;
59 0 my $tmp;
60 0 ▶ if ( blessed $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($file)) { } 0 0 0
61 0 ▶ if ( $file->isa( 'IO::Handle' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->isa('IO::Handle')) { } 0 0 0
elsif ($file->isa('Path::Class::File')) { } 0 0 0
62 0 $tmp = $file;
63 }
64 elsif ( $file->isa( 'Path::Class::File' ) ) {
65 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
66 }
67 else {
68 0 die "Invalid file: $file";
69 }
70 }
71 else {
72 0 ▶ $file = '' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
73 0 ▶ if ( ref $file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
elsif (length $file) { } 0 0 0
74 0 die "Invalid file: $file";
75 }
76 elsif ( length $file ) {
77 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
78 }
79 else {
80 0 die "Missing file";
81 }
82 }
83 0 $tmp->autoflush( 1 );
84  
85 0 while ( 1 ) {
86 0 ▶ $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): $!" 0 0 0
87 0 ▶ $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): $!" 0 0 0
88 0 $tmp->print( $self->join( $self->preamble, $self->content ) );
89  
90 0 ▶ if ( $Test_edit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pinto::Editor::Edit::Test_edit) { } 0 0 0
91 0 $Test_edit->( $tmp );
92 }
93 else {
94 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 $EDITOR->edit_file( $tmp->filename );
96 }
97 0 ▶ catch {
Subroutine __ANON__: not called CC=9 SCAR=45.0
98 0 my $error = $_[0];
99 0 warn "$error";
100 0 warn "*** There was an error editing ", $tmp->filename, "\n";
101 0 while ( 1 ) {
102 0 print STDERR "Do you want to (c)ontinue, (a)bort, or (s)ave? ";
103 0 my $input = <STDIN>;
104 0 chomp $input;
105 0 ▶ die $error unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
106 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
107 elsif ( $input eq 'c' ) {
108 0 last;
109 }
110 elsif ( $input eq 'a' ) {
111 0 die $error;
112 }
113 elsif ( $input eq 's' ) {
114 0 my $save;
115 0 ▶ unless ( $save = File::Temp->new( dir => '.', template => 'PintoEditor.XXXXXX', unlink => 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($save = "File::Temp"->new("dir", ".", "template", "PintoEditor.XXXXXX", "unlink", 0)) 0 0 0
116 0 ▶ warn "Unable to create temporary file: $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
warn "Unable to create temporary file: $!" and next 0 0 0
117 }
118 0 my $tmp_filename = $tmp->filename;
119 0 my $tmpr;
120 0 ▶ unless ( $tmpr = IO::File->new( $tmp_filename, 'r' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmpr = "IO::File"->new($tmp_filename, "r")) 0 0 0
121 0 ▶ warn "Unable to open ($tmp_filename): $!" and next;
Branch: 1 branch
BranchTrueFalseTotal
warn "Unable to open ($tmp_filename): $!" and next 0 0 0
122 }
123 0 $save->print( join '', <$tmpr> );
124 0 $save->close;
125 0 warn "Saved to: ", $save->filename, " ", ( -s $save->filename ), "\n";
126 }
127 else {
128 0 warn "I don't understand ($input)\n";
129 }
130 }
131  
132 0 };
133 }
134  
135 0 my $document;
136 {
137 0 my $filename = $tmp->filename;
138 0 ▶ 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): $!" 0 0 0
139 0 $document = join '', <$tmpr>;
140 0 $tmpr->close;
141 0 undef $tmpr;
142 }
143  
144 0 $self->document( $document );
145 0 my ( $preamble, $content ) = $self->split( $document );
146 0 $self->preamble( $preamble );
147 0 $self->content( $content );
148  
149 0 ▶ if ( my $process = $self->process ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $process = $self->process) 0 0 0
150 0 my ( @result, $retry );
151 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 @result = $process->( $self );
153 }
154 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 ▶ die $_ unless $_ eq $RETRY;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq $Pinto::Editor::Edit::RETRY 0 0 0
156 0 $retry = 1;
157 0 };
158  
159 0 ▶ next if $retry;
Branch: 1 branch
BranchTrueFalseTotal
if $retry 0 0 0
160  
161 0 ▶ return $result[0] if defined $result[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $result[0] 0 0 0
162 }
163  
164 0 return $content;
165 }
166  
167 }
168  
169 #-----------------------------------------------------------------------------
170  
171 0 ▶sub first_line_blank {
Subroutine first_line_blank: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my $self = shift;
173 0 return $self->document =~ m/\A\s*$/m;
174 }
175  
176 #-----------------------------------------------------------------------------
177  
178 0 ▶sub line0_blank { return $_[0]->first_line_blank }
Subroutine line0_blank: not called CC=1 SCAR=6.9
Pod undocumented
179  
180 #-----------------------------------------------------------------------------
181  
182 0 ▶sub preamble_from_initial {
Subroutine preamble_from_initial: not called CC=4 SCAR=30.0
Pod undocumented
183 0 my $self = shift;
184 0 my @preamble;
185 0 for my $part ( "$_[0]", $self->initial_preamble ) {
186 0 ▶ next unless defined $part;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 0 0
187 0 chomp $part;
188 0 push @preamble, $part;
189 }
190 0 ▶ $self->preamble( join "\n", @preamble, '' ) if @preamble;
Branch: 1 branch
BranchTrueFalseTotal
if @preamble 0 0 0
191 }
192  
193 #-----------------------------------------------------------------------------
194  
195 0 ▶sub retry {
Subroutine retry: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 die $RETRY;
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 0 ▶sub split {
Subroutine split: not called CC=4 SCAR=30.0
Pod undocumented
203 0 my $self = shift;
204 0 my $document = shift;
205  
206 0 ▶ return ( undef, $document ) unless my $separator = $self->separator;
Branch: 1 branch
BranchTrueFalseTotal
unless my $separator = $self->separator 0 0 0
207  
208 0 ▶ die "Invalid separator ($separator)" if ref $separator;
Branch: 1 branch
BranchTrueFalseTotal
if ref $separator 0 0 0
209  
210 0 ▶ 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)) 0 0 0
211 0 return ( $mark->preceding, $mark->remaining );
212 }
213  
214 0 return ( undef, $document );
215 }
216  
217 #-----------------------------------------------------------------------------
218  
219 0 ▶sub join {
Subroutine join: not called CC=5 SCAR=34.0
Pod undocumented
220 0 my $self = shift;
221 0 my $preamble = shift;
222 0 my $content = shift;
223  
224 0 ▶ return $content unless defined $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $preamble 0 0 0
225 0 chomp $preamble;
226  
227 0 my $separator = $self->separator;
228 0 ▶ unless ( defined $separator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $separator) 0 0 0
229 0 ▶ return $content unless length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless length $preamble 0 0 0
230 0 return join "\n", $preamble, $content;
231 }
232 0 ▶ return join "\n", $separator, $content unless length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
unless length $preamble 0 0 0
233 0 return join "\n", $preamble, $separator, $content;
234 }
235  
236 #-----------------------------------------------------------------------------
237 65 1;
238  
239 =pod
240  
241 =encoding UTF-8
242  
243 =for :stopwords Jeffrey Ryan Thalhammer
244  
245 =head1 NAME
246  
247 Pinto::Editor::Edit - Internal class for Pinto::Editor
248  
249 =head1 VERSION
250  
251 version 0.14
252  
253 =head1 DESCRIPTION
254  
255 This is a forked version of L<Term::EditorEdit::Edit> which does not use the deprecated
256 module L<Any::Moose>. My thanks to Robert Krimen for authoring the original.
257 No user-servicable parts in here.
258  
259 =head1 AUTHOR
260  
261 Jeffrey Ryan Thalhammer <jeff@stratopan.com>
262  
263 =head1 COPYRIGHT AND LICENSE
264  
265 This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
266  
267 This is free software; you can redistribute it and/or modify it under
268 the same terms as the Perl 5 programming language system itself.
269  
270 =cut
271  
272 __END__
« /usr/local/bin/cpanm ↑ Summary blib/lib/App/Pinto.pm »