CLI/ReadLine.pm

filter: Statementstmt 55.6% 138 / 248 Branchbran 41.5% 44 / 106 Conditioncond 19.0% 4 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 65.8% 27 / 41 Podpod 100% 16 / 16 totaltotal 51.6% 230 / 445 CC 65 SCAR 26.2
CC
65
Cov
50%
echo_signal_char
72.0
_prepare_prompt
40.4
__ANON__
30.0
CRAP 13.8
« CLI/Util.pm ↑ Summary CLI/Role/HelpText.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::ReadLine
4 #
5 # Description: Class for Term::CLI and Term::ReadLine glue
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 23/Jan/2018
9 #
10 # Copyright (c) 2018-2022 Steven Bakker
11 #
12 # This module is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl itself. See "perldoc perlartistic."
14 #
15 # This software is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 #
19 #=============================================================================
20  
21 package Term::CLI::ReadLine 0.061001;
22  
23 21 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 21 ▶use parent 0.225 qw( Term::ReadLine );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 21 ▶use Term::ReadKey 2.32 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 21 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 21 my $DFL_HIST_SIZE = 500;
33 21 my $HIST_LIMIT = 2**64 - 1;
34 21 my $Term = undef;
35  
36 # Since we cannot be sure what type the Term::ReadLine object
37 # is (HASH or ARRAY), we'll have to keep some state here.
38  
39 21 my $History_Size = $DFL_HIST_SIZE;
40 21 my @History = ();
41  
42 # Keep a filehandle for Term::ReadKey operations.
43 21 my $Term_FH = undef;
44  
45 # Original_KB_Signals is fetched at `new` time and used to both restore
46 # the ControlChars as well as validate given key names: not all
47 # platforms support the same keys.
48 21 my %Original_KB_Signals = ();
49 21 my @Default_Ignore_KB_Signals = qw( QUIT );
50 21 my %Ignore_KB_Signals = ();
51 21 my %Sig2KeyName = (
52 'INT' => 'INTERRUPT',
53 'QUIT' => 'QUIT',
54 'TSTP' => 'SUSPEND',
55 );
56  
57 10 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
58 10 my ( $class, @args ) = @_;
59  
60 10 ▶ return $Term if $Term;
Branch: 1 branch
BranchTrueFalseTotal
if $Term 2 8 10
61  
62 8 $Term = bless Term::ReadLine->new(@args), $class;
63  
64 ## no critic (ProhibitInteractiveTest)
65 8 ▶ $Term_FH = -t $Term->OUT ? $Term->OUT : undef;
Branch: 1 branch
BranchTrueFalseTotal
-t $Term->OUT ? : 0 8 8
66  
67 8 ▶ %Original_KB_Signals = Term::ReadKey::GetControlChars($Term_FH)
Branch: 1 branch
BranchTrueFalseTotal
if $Term_FH 0 8 8
68 if $Term_FH;
69  
70 8 ▶ if ( eval { exists $Term->Attribs->{catch_signals} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { exists $Term->Attribs->{"catch_signals"} } }) 0 8 8
71 0 $Term->Attribs->{catch_signals} = 1;
72 }
73  
74 8 $Term->reset_ignore_keyboard_signals();
75  
76 8 return $Term->_install_stubs;
77 }
78  
79 166 ▶sub term { return $Term }
Subroutine term: called CC=1 SCAR=0.0
Pod documented
80  
81 # Dumb wrapper around "Attrib" that allows mocking the
82 # `completion_quote_character` state.
83 36 ▶sub completion_quote_character {
Subroutine completion_quote_character: called CC=2 SCAR=7.1
Pod documented
84 36 my ($self) = @_;
85 36 ▶ my $c = $self->term->Attribs->{completion_quote_character} // q{};
Condition50%1/2
$self->term->Attribs->{"completion_quote_character"} // ""
l!l
036
MC/DC0%0/1
$self->term->Attribs->{"completion_quote_character"} // ""
$self->term->Attribs->{"completion_quote_character"}
Truth table50%1/2
A
A= $self->term->Attribs->{"completion_quote_character"}
Aresult
11
00
86 36 return $c =~ s/\000//rgx;
87 }
88  
89 9 ▶sub ignore_keyboard_signals {
Subroutine ignore_keyboard_signals: called CC=4 SCAR=14.2
Pod documented
90 9 my ( $self, @args ) = @_;
91 9 foreach my $signame (@args) {
92 10 ▶ my $charname = $Sig2KeyName{$signame} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $charname = $Sig2KeyName{$signame} or next 9 1 10
93 9 ▶ $Original_KB_Signals{$charname} or next;
Branch: 1 branch
BranchTrueFalseTotal
$Original_KB_Signals{$charname} or next 0 9 9
94 0 $Ignore_KB_Signals{$charname} = q{};
95 }
96 9 return;
97 }
98  
99 1 ▶sub no_ignore_keyboard_signals {
Subroutine no_ignore_keyboard_signals: called CC=4 SCAR=14.2
Pod documented
100 1 my ( $self, @args ) = @_;
101 1 foreach my $signame (@args) {
102 2 ▶ my $charname = $Sig2KeyName{$signame} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $charname = $Sig2KeyName{$signame} or next 1 1 2
103 1 ▶ $Original_KB_Signals{$charname} or next;
Branch: 1 branch
BranchTrueFalseTotal
$Original_KB_Signals{$charname} or next 0 1 1
104 0 delete $Ignore_KB_Signals{$charname};
105 }
106 1 return;
107 }
108  
109 6 ▶sub _set_ignore_keyboard_signals {
Subroutine _set_ignore_keyboard_signals: called CC=2 SCAR=9.2
110 6 my ($self) = @_;
111 6 ▶ return if !$Term_FH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Term_FH 6 0 6
112 0 Term::ReadKey::SetControlChars( %Ignore_KB_Signals, $Term_FH );
113 0 return;
114 }
115  
116 6 ▶sub _restore_keyboard_signals {
Subroutine _restore_keyboard_signals: called CC=2 SCAR=9.2
117 6 my ($self) = @_;
118 6 ▶ return if !$Term_FH;
Branch: 1 branch
BranchTrueFalseTotal
unless $Term_FH 6 0 6
119 0 Term::ReadKey::SetControlChars( %Original_KB_Signals, $Term_FH );
120 0 return;
121 }
122  
123 8 ▶sub reset_ignore_keyboard_signals {
Subroutine reset_ignore_keyboard_signals: called CC=1 SCAR=0.0
Pod documented
124 8 my ($self) = @_;
125 8 %Ignore_KB_Signals = ();
126 8 $self->ignore_keyboard_signals(@Default_Ignore_KB_Signals);
127 8 return;
128 }
129  
130 7 ▶sub term_width {
Subroutine term_width: called CC=1 SCAR=0.4
Pod documented
131 7 my ($self) = @_;
132 7 my ( $rows, $cols ) = $self->get_screen_size();
133 0 return $cols;
134 }
135  
136 0 ▶sub term_height {
Subroutine term_height: not called CC=1 SCAR=6.9
Pod documented
137 0 my ($self) = @_;
138 0 my ( $rows, $cols ) = $self->get_screen_size();
139 0 return $rows;
140 }
141  
142 0 ▶sub echo_signal_char {
Subroutine echo_signal_char: not called CC=8 SCAR=42.8
Pod documented
143 0 my ( $self, $sig_arg ) = @_;
144  
145 0 state $name2int = {
146 'INT' => 2,
147 'QUIT' => 3,
148 'TSTP' => 20
149 };
150  
151 0 ▶ if ( $self->ReadLine =~ /::Gnu$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ReadLine =~ /::Gnu$/ux) 0 0 0
152 0 ▶ if ( $sig_arg =~ /\D/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_arg =~ /\D/ux) 0 0 0
153 0 ▶ $sig_arg = $name2int->{ uc $sig_arg } or return;
Branch: 1 branch
BranchTrueFalseTotal
$sig_arg = $$name2int{uc $sig_arg} or return 0 0 0
154 }
155 0 return $self->SUPER::echo_signal_char($sig_arg);
156 }
157  
158 0 state $int2name = { reverse %{$name2int} };
159  
160 0 ▶ if ( $sig_arg =~ /^\d+$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_arg =~ /^\d+$/ux) 0 0 0
161 0 ▶ $sig_arg = $int2name->{$sig_arg} or return;
Branch: 1 branch
BranchTrueFalseTotal
$sig_arg = $int2name->{$sig_arg} or return 0 0 0
162 }
163 0 ▶ $sig_arg = $Sig2KeyName{$sig_arg} // $sig_arg;
Condition0%0/3
$Sig2KeyName{$sig_arg} // $sig_arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$Sig2KeyName{$sig_arg} // $sig_arg
$Sig2KeyName{$sig_arg} $sig_arg
Truth table0%0/3
A // B
A= $Sig2KeyName{$sig_arg}B= $sig_arg
ABresult
1X1
011
000
164 0 ▶ my $char = $Original_KB_Signals{$sig_arg} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $char = $Original_KB_Signals{$sig_arg} or return 0 0 0
165 0 $char =~ s{ ([\000-\037]) }{'^'.chr(ord($1)+ord('@'))}gex;
166 0 $self->OUT->print($char);
167  
168 0 return;
169 }
170  
171 0 ▶sub _escape_str { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _escape_str: not called CC=1 SCAR=6.9
172 0 my ( $self, $str ) = @_;
173 0 $str =~ s/\t/\\t/gx;
174 0 $str =~ s/\n/\\n/gx;
175 0 $str =~ s/\r/\\r/gx;
176 0 $str =~ s/([\177-\377])/sprintf("\\%03o", ord($1))/gex;
177 0 $str =~ s/([\000-\037])/'^'.chr(ord($1)+ord('@'))/gex;
178 0 return $str;
179 }
180  
181 # The GNU readline implementation will just slap the prompt between the
182 # ornament-start/ornament-end sequences, but this looks ugly if there
183 # are leading/trailing spaces and the ornament is set to underline
184 # (or standout). The following will bring it in line with how the Perl
185 # implementation handles it, by inserting start/end sequences where
186 # necessary.
187 6 ▶sub _prepare_prompt {
Subroutine _prepare_prompt: called CC=7 SCAR=37.0
188 6 my ( $self, $prompt ) = @_;
189  
190 6 ▶ return $prompt if $self->ReadLine !~ /::Gnu$/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ReadLine =~ /::Gnu$/ux 6 0 6
191 0 ▶ return $prompt if length $self->Attribs->{term_set}[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->Attribs->{"term_set"}[0] == 0 0 0 0
192  
193 0 my ( $head, $body, $tail ) = $prompt =~ /^(\s*)(.*?)(\s*)$/x;
194 0 ▶ return $prompt if ( $head eq q{} and $tail eq q{} );
Branch: 1 branch
BranchTrueFalseTotal
if $head eq "" and $tail eq "" 0 0 0
Condition0%0/3
$head eq "" and $tail eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$head eq "" and $tail eq ""
$head eq "" $tail eq ""
Truth table0%0/3
A and B
A= $head eq ""B= $tail eq ""
ABresult
0X0
100
111
195  
196 #say "prompt: ", $self->_escape_str("<$head><$body><$tail>");
197 #say "start_ignore: ", $self->_escape_str($self->RL_PROMPT_START_IGNORE);
198 #say "end_ignore: ", $self->_escape_str($self->RL_PROMPT_END_IGNORE);
199 #say "term_set 0: ", $self->_escape_str($self->Attribs->{term_set}[0]);
200 #say "term_set 1: ", $self->_escape_str($self->Attribs->{term_set}[1]);
201  
202 0 $prompt = q{};
203 0 ▶ if ( length $head ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $head) 0 0 0
204 $prompt
205 .= $self->Attribs->{term_set}[1]
206 . $head
207 0 . $self->Attribs->{term_set}[0];
208 }
209  
210 #say $self->_escape_str($prompt);
211  
212 0 $prompt .= $body;
213  
214 #say $self->_escape_str($prompt);
215  
216 0 ▶ if ( length $tail ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail) 0 0 0
217 0 $prompt .= $self->Attribs->{term_set}[1] . $tail;
218 }
219  
220 #say $self->_escape_str($prompt);
221  
222 0 return $prompt;
223 }
224  
225 6 ▶sub readline { ## no critic (ProhibitBuiltinHomonyms)
Subroutine readline: called CC=4 SCAR=13.9
Pod documented
226 6 my ( $self, $prompt ) = @_;
227  
228 # local(%SIG) logic does not work... :-(
229 6 my %old_SIG = $self->_set_signal_handlers;
230  
231 6 $prompt = $self->_prepare_prompt($prompt);
232 6 $self->_set_ignore_keyboard_signals();
233 6 my $input = $self->SUPER::readline($prompt);
234 6 $self->_restore_keyboard_signals();
235  
236 6 ▶ if ( !$self->Features->{autohistory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->Features->{"autohistory"}) 6 0 6
237 6 ▶ if ( defined $input && length($input) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and length $input) 4 2 6
Condition66%2/3
defined $input and length $input
!ll&&!rl&&r
024
MC/DC50%1/2
defined $input and length $input
defined $input length $input
Truth table66%2/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
238 4 $self->AddHistory($input);
239 }
240 }
241 6 $self->_restore_signal_handlers( %old_SIG );
242 6 return $input;
243 }
244  
245 # CLI->_restore_signal_handlers( $HASH_REF );
246 #
247 # Re-set signal handlers that we overrode earlier.
248 #
249 6 ▶sub _restore_signal_handlers {
Subroutine _restore_signal_handlers: called CC=2 SCAR=6.9
250 6 my ($self, %old_SIG) = @_;
251 # Cannot assign slices to %SIG... :-(
252 6 ▶ while ( my ($sig, $handler) = each %old_SIG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %old_SIG) 36 6 42
253 36 $SIG{$sig} = $handler;
254 }
255 }
256  
257 # %old_sig = CLI->_set_signal_handlers();
258 #
259 # Set signal handlers to ensure proper terminal/CLI handling in the
260 # face of various signals (^C ^\ ^Z).
261 #
262 # Return a hash with the overridden signal handlers.
263 #
264 6 ▶sub _set_signal_handlers {
Subroutine _set_signal_handlers: called CC=4 SCAR=25.3
265 ## no critic (RequireLocalizedPunctuationVars)
266 6 my ($self) = @_;
267  
268 6 my %old_SIG;
269  
270 @old_SIG{qw( HUP INT QUIT ALRM TERM CONT )}
271 6 = @SIG{qw( HUP INT QUIT ALRM TERM CONT )};
272  
273 6 my $most_recent_signal = q{};
274  
275 # The generic signal handler will attempt to re-throw the signal, after
276 # putting the terminal in the correct state. Any previously set signal
277 # handlers should then be triggered.
278 0 ▶ my $generic_handler = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
279 0 my ($signal) = @_;
280  
281 0 my $this_handler = $SIG{$signal};
282 0 ▶ my $handler = $old_SIG{$signal} // q{};
Condition0%0/2
$old_SIG{$signal} // ""
l!l
00
MC/DC0%0/1
$old_SIG{$signal} // ""
$old_SIG{$signal}
Truth table0%0/2
A
A= $old_SIG{$signal}
Aresult
11
00
283  
284 0 $self->deprep_terminal();
285 0 $self->_restore_keyboard_signals();
286  
287 0 ▶ if ( $handler eq q{} or $handler eq 'DEFAULT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler eq "" or $handler eq "DEFAULT") 0 0 0
Condition0%0/3
$handler eq "" or $handler eq "DEFAULT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$handler eq "" or $handler eq "DEFAULT"
$handler eq "" $handler eq "DEFAULT"
Truth table0%0/3
A or B
A= $handler eq ""B= $handler eq "DEFAULT"
ABresult
1X1
011
000
288  
289 # We've de-prepped the terminal, now reset the signal handler
290 # and re-issue the signal. Since we're inside a signal handler
291 # the re-thrown signal will be deferred until we return from
292 # this. For HUP, QUIT, ALRM, and TERM, this will result in
293 # termination of the process, so leave the terminal in a
294 # de-prepped state.
295 0 $SIG{$signal} = 'DEFAULT';
296 0 kill $signal, $$;
297 0 return;
298 }
299  
300 # Call old signal handler and re-prep the terminal.
301 0 ▶ if ( ref $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler) 0 0 0
302 0 local ( $SIG{$signal} ) = $handler;
303 0 $handler->( $signal, @_ );
304 }
305  
306 0 $self->prep_terminal(1);
307 0 $self->_set_ignore_keyboard_signals();
308 0 $self->forced_update_display();
309 0 return;
310 6 };
311  
312 6 ▶ if ( $self->ReadLine =~ /::Gnu$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ReadLine =~ /::Gnu$/ux) { } 0 6 6
313 0 for my $sig (qw( HUP QUIT ALRM TERM )) {
314 0 ▶ if ( ref $old_SIG{$sig} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old_SIG{$sig}) 0 0 0
315 0 $SIG{$sig} = $generic_handler;
316 }
317 }
318 }
319 else {
320 6 $SIG{HUP} = $SIG{QUIT} = $SIG{ALRM} = $SIG{TERM} = $generic_handler;
321 }
322  
323 # The INT signal handler; slightly different from
324 # the generic one: we abort the current input line.
325 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
326 0 my ($signal) = @_;
327 0 ▶ if ( $self->ReadLine =~ /::Gnu$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ReadLine =~ /::Gnu$/ux) 0 0 0
328 0 $self->crlf;
329 }
330 0 $self->replace_line(q{});
331 0 $generic_handler->($signal);
332 0 return 1;
333 6 };
334  
335 # The CONT signal handler.
336 # In case we get suspended, make sure we redraw the CLI on wake-up.
337 0 ▶ $SIG{CONT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
338 0 my ($signal) = @_;
339 0 $most_recent_signal = $signal;
340 0 ▶ $old_SIG{$signal}->(@_) if ref $old_SIG{$signal};
Branch: 1 branch
BranchTrueFalseTotal
if ref $old_SIG{$signal} 0 0 0
341 0 $self->_set_ignore_keyboard_signals();
342 0 return 1;
343 6 };
344  
345 # GNU readline will call the signal_event_hook after handling
346 # a signal, so use this to force a display update after a 'CONT'
347 # signal.
348 0 ▶ $self->Attribs->{signal_event_hook} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
349 0 ▶ if ( $most_recent_signal eq 'CONT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($most_recent_signal eq "CONT") 0 0 0
350 0 $self->forced_update_display();
351 }
352 0 $most_recent_signal = '';
353 0 return 1;
354 6 };
355  
356 6 return %old_SIG;
357 }
358  
359 # Install stubs for common GRL methods.
360 8 ▶sub _install_stubs {
Subroutine _install_stubs: called CC=3 SCAR=12.6
361 8 my ($self) = @_;
362  
363 8 ▶ return $self if $self->ReadLine =~ /::Gnu$/x;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ReadLine =~ /::Gnu$/ux 0 8 8
364  
365 21 ▶ no warnings 'once'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
366  
367 8 ▶ *{free_line_state} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
368 0 ▶ *{crlf} = sub { $self->OUT->print("\n") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
369  
370 7 ▶ *{get_screen_size} = sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
371 7 my ( $width, $height ) = Term::ReadKey::GetTerminalSize($Term_FH);
372 0 return ( $height, $width );
373 8 };
374  
375 8 ▶ if ( $self->ReadLine !~ /::Perl$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ReadLine =~ /::Perl$/ux) 8 0 8
376 *{replace_line} = *{prep_terminal} = *{deprep_terminal} =
377 8 ▶ *{forced_update_display} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
378  
379 8 return $self;
380 }
381  
382 0 *{replace_line} = \&_perl_replace_line;
383 0 *{prep_terminal} = \&_perl_prep_terminal;
384 0 *{deprep_terminal} = \&_perl_deprep_terminal;
385 0 *{forced_update_display} = \&_perl_forced_update_display;
386  
387 0 return $self;
388 }
389  
390 # Term::ReadLine::Perl implementations of GRL methods.
391 0 ▶sub _perl_prep_terminal { readline::SetTTY(); return; }
Subroutine _perl_prep_terminal: not called CC=1 SCAR=6.9
392 0 ▶sub _perl_deprep_terminal { readline::ResetTTY(); return; }
Subroutine _perl_deprep_terminal: not called CC=1 SCAR=6.9
393 0 ▶sub _perl_forced_update_display { readline::redisplay(); return; }
Subroutine _perl_forced_update_display: not called CC=1 SCAR=6.9
394  
395 0 ▶sub _perl_replace_line {
Subroutine _perl_replace_line: not called CC=3 SCAR=24.8
396 0 my ( $self, $line ) = @_;
397 0 ▶ $line //= q{};
Condition0%0/2
$line //= ""
l!l
00
MC/DC0%0/1
$line //= ""
$line
Truth table0%0/2
A
A= $line
Aresult
11
00
398 ## no critic (ProhibitPackageVars)
399 0 $readline::line = $line;
400 0 ▶ $readline::D = length($line) if $readline::D > length($line);
Branch: 1 branch
BranchTrueFalseTotal
if $readline::D > length $line 0 0 0
401 0 return;
402 }
403  
404 2 ▶sub ReadHistory {
Subroutine ReadHistory: called CC=6 SCAR=18.0
Pod documented
405 2 my ( $self, $hist_file ) = @_;
406  
407 2 ▶ if ( $self->Features->{'readHistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"readHistory"}) 0 2 2
408 0 return $self->SUPER::ReadHistory($hist_file);
409 }
410  
411 2 ▶ open my $fh, '<', $hist_file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $hist_file or return 1 1 2
412  
413 1 my @history;
414 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = CORE::readline $fh)) 5 1 6
415 5 ▶ next if $_ eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "\n" 1 4 5
416 4 chomp;
417 4 ▶ shift @history if @history == $History_Size;
Branch: 1 branch
BranchTrueFalseTotal
if @history == $History_Size 0 4 4
418 4 push @history, $_;
419 }
420 1 $fh->close;
421  
422 1 $self->term->SetHistory(@history);
423 1 return 1;
424 }
425  
426 2 ▶sub WriteHistory {
Subroutine WriteHistory: called CC=5 SCAR=16.8
Pod documented
427 2 my ( $self, $hist_file ) = @_;
428  
429 2 ▶ if ( $self->Features->{'writeHistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"writeHistory"}) 0 2 2
430 0 return $self->SUPER::WriteHistory($hist_file);
431 }
432  
433 2 ▶ open my $fh, '>', $hist_file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $hist_file or return 1 1 2
434 1 ▶ print $fh map {"$_\n"} $self->term->GetHistory or return;
Branch: 1 branch
BranchTrueFalseTotal
print $fh map({"$_\n";} $self->term->GetHistory) or return 1 0 1
435 1 ▶ $fh->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or return 1 0 1
436 1 return 1;
437 }
438  
439 21 *{stifle_history} = \&StifleHistory;
440  
441 8 ▶sub StifleHistory {
Subroutine StifleHistory: called CC=7 SCAR=26.8
Pod documented
442 8 my ( $self, $max ) = @_;
443  
444 8 ▶ if ( $self->Features->{'stiflehistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"stiflehistory"}) 0 8 8
445 0 return $self->SUPER::StifleHistory($max);
446 }
447  
448 8 ▶ $max //= $HIST_LIMIT;
Condition33%1/3
$max //= $HIST_LIMIT
l!l&&r!l&&!r
800
MC/DC0%0/2
$max //= $HIST_LIMIT
$max $HIST_LIMIT
Truth table33%1/3
A //= B
A= $maxB= $HIST_LIMIT
ABresult
1X1
011
000
449 8 ▶ $max = 0 if $max <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max <= 0 0 8 8
450  
451 8 ▶ if ( $self->ReadLine =~ /::Perl$/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ReadLine =~ /::Perl$/ux) 0 8 8
452 ## no critic (ProhibitPackageVars)
453 0 $readline::rl_MaxHistorySize = $max;
454 0 my $cur = int @readline::rl_History;
455 0 ▶ if ( $cur > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur > $max) 0 0 0
456 0 splice( @readline::rl_History, 0, -$max );
457 0 $readline::rl_HistoryIndex -= ( $cur - $max );
458 }
459 0 return $max;
460 }
461  
462 8 ▶ splice( @History, 0, -$max ) if @History > $max;
Branch: 1 branch
BranchTrueFalseTotal
if @History > $max 0 8 8
463 8 $History_Size = $max;
464 8 return $max;
465 }
466  
467 2 ▶sub GetHistory {
Subroutine GetHistory: called CC=2 SCAR=7.6
Pod documented
468 2 my ($self) = @_;
469  
470 2 ▶ if ( $self->Features->{'getHistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"getHistory"}) 0 2 2
471 0 return $self->SUPER::GetHistory();
472 }
473 2 return @History;
474 }
475  
476 1 ▶sub SetHistory {
Subroutine SetHistory: called CC=3 SCAR=11.8
Pod documented
477 1 my ( $self, @l ) = @_;
478  
479 1 ▶ splice( @l, 0, -$History_Size ) if @l > $History_Size;
Branch: 1 branch
BranchTrueFalseTotal
if @l > $History_Size 0 1 1
480  
481 1 ▶ if ( $self->Features->{'setHistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"setHistory"}) 0 1 1
482 0 return $self->SUPER::SetHistory(@l);
483 }
484  
485 1 @History = @l;
486  
487 1 return int(@History);
488 }
489  
490 5 ▶sub AddHistory {
Subroutine AddHistory: called CC=3 SCAR=11.8
Pod documented
491 5 my ( $self, @lines ) = @_;
492  
493 5 ▶ if ( $self->Features->{'addHistory'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Features->{"addHistory"}) 0 5 5
494 0 return $self->SUPER::AddHistory(@lines);
495 }
496  
497 5 push @History, @lines;
498 5 ▶ splice( @History, 0, -$History_Size ) if int(@History) > $History_Size;
Branch: 1 branch
BranchTrueFalseTotal
if int @History > $History_Size 0 5 5
499 5 return;
500 }
501  
502 21 1;
503  
504 __END__
« CLI/Util.pm ↑ Summary CLI/Role/HelpText.pm »