lib/TUI/Editors/FileEditor.pm

filter: Statementstmt 33.5% 67 / 200 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.8% 19 / 34 Podpod 73.3% 11 / 15 totaltotal 29.9% 97 / 324 CC 35 SCAR 31.5
CC
35
Cov
25%
valid
72.0
BUILD
42.0
handleEvent
30.0
CRAP 23.3
« lib/TUI/ColorSel/ColorGroupList.pm ↑ Summary lib/TUI/Drivers/ColorAttr.pm »
1 package TUI::Editors::FileEditor;
2 # ABSTRACT: text editor view bound to a file on disk
3
4 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 6 our $VERSION = '2.000003';
9 6 $VERSION =~ tr/_//d;
10 6 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 our @EXPORT = qw(
14 TFileEditor
15 new_TFileEditor
16 );
17
18 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use File::Basename qw( fileparse );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use TUI::toolkit;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 Maybe
24 is_HashRef
25 is_Object
26 is_Str
27 :types
28 6 );
29
30 6 ▶use TUI::Const qw( EOS );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use TUI::Drivers::Const qw( :evXXXX );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use TUI::Drivers::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use TUI::Editors::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
34 :cmXXXX
35 :edXXXX
36 :efXXXX
37 ufUpdate
38 6 );
39 6 ▶use TUI::Editors::Editor;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use TUI::Objects::Rect;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use TUI::StdDlg::Util qw( fexpand );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use TUI::Views::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
43 cmCancel
44 cmNo
45 cmSave
46 cmSaveAs
47 cmValid
48 cmYes
49 6 );
50 6 ▶use TUI::Views::Util qw( message );
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 sub TFileEditor() { __PACKAGE__ }
53 sub name() { 'TFileEditor' }
54 0 ▶sub new_TFileEditor { __PACKAGE__->from(@_) }
Subroutine new_TFileEditor: not called CC=1 SCAR=6.9
Pod documented
55
56 6 extends TEditor;
57
58 # import global variables
59 6 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
60 $editorDialog
61 $editorFlags
62 6 );
63 {
64 6 *editorDialog = \$TUI::Editors::Editor::editorDialog;
65 6 *editorFlags = \$TUI::Editors::Editor::editorFlags;
66 }
67
68 # public attributes
69 6 has fileName => ( is => 'rw', default => '' );
70
71 # declare local variables
72 6 my $backupExt = '.BAK';
73
74 0 ▶sub BUILDARGS { # \%args (%args)
Subroutine BUILDARGS: not called CC=1 SCAR=6.9
Pod documented
75 0 state $sig = signature(
76 method => 1,
77 named => [
78 bounds => Object,
79 hScrollBar => Maybe[Object], { alias => 'aHScrollBar' },
80 vScrollBar => Maybe[Object], { alias => 'aVScrollBar' },
81 indicator => Maybe[Object], { alias => 'aIndicator' },
82 fileName => Maybe[Str], { alias => 'aFileName' },
83 ],
84 caller_level => +1,
85 );
86 0 my ( $class, $args1 ) = $sig->( @_ );
87 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
88 my $args2 = $class->SUPER::BUILDARGS(
89 bounds => $args1->{bounds},
90 hScrollBar => $args1->{hScrollBar},
91 vScrollBar => $args1->{vScrollBar},
92 indicator => $args1->{indicator},
93 0 bufSize => 0,
94 );
95 0 return { %$args1, %$args2 };
96 }
97
98 0 ▶sub BUILD { # void (\%args)
Subroutine BUILD: not called CC=6 SCAR=37.4
Pod documented
99 0 my ( $self, $args ) = @_;
100 0 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
101 0 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 0 0
102 0 ▶ $self->{fileName} //= EOS;
Condition0%0/2
$self->{"fileName"} //= ""
l!l
00
MC/DC0%0/1
$self->{"fileName"} //= ""
$self->{"fileName"}
Truth table0%0/2
A
A= $self->{"fileName"}
Aresult
11
00
103 0 ▶ if ( length $self->{fileName} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"fileName"}) 0 0 0
104 0 fexpand( $self->{fileName} );
105 $self->{isValid} = $self->loadFile()
106 0 ▶ if $self->{isValid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"isValid"} 0 0 0
107 }
108 0 return;
109 }
110
111 0 ▶sub from { # $obj ($bounds, $aHScrollBar|undef, $aVScrollBar|undef, $aIndicator|undef, $aFileName|undef)
Subroutine from: not called CC=1 SCAR=6.9
Pod undocumented
112 0 state $sig = signature(
113 method => 1,
114 pos => [
115 Object,
116 Maybe[Object],
117 Maybe[Object],
118 Maybe[Object],
119 Maybe[Str],
120 ],
121 );
122 0 my ( $class, @args ) = $sig->( @_ );
123 0 return $class->new( bounds => $args[0], hScrollBar => $args[1],
124 vScrollBar => $args[2], indicator => $args[3], fileName => $args[4] );
125 }
126
127 0 ▶sub handleEvent { # void ($event)
Subroutine handleEvent: not called CC=5 SCAR=34.0
Pod documented
128 0 state $sig = signature(
129 method => Object,
130 pos => [Object],
131 );
132 0 my ( $self, $event ) = $sig->( @_ );
133 0 $self->SUPER::handleEvent( $event );
134 return
135 0 ▶ if $event->{what} != evCommand;
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"what"} != 256 0 0 0
136
137 0 SWITCH: for ( $event->{message}{command} ) {
138 0 ▶ cmSave == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
32 == $_ and do { $self->save; last } 0 0 0
139 0 $self->save();
140 0 last;
141 };
142 0 ▶ cmSaveAs == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
33 == $_ and do { $self->saveAs; last } 0 0 0
143 0 $self->saveAs();
144 0 last;
145 };
146 0 return;
147 } #/ SWITCH: for ( $event->{message}{command} )
148
149 0 $self->clearEvent( $event );
150 0 return;
151 }
152
153 0 ▶sub loadFile { # $bool ()
Subroutine loadFile: not called CC=5 SCAR=34.0
Pod documented
154 0 state $sig = signature(
155 method => Object,
156 pos => [],
157 );
158 0 my ( $self ) = $sig->( @_ );
159 0 my $fh;
160 0 ▶ if ( !open $fh, '<:raw', $self->{fileName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<:raw", $self->{"fileName"}) 0 0 0
161 0 $self->setBufLen( 0 );
162 0 return true;
163 }
164
165 0 ▶ my $fSize = -s $fh // 0;
Condition0%0/2
-s $fh // 0
l!l
00
MC/DC0%0/1
-s $fh // 0
-s $fh
Truth table0%0/2
A
A= -s $fh
Aresult
11
00
166 0 my $data = '';
167 0 my $read = read $fh, $data, $fSize;
168 0 ▶ if ( !defined $read || $read != $fSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $read or $read != $fSize) 0 0 0
Condition0%0/3
not defined $read or $read != $fSize
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $read or $read != $fSize
not defined $read $read != $fSize
Truth table0%0/3
not(A) or B
A= not defined $readB= $read != $fSize
ABresult
1X1
011
000
169 0 close $fh;
170 0 &$editorDialog( edReadError, $self->{fileName} );
171 0 return false;
172 }
173 0 close $fh;
174
175 0 $self->{buffer} = $data;
176 0 $self->setBufLen( length $data );
177 0 return true;
178 }
179
180 0 ▶sub save { # $bool ()
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
181 0 state $sig = signature(
182 method => Object,
183 pos => [],
184 );
185 0 my ( $self ) = $sig->( @_ );
186 return length $self->{fileName}
187 0 ▶ ? $self->saveFile()
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'fileName'} ? : 0 0 0
188 : $self->saveAs();
189 }
190
191 0 ▶sub saveAs { # $bool ()
Subroutine saveAs: not called CC=3 SCAR=24.8
Pod documented
192 0 state $sig = signature(
193 method => Object,
194 pos => [],
195 );
196 0 my ( $self ) = $sig->( @_ );
197 0 my $res = false;
198 0 ▶ if ( &$editorDialog( edSaveAs, $self->{fileName} ) != cmCancel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$TUI::Editors::FileEditor::editorDialog(6, $self->{"fileName"}) != 11) 0 0 0
199 0 fexpand( $self->{fileName} );
200 0 message( $self->{owner}, evBroadcast, cmUpdateTitle, undef );
201 0 $res = $self->saveFile();
202 0 ▶ $self->{fileName} = EOS
Branch: 1 branch
BranchTrueFalseTotal
if $self->isClipboard 0 0 0
203 if $self->isClipboard();
204 }
205 0 return $res;
206 }
207
208 0 ▶sub saveFile { # $bool ()
Subroutine saveFile: not called CC=5 SCAR=34.0
Pod documented
209 0 state $sig = signature(
210 method => Object,
211 pos => [],
212 );
213 0 my ( $self ) = $sig->( @_ );
214
215 0 ▶ if ( $editorFlags & efBackupFiles ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TUI::Editors::FileEditor::editorFlags & 256) 0 0 0
216 0 my ( $file, $dir, $ext ) = fileparse( $self->{fileName}, qr/\.[^.]*/ );
217 0 my $backupName = $dir . $file . $backupExt;
218 0 unlink $backupName;
219 0 rename $self->{fileName}, $backupName;
220 }
221
222 0 my $fh;
223 0 ▶ if ( !open $fh, '>:raw', $self->{fileName} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">:raw", $self->{"fileName"}) 0 0 0
224 0 &$editorDialog( edCreateError, $self->{fileName} );
225 0 return false;
226 }
227
228 0 my $written = print {$fh} $self->{buffer};
229 0 my $closed = close $fh;
230 0 ▶ if ( !$written || !$closed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($written and $closed) 0 0 0
Condition0%0/3
$written and $closed
!ll&&!rl&&r
000
MC/DC0%0/2
$written and $closed
$written $closed
Truth table0%0/3
A and B
A= $writtenB= $closed
ABresult
0X0
100
111
231 0 &$editorDialog( edWriteError, $self->{fileName} );
232 0 return false;
233 }
234
235 0 $self->{modified} = false;
236 0 $self->update( ufUpdate );
237 0 return true;
238 }
239
240 0 ▶sub shutDown { # void ()
Subroutine shutDown: not called CC=1 SCAR=6.9
Pod documented
241 0 state $sig = signature(
242 method => Object,
243 pos => [],
244 );
245 0 my ( $self ) = $sig->( @_ );
246 0 $self->setCmdState( cmSave, false );
247 0 $self->setCmdState( cmSaveAs, false );
248 0 $self->SUPER::shutDown();
249 0 return;
250 }
251
252 0 ▶sub updateCommands { # void ()
Subroutine updateCommands: not called CC=1 SCAR=6.9
Pod documented
253 0 state $sig = signature(
254 method => Object,
255 pos => [],
256 );
257 0 my ( $self ) = $sig->( @_ );
258 0 $self->SUPER::updateCommands();
259 0 $self->setCmdState( cmSave, true );
260 0 $self->setCmdState( cmSaveAs, true );
261 0 return;
262 }
263
264 0 ▶sub valid { # $bool ($command)
Subroutine valid: not called CC=8 SCAR=42.8
Pod documented
265 0 state $sig = signature(
266 method => Object,
267 pos => [PositiveOrZeroInt],
268 );
269 0 my ( $self, $command ) = $sig->( @_ );
270 0 ▶ return $self->{isValid} if $command == cmValid;
Branch: 1 branch
BranchTrueFalseTotal
if $command == 0 0 0 0
271
272 0 ▶ if ( $self->{modified} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"modified"}) 0 0 0
273 0 ▶ my $d = length $self->{fileName} ? edSaveModify : edSaveUntitled;
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'fileName'} ? : 0 0 0
274 0 my $result = &$editorDialog( $d, $self->{fileName} );
275 0 SWITCH: for ( $result ) {
276 0 ▶ cmYes == $_ and return $self->save();
Branch: 1 branch
BranchTrueFalseTotal
12 == $_ and return $self->save 0 0 0
277 0 ▶ cmNo == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
13 == $_ and 0 0 0
278 0 $self->{modified} = false;
279 0 return true;
280 };
281 0 ▶ cmCancel == $_ and return false;
Branch: 1 branch
BranchTrueFalseTotal
11 == $_ and 0 0 0
282 } #/ SWITCH: for ( $result )
283 }
284 0 return true;
285 }
286
287 0 ▶sub write :method { # void ($os)
Subroutine write: not called CC=1 SCAR=6.9
Pod undocumented
288 0 state $sig = signature(
289 method => Object,
290 pos => [Object],
291 );
292 0 my ( $self, $os ) = $sig->( @_ );
293 0 $self->SUPER::write( $os );
294 $os->put(
295 $self->{fileName}, $self->{selStart}, $self->{selEnd}, $self->{curPtr},
296 0 );
297 0 return;
298 }
299
300 0 ▶sub read :method { # $self ($is)
Subroutine read: not called CC=5 SCAR=34.0
Pod undocumented
301 0 state $sig = signature(
302 method => Object,
303 pos => [Object],
304 );
305 0 my ( $self, $is ) = $sig->( @_ );
306 0 $self->SUPER::read( $is );
307 0 $self->{fileName} = $is->get();
308 0 ▶ if ( $self->{isValid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"isValid"}) 0 0 0
309 0 $self->{isValid} = $self->loadFile();
310 0 my $sStart = $is->get();
311 0 my $sEnd = $is->get();
312 0 my $curs = $is->get();
313 0 ▶ if ( $self->{isValid} && $sEnd <= $self->bufLen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"isValid"} and $sEnd <= $self->bufLen) 0 0 0
Condition0%0/3
$self->{"isValid"} and $sEnd <= $self->bufLen
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"isValid"} and $sEnd <= $self->bufLen
$self->{"isValid"} $sEnd <= $self->bufLen
Truth table0%0/3
A and B
A= $self->{"isValid"}B= $sEnd <= $self->bufLen
ABresult
0X0
100
111
314 0 ▶ $self->setSelect( $sStart, $sEnd, $curs == $sStart ? true : false );
Branch: 1 branch
BranchTrueFalseTotal
$curs == $sStart ? : 0 0 0
315 0 $self->trackCursor( true );
316 }
317 }
318 0 return $self;
319 }
320
321 0 ▶sub build { # $obj ()
Subroutine build: not called CC=4 SCAR=30.0
Pod undocumented
322 0 my ( $class ) = @_;
323 0 ▶ assert ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
324 0 ▶ assert ( is_Str $class );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Str($class) 0 0 0
325 0 ▶ assert ( Storable::is_retrieving );
Branch: 1 branch
BranchTrueFalseTotal
unless &Storable::is_retrieving() 0 0 0
326 0 return $class->new(
327 bounds => TRect->new(),
328 hScrollBar => undef,
329 vScrollBar => undef,
330 indicator => undef,
331 fileName => '',
332 );
333 }
334
335 6 1
336
337 __END__
« lib/TUI/ColorSel/ColorGroupList.pm ↑ Summary lib/TUI/Drivers/ColorAttr.pm »