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), <sbakker@cpan.org>
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.061000;
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 sub new {
58 10 ▶ my ( $class, @args ) = @_;
Subroutine new: called CC=5 SCAR=16.6
Pod documented
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 sub completion_quote_character {
84 36 ▶ my ($self) = @_;
Subroutine completion_quote_character: called CC=2 SCAR=7.1
Pod documented
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 sub ignore_keyboard_signals {
90 9 ▶ my ( $self, @args ) = @_;
Subroutine ignore_keyboard_signals: called CC=4 SCAR=14.2
Pod documented
91 9 foreach my $signame (@args) {
92 10 ▶ my $charname = $Sig2KeyName{$signame} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $charname = $Sig2KeyName{$signame} or next 1 9 10
93 9 ▶ $Original_KB_Signals{$charname} or next;
Branch: 1 branch
BranchTrueFalseTotal
$Original_KB_Signals{$charname} or next 9 0 9
94 0 $Ignore_KB_Signals{$charname} = q{};
95 }
96 9 return;
97 }
98  
99 sub no_ignore_keyboard_signals {
100 1 ▶ my ( $self, @args ) = @_;
Subroutine no_ignore_keyboard_signals: called CC=4 SCAR=14.2
Pod documented
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 1 0 1
104 0 delete $Ignore_KB_Signals{$charname};
105 }
106 1 return;
107 }
108  
109 sub _set_ignore_keyboard_signals {
110 6 ▶ my ($self) = @_;
Subroutine _set_ignore_keyboard_signals: called CC=2 SCAR=9.2
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 sub _restore_keyboard_signals {
117 6 ▶ my ($self) = @_;
Subroutine _restore_keyboard_signals: called CC=2 SCAR=9.2
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 sub reset_ignore_keyboard_signals {
124 8 ▶ my ($self) = @_;
Subroutine reset_ignore_keyboard_signals: called CC=1 SCAR=0.0
Pod documented
125 8 %Ignore_KB_Signals = ();
126 8 $self->ignore_keyboard_signals(@Default_Ignore_KB_Signals);
127 8 return;
128 }
129  
130 sub term_width {
131 7 ▶ my ($self) = @_;
Subroutine term_width: called CC=1 SCAR=0.4
Pod documented
132 7 my ( $rows, $cols ) = $self->get_screen_size();
133 0 return $cols;
134 }
135  
136 sub term_height {
137 0 ▶ my ($self) = @_;
Subroutine term_height: not called CC=1 SCAR=6.9
Pod documented
138 0 my ( $rows, $cols ) = $self->get_screen_size();
139 0 return $rows;
140 }
141  
142 sub echo_signal_char {
143 0 ▶ my ( $self, $sig_arg ) = @_;
Subroutine echo_signal_char: not called CC=8 SCAR=42.8
Pod documented
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 sub _escape_str { ## no critic (ProhibitUnusedPrivateSubroutines)
172 0 ▶ my ( $self, $str ) = @_;
Subroutine _escape_str: not called CC=1 SCAR=6.9
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 sub _prepare_prompt {
188 6 ▶ my ( $self, $prompt ) = @_;
Subroutine _prepare_prompt: called CC=7 SCAR=37.0
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 sub readline { ## no critic (ProhibitBuiltinHomonyms)
226 6 ▶ my ( $self, $prompt ) = @_;
Subroutine readline: called CC=4 SCAR=13.9
Pod documented
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 sub _restore_signal_handlers {
250 6 ▶ my ($self, %old_SIG) = @_;
Subroutine _restore_signal_handlers: called CC=2 SCAR=6.9
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 sub _set_signal_handlers {
265 ## no critic (RequireLocalizedPunctuationVars)
266 6 ▶ my ($self) = @_;
Subroutine _set_signal_handlers: called CC=4 SCAR=25.3
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 my $generic_handler = sub {
279 0 ▶ my ($signal) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
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 $SIG{INT} = sub {
326 0 ▶ my ($signal) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 $SIG{CONT} = sub {
338 0 ▶ my ($signal) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 $self->Attribs->{signal_event_hook} = sub {
349 0 ▶ if ( $most_recent_signal eq 'CONT' ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 sub _install_stubs {
361 8 ▶ my ($self) = @_;
Subroutine _install_stubs: called CC=3 SCAR=12.6
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 8 ▶ *{crlf} = sub { $self->OUT->print("\n") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
369  
370 *{get_screen_size} = sub {
371 7 ▶ my ( $width, $height ) = Term::ReadKey::GetTerminalSize($Term_FH);
Subroutine __ANON__: called CC=1 SCAR=1.2
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 sub _perl_replace_line {
396 0 ▶ my ( $self, $line ) = @_;
Subroutine _perl_replace_line: not called CC=3 SCAR=24.8
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 sub ReadHistory {
405 2 ▶ my ( $self, $hist_file ) = @_;
Subroutine ReadHistory: called CC=6 SCAR=18.0
Pod documented
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 sub WriteHistory {
427 2 ▶ my ( $self, $hist_file ) = @_;
Subroutine WriteHistory: called CC=5 SCAR=16.8
Pod documented
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 0 1 1
435 1 ▶ $fh->close or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or return 0 1 1
436 1 return 1;
437 }
438  
439 21 *{stifle_history} = \&StifleHistory;
440  
441 sub StifleHistory {
442 8 ▶ my ( $self, $max ) = @_;
Subroutine StifleHistory: called CC=7 SCAR=26.8
Pod documented
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 sub GetHistory {
468 2 ▶ my ($self) = @_;
Subroutine GetHistory: called CC=2 SCAR=7.6
Pod documented
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 sub SetHistory {
477 1 ▶ my ( $self, @l ) = @_;
Subroutine SetHistory: called CC=3 SCAR=11.8
Pod documented
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 sub AddHistory {
491 5 ▶ my ( $self, @lines ) = @_;
Subroutine AddHistory: called CC=3 SCAR=11.8
Pod documented
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__
505  
506 =pod
507  
508 =head1 NAME
509  
510 Term::CLI::ReadLine - Term::ReadLine compatibility layer for Term::CLI
511  
512 =head1 VERSION
513  
514 version 0.061000
515  
516 =head1 SYNOPSIS
517  
518 use Term::CLI::ReadLine;
519  
520 sub initialise {
521 my $term = Term::CLI::ReadLine->new( ... );
522 ... # Use Term::ReadLine methods on $term.
523 }
524  
525 # The original $term reference is now out of scope, but
526 # we can get a reference to it again:
527  
528 sub somewhere_else {
529 my $term = Term::CLI::ReadLine->term;
530 ... # Use Term::ReadLine methods on $term.
531 }
532  
533 =head1 DESCRIPTION
534  
535 This class provides a compatibility layer between L<Term::ReadLine>(3p)
536 and L<Term::CLI>(3p). If L<Term::ReadLine::Gnu>(3p) is not loaded as the
537 C<Term::ReadLine> implementation, this class will compensate for the lack
538 of certain functions by replacing or wrapping methods that are needed
539 by the rest of the L<Term::CLI>(3p) classes.
540  
541 The ultimate purpose is to behave as consistently as possible regardless
542 of the C<Term::ReadLine> interface that has been loaded.
543  
544 This class inherits from L<Term::ReadLine> and behaves as a singleton
545 with a class accessor to access that single instance, because
546 even though L<Term::ReadLine>(3p) has an object-oriented interface,
547 the L<Term::ReadLine::Gnu>(3p) and L<Term::ReadLine::Perl>(3p) modules
548 really only keep a single instance around (if you create multiple
549 L<Term::ReadLine> objects, all parameters and history are shared).
550  
551 =head1 CONSTRUCTORS
552  
553 =over
554  
555 =item B<new> ( ... )
556 X<new>
557  
558 Create a new L<Term::CLI::ReadLine>(3p) object and return a reference to it.
559  
560 Arguments are identical to L<Term::ReadLine>(3p).
561  
562 A reference to the newly created object is stored internally and can be
563 retrieved later with the L<term|/term> class method. Note that repeated calls
564 to C<new> will reset this internal reference.
565  
566 =back
567  
568 =head1 METHODS
569  
570 See L<Term::ReadLine>(3p), L<Term::ReadLine::Gnu>(3p) and/or
571 L<Term::ReadLine::Perl> for the inherited methods.
572  
573 =over
574  
575 =item B<completion_quote_character>
576 X<completion_quote_character>
577  
578 In a L<Term::ReadLine::Gnu|Term::ReadLine::Gnu> environment this returns
579 the C<rl_completion_quote_character>. This value is set during completion
580 if the text to be completed has an open quote. Consider the case:
581  
582 foo 'bar <TAB>
583  
584 When the completion function is called, the C<rl_completion_quote_character>
585 will contain a single quote, C<'>.
586  
587 For non-GNU ReadLine backends, this function returns an empty string.
588  
589 =item B<echo_signal_char> ( I<signal> )
590 X<echo_signal_char>
591  
592 Print the character that generates a particular signal when entered from
593 the keyboard (e.g. C<^C> for keyboard interrupt).
594  
595 This method also accepts a signal name instead of a signal number. It only
596 works for C<INT> (2), C<QUIT> (3), and C<TSTP> (20) signals as these are
597 the only ones that can be entered from a keyboard.
598  
599 If L<Term::ReadLine::Gnu> is loaded, this method wraps around the method of
600 the same name in C<Term::ReadLine::Gnu> (translating a signal name to a
601 number first). For other C<Term::ReadLine> implementations, it emulates the
602 C<Term::ReadLine::Gnu> behaviour.
603  
604 =item B<readline> ( I<prompt> )
605 X<readline>
606  
607 Wrap around the original L<Term::ReadLine's readline|Term::ReadLine/readline>
608 with custom signal handling, see the
609 L<CAVEATS section in Term::CLI|Term::CLI/CAVEATS>.
610  
611 This also calls C<AddHistory> if C<autohistory> is not set in C<Features>.
612  
613 =item B<term_width>
614 X<term_width>
615  
616 Return the width of the terminal in characters, as given by
617 L<Term::ReadLine>.
618  
619 =item B<term_height>
620 X<term_height>
621  
622 Return the height of the terminal in characters, as given by
623 L<Term::ReadLine>.
624  
625 =item B<ignore_keyboard_signals> ( I<SIGNAME>, ... )
626 X<ignore_keyboard_signals>
627  
628 Ensure that I<SIGNAME> signals cannot be entered from the
629 keyboard. I<SIGNAME> should be the name of a signal that
630 can be entered from the keyboard, i.e. one of:
631 C<INT>, C<QUIT>, C<TSTP>.
632  
633 By default, the C<QUIT> keyboard signal is already disabled.
634  
635 Notes:
636  
637 =over
638  
639 =item 1.
640  
641 This will only disable the keys for the given signals
642 I<during> a C<readline> operation. Outside of that, they will still
643 generate signals.
644  
645 =item 2.
646  
647 This only disables the keyboard sequences, not the actual signals
648 themselves (i.e. you can still C<kill -3 PID> from another terminal.
649  
650 =item 3.
651  
652 Disabling the C<INT> key will cause I<Ctrl-C> to no longer discard the
653 input line under L<Term::ReadLine::Gnu>; it I<will> discard it under
654 L<Term::ReadLine::Perl>! It is therefore recommended to just set
655 C<$SIG{INT}> to C<IGNORE> instead.
656  
657 =item 4.
658  
659 Disabling the C<TSTP> key works under L<Term::ReadLine::Gnu>, but
660 not under L<Term::ReadLine::Perl>. The latter maps the key in raw
661 mode and explicitly sends a C<TSTP> signal to itself.
662  
663 =back
664  
665 See also L<SIGNAL HANDLING|/SIGNAL HANDLING> below.
666  
667 =item B<no_ignore_keyboard_signals> ( I<SIGNAME>, ... )
668 X<no_ignore_keyboard_signals>
669  
670 (Re-)Enable keyboard generation for I<SIGNAME> signals.
671 See L<ignore_keyboard_signals|/ignore_keyboard_signals> above for
672 valid I<SIGNAME> values.
673  
674 =item B<reset_ignore_keyboard_signals>
675 X<reset_ignore_keyboard_signals>
676  
677 Reset all keyboard signal generation to the defaults.
678  
679 =item B<AddHistory> ( I<line>, ... )
680 X<AddHistory>
681  
682 =item B<GetHistory>
683 X<GetHistory>
684  
685 =item B<ReadHistory> ( I<file> )
686 X<ReadHistory>
687  
688 =item B<SetHistory> ( I<line>, ... )
689 X<SetHistory>
690  
691 =item B<StifleHistory> ( I<max_lines> )
692 X<StifleHistory>
693  
694 =item B<stifle_history> ( I<max_lines> )
695 X<stifle_history>
696  
697 =item B<WriteHistory> ( I<file> )
698 X<WriteHistory>
699  
700 Depending on the underlying C<Term::ReadLine> implementation, these will
701 either call the parent class's method, or implement a proper emulation.
702  
703 In the case of C<Term::ReadLine::Perl>, this means that C<ReadHistory>
704 and C<WriteHistory> implement their own file I/O read/write (because
705 C<Term::ReadLine::Perl> doesn't provide them); furthermore, C<StifleHistory>
706 uses knowledge of C<Term::ReadLine::Perl>'s internals to manipulate the
707 history.
708  
709 In cases where history is not supported at all (e.g. C<Term::ReadLine::Stub>,
710 the history list is kept in this object and manipulated.
711  
712 =back
713  
714 =head1 STUB METHODS
715  
716 If C<Term::ReadLine> is I<not> using the GNU ReadLine library, this object
717 provides stubs for a few GNU ReadLine methods:
718  
719 =over
720  
721 =item B<free_line_state>
722 X<free_line_state>
723  
724 =item B<forced_update_display>
725 X<forced_update_display>
726  
727 If L<Term::ReadLine::Perl> is loaded, this will use knowledge of
728 its internals to force an redraw of the input line.
729  
730 =item B<crlf>
731 X<crlf>
732  
733 Prints a newline to the terminal's output.
734  
735 =item B<replace_line> ( I<str> )
736 X<replace_line>
737  
738 If L<Term::ReadLine::Perl> is loaded, this will use knowledge of
739 its internals to replace the current input line with I<str>.
740  
741 =item B<deprep_terminal>
742 X<deprep_terminal>
743  
744 =item B<prep_terminal>
745 X<prep_terminal>
746  
747 If L<Term::ReadLine::Perl> is loaded, this will use knowledge of
748 its internals to either restore (deprep) terminal settings to
749 what they were before calling C<readline>, or to set them to what
750 C<readline> uses. You will rarely (if ever) need these, since
751 the ReadLine libraries usually take care if this themselves.
752  
753 One exception to this is in signal handlers: C<Term::CLI::ReadLine>
754 calls these methods during its signal handling.
755  
756 =item B<get_screen_size>
757 X<get_screen_size>
758  
759 Use C<Term::ReadKey::GetTerminalSize> to get the appropriate
760 dimensions and return them as (I<height>, I<width>).
761  
762 =back
763  
764 =head1 CLASS METHODS
765  
766 =over
767  
768 =item B<term>
769 X<term>
770  
771 Return the latest C<Term::CLI::ReadLine> object created.
772  
773 =back
774  
775 =head1 SIGNAL HANDLING
776  
777 The class sets its own signal handlers in the L<readline|/readline>
778 function where necessary.
779  
780 The following signals may be caught:
781 C<ALRM>, C<CONT>, C<HUP>, C<INT>, C<QUIT>, C<TERM>.
782  
783 The signal handlers will:
784  
785 =over
786  
787 =item *
788  
789 Restore the terminal to a "sane" state, i.e. the state it was in before
790 C<readline> was called (the C<CONT> signal being an exception to this
791 rule).
792  
793 =item *
794  
795 If any signal handler was set prior to the call to C<readline>, it will
796 be called and if control returns L<Term::CLI::ReadLine>'s signal handler,
797 the terminal will be set back to the state that C<readline> expects it to
798 be in.
799  
800 =item *
801  
802 If the signal handler was previously set to C<DEFAULT>, it is restored
803 as C<DEFAULT> and the signal is re-thrown, so the default actions (abnormal
804 exit and possible core dump) can take place.
805  
806 =back
807  
808 Just how and when these "wrapper" signal handlers are installed depends on
809 the selected C<Term::ReadLine> implementation. The
810 L<Gnu|Term::ReadLine::Gnu> backend doesn't require separate handlers
811 for signals that are set to C<IGNORE> or C<DEFAULT>. The
812 L<Perl|Term::ReadLine::Perl> backend does require some wrapping.
813  
814 The C<INT> signal is always wrapped to ensure that the current input
815 line is discarded and a newline is emitted.
816  
817 =head2 Keyboard signals
818  
819 One subtle difference between the
820 L<Term::ReadLine::Gnu> and L<Term::ReadLine::Perl> is in keyboard-generated
821 signal handling (interrupt, quit, suspend).
822  
823 =over
824  
825 =item *
826  
827 L<Term::ReadLine::Perl> disables keyboard-generated signals. When it
828 reads a I<Ctrl-C>, it will send itself an C<INT> signal, when it
829 sees a I<Ctrl-Z>, it will send a C<TSTP> signal; the "quit" key
830 I<Ctrl-\> is simply ignored.
831  
832 =item *
833  
834 L<Term::ReadLine::Gnu> leaves keyboard-generated signals enabled and
835 sets signal handlers to catch them.
836  
837 =back
838  
839 This subtle difference means that:
840  
841 =over
842  
843 =item *
844  
845 It is impossible to have I<Ctrl-\> generate a C<QUIT> signal under
846 L<Term::ReadLine::Perl>.
847  
848 =item *
849  
850 It is impossible to disable I<Ctrl-Z> through L</ignore_keyboard_signals>
851 under L<Term::ReadLine::Perl>.
852  
853 =item *
854  
855 Disabling I<Ctrl-C> through L</ignore_keyboard_signals> will completely
856 disable I<Ctrl-C> under L<Term::ReadLine::Gnu> (will not discard the
857 input line), but not L<Term::ReadLine::Perl>.
858  
859 =back
860  
861 For this reason, the module by default ignores the C<QUIT> key sequence.
862  
863 =head2 Recommendations
864  
865 To behave as consistently as possible across the C<Term::ReadLine> backends,
866 the following is best if you don't want keyboard signals to kill or stop
867 the program:
868  
869 =over
870  
871 =item 1.
872  
873 Set C<$SIG{INT}> to C<IGNORE>.
874  
875 =item 2.
876  
877 Set C<$SIG{TSTP}> to C<IGNORE>.
878  
879 =item 3.
880  
881 Ignore keyboard signal C<QUIT> (already default).
882  
883 =back
884  
885 =head1 SEE ALSO
886  
887 L<Term::CLI>(3p),
888 L<Term::ReadLine>(3p),
889 L<Term::ReadLine::Gnu>(3p),
890 L<Term::ReadLine::Perl>(3p).
891  
892 =head1 AUTHOR
893  
894 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018-2021.
895  
896 =head1 COPYRIGHT AND LICENSE
897  
898 Copyright (c) 2018 Steven Bakker
899  
900 This module is free software; you can redistribute it and/or modify
901 it under the same terms as Perl itself. See "perldoc perlartistic."
902  
903 This software is distributed in the hope that it will be useful,
904 but WITHOUT ANY WARRANTY; without even the implied warranty of
905 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
906  
907 =cut
« CLI/Util.pm ↑ Summary CLI/Role/HelpText.pm »