Tiny.pm

filter: Statementstmt 6.7% 24 / 357 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 14.2% 6 / 42 Podpod 81.8% 18 / 22 totaltotal 8.6% 48 / 553 CC 60 SCAR 41.6
CC
60
Cov
5%
readline
240.0
newTTY
132.0
readkey
90.0
CRAP 63.9
↑ Summary Tiny/readline.pm »
1 package Term::ReadLine::Tiny;
2 =head1 NAME
3  
4 Term::ReadLine::Tiny - Tiny implementation of ReadLine
5  
6 =head1 VERSION
7  
8 version 1.08
9  
10 =head1 SYNOPSIS
11  
12 use Term::ReadLine::Tiny;
13
14 $term = Term::ReadLine::Tiny->new();
15 while ( defined($_ = $term->readline("Prompt: ")) )
16 {
17 print "$_\n";
18 }
19 print "\n";
20
21 $s = "";
22 while ( defined($_ = $term->readkey(1)) )
23 {
24 $s .= $_;
25 }
26 print "\n$s\n";
27  
28 =head1 DESCRIPTION
29  
30 This package is a native perls implementation of ReadLine that doesn't need any library such as 'Gnu ReadLine'.
31 Also fully supports UTF-8, details in L<UTF-8 section|https://metacpan.org/pod/Term::ReadLine::Tiny#UTF-8>.
32  
33 =head2 Keys
34  
35 B<C<Enter> or C<^J> or C<^M>:> Gets input line. Returns the line unless C<EOF> or aborting or error, otherwise undef.
36  
37 B<C<BackSpace> or C<^H> or C<^?>:> Deletes one character behind cursor.
38  
39 B<C<UpArrow>:> Changes line to previous history line.
40  
41 B<C<DownArrow>:> Changes line to next history line.
42  
43 B<C<RightArrow>:> Moves cursor forward to one character.
44  
45 B<C<LeftArrow>:> Moves cursor back to one character.
46  
47 B<C<Home> or C<^A>:> Moves cursor to the start of the line.
48  
49 B<C<End> or C<^E>:> Moves cursor to the end of the line.
50  
51 B<C<PageUp>:> Change line to first line of history.
52  
53 B<C<PageDown>:> Change line to latest line of history.
54  
55 B<C<Insert>:> Switch typing mode between insert and overwrite.
56  
57 B<C<Delete>:> Deletes one character at cursor. Does nothing if no character at cursor.
58  
59 B<C<Tab> or C<^I>:> Completes line automatically by history.
60  
61 B<C<^D>:> Aborts the operation. Returns C<undef>.
62  
63 =cut
64 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use feature qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶no if ($] >= 5.018), 'warnings' => 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 require utf8;
70 1 require PerlIO;
71 1 require Term::ReadLine;
72 1 require Term::ReadKey;
73  
74  
75 BEGIN
76 {
77 1 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 our $VERSION = '1.08';
79 1 our @ISA = qw(Exporter);
80 1 our @EXPORT = qw();
81 1 our @EXPORT_OK = qw();
82 }
83  
84  
85 =head1 Standard Methods and Functions
86  
87 =cut
88  
89 =head2 ReadLine()
90  
91 returns the actual package that executes the commands. If this package is used, the value is C<Term::ReadLine::Tiny>.
92  
93 =cut
94 sub ReadLine
95 {
96 0 ▶ return __PACKAGE__;
Subroutine ReadLine: not called CC=1 SCAR=6.9
Pod documented
97 }
98  
99 =head2 new([$appname[, IN[, OUT]]])
100  
101 returns the handle for subsequent calls to following functions.
102 Argument I<appname> is the name of the application B<but not supported yet>.
103 Optionally can be followed by two arguments for IN and OUT filehandles. These arguments should be globs.
104  
105 This routine may also get called via C<Term::ReadLine-E<gt>new()> if you have $ENV{PERL_RL} set to 'Tiny'.
106  
107 =cut
108 sub new
109 {
110 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($appname, $IN, $OUT) = @_;
112 0 my $self = {};
113 0 bless $self, $class;
114  
115 0 $self->{readmode} = '';
116 0 $self->{history} = [];
117  
118 0 $self->{features} = {};
119 #$self->{features}->{appname} = $appname;
120 0 $self->{features}->{addhistory} = 1;
121 0 $self->{features}->{minline} = 1;
122 0 $self->{features}->{autohistory} = 1;
123 0 $self->{features}->{gethistory} = 1;
124 0 $self->{features}->{sethistory} = 1;
125 0 $self->{features}->{changehistory} = 1;
126 0 $self->{features}->{utf8} = 1;
127  
128 0 $self->newTTY($IN, $OUT);
129  
130 0 return $self;
131 }
132  
133 sub DESTROY
134 {
135 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
136 0 ▶ if ($self->{readmode})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"readmode"}) 0 0 0
137 {
138 0 Term::ReadKey::ReadMode('restore', $self->{IN});
139 0 $self->{readmode} = '';
140 }
141 }
142  
143 =head2 readline([$prompt[, $default]])
144  
145 interactively gets an input line. Trailing newline is removed.
146  
147 Returns C<undef> on C<EOF>.
148  
149 =cut
150 sub readline
151 {
152 0 ▶ my $self = shift;
Subroutine readline: not called CC=15 SCAR=54.8
Pod documented
153 0 my ($prompt, $default) = @_;
154 0 ▶ $prompt = "" unless defined($prompt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt 0 0 0
155 0 ▶ $default = "" unless defined($default);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 0 0 0
156 my ($in, $out, $history, $minline, $changehistory) =
157 0 ($self->{IN}, $self->{OUT}, $self->{history}, $self->{features}->{minline}, $self->{features}->{changehistory});
158 0 ▶ unless (-t $in)
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $in) 0 0 0
159 {
160 0 my $line = <$in>;
161 0 ▶ chomp $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 0 0 0
162 0 return $line;
163 }
164 0 local $\ = undef;
165  
166 0 $self->{readmode} = 'cbreak';
167 0 Term::ReadKey::ReadMode($self->{readmode}, $self->{IN});
168  
169 0 my @line;
170 0 my ($line, $index) = ("", 0);
171 0 my $history_index;
172 0 my $ins_mode = 0;
173  
174 my $write = sub {
175 0 ▶ my ($text, $ins) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
176 0 my $s;
177 0 my @a = @line[$index..$#line];
178 0 my $a = substr($line, $index);
179 0 @line = @line[0..$index-1];
180 0 $line = substr($line, 0, $index);
181 0 print $out " ";
182 0 print $out "\e[D";
183 0 print $out "\e[J";
184 0 for my $c (split("", $text))
185 {
186 0 $s = encode_controlchar($c);
187 0 ▶ unless ($ins)
Branch: 1 branch
BranchTrueFalseTotal
if ($ins) { } 0 0 0
188 {
189 0 print $out $s;
190 0 push @line, $s;
191 0 $line .= $c;
192 } else
193 {
194 0 my $i = $index-length($line);
195 0 $a[$i] = $s;
196 0 substr($a, $i, 1) = $c;
197 }
198 0 $index++;
199 }
200 0 ▶ unless ($ins)
Branch: 1 branch
BranchTrueFalseTotal
if ($ins) { } 0 0 0
201 {
202 0 $s = join("", @a);
203 0 print $out $s;
204 0 print $out "\e[D" x length($s);
205 } else
206 {
207 0 $s = join("", @a);
208 0 print $out $s;
209 0 print $out "\e[D" x (length($s) - length(join("", @a[0..length($text)-1])));
210 }
211 0 push @line, @a;
212 0 $line .= $a;
213 0 ▶ if ($index >= length($line))
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= length $line) 0 0 0
214 {
215 0 print $out " ";
216 0 print $out "\e[D";
217 0 print $out "\e[J";
218 }
219 0 };
220 my $print = sub {
221 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 $write->($text, $ins_mode);
223 0 };
224 my $set = sub {
225 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 print $out "\e[D" x length(join("", @line[0..$index-1]));
227 0 print $out "\e[J";
228 0 @line = ();
229 0 $line = "";
230 0 $index = 0;
231 0 $write->($text);
232 0 };
233 my $backspace = sub {
234 0 ▶ return if $index <= 0;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $index <= 0 0 0 0
235 0 my @a = @line[$index..$#line];
236 0 my $a = substr($line, $index);
237 0 $index--;
238 0 print $out "\e[D" x length($line[$index]);
239 0 @line = @line[0..$index-1];
240 0 $line = substr($line, 0, $index);
241 0 $write->($a);
242 0 print $out "\e[D" x length(join("", @a));
243 0 $index -= scalar(@a);
244 0 };
245 my $delete = sub {
246 0 ▶ my @a = @line[$index+1..$#line];
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 my $a = substr($line, $index+1);
248 0 @line = @line[0..$index-1];
249 0 $line = substr($line, 0, $index);
250 0 $write->($a);
251 0 print $out "\e[D" x length(join("", @a));
252 0 $index -= scalar(@a);
253 0 };
254 my $home = sub {
255 0 ▶ print $out "\e[D" x length(join("", @line[0..$index-1]));
Subroutine __ANON__: not called CC=1 SCAR=6.9
256 0 $index = 0;
257 0 };
258 my $end = sub {
259 0 ▶ my @a = @line[$index..$#line];
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 0 my $a = substr($line, $index);
261 0 @line = @line[0..$index-1];
262 0 $line = substr($line, 0, $index);
263 0 $write->($a);
264 0 };
265 my $left = sub {
266 0 ▶ return if $index <= 0;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $index <= 0 0 0 0
267 0 print $out "\e[D" x length($line[$index-1]);
268 0 $index--;
269 0 };
270 my $right = sub {
271 0 ▶ return if $index >= length($line);
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $index >= length $line 0 0 0
272 0 print $out $line[$index];
273 0 $index++;
274 0 ▶ if ($index >= length($line))
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= length $line) { } 0 0 0
275 {
276 0 print $out " ";
277 0 print $out "\e[D";
278 0 print $out "\e[J";
279 } else
280 {
281 0 print $out $line[$index];
282 0 print $out "\e[D" x length($line[$index]);
283 }
284 0 };
285 my $up = sub {
286 0 ▶ return if $history_index <= 0;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $history_index <= 0 0 0 0
287 0 ▶ $history->[$history_index] = $line if $changehistory;
Branch: 1 branch
BranchTrueFalseTotal
if $changehistory 0 0 0
288 0 $history_index--;
289 0 $set->($history->[$history_index]);
290 0 };
291 my $down = sub {
292 0 ▶ return if $history_index >= $#$history;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $history_index >= $history->$#* 0 0 0
293 0 ▶ $history->[$history_index] = $line if $changehistory;
Branch: 1 branch
BranchTrueFalseTotal
if $changehistory 0 0 0
294 0 $history_index++;
295 0 $set->($history->[$history_index]);
296 0 };
297 my $pageup = sub {
298 0 ▶ return if $history_index <= 0;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $history_index <= 0 0 0 0
299 0 ▶ $history->[$history_index] = $line if $changehistory;
Branch: 1 branch
BranchTrueFalseTotal
if $changehistory 0 0 0
300 0 $history_index = 0;
301 0 $set->($history->[$history_index]);
302 0 };
303 my $pagedown = sub {
304 0 ▶ return if $history_index >= $#$history;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $history_index >= $history->$#* 0 0 0
305 0 ▶ $history->[$history_index] = $line if $changehistory;
Branch: 1 branch
BranchTrueFalseTotal
if $changehistory 0 0 0
306 0 $history_index = $#$history;
307 0 $set->($history->[$history_index]);
308 0 };
309  
310 0 print $prompt;
311 0 $set->($default);
312 0 push @$history, $line;
313 0 $history_index = $#$history;
314  
315 0 my $result = undef;
316 0 my ($char, $esc) = ("", undef);
317 0 ▶ while (defined($char = getc($in)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = getc $in)) 0 0 0
318 {
319 0 ▶ unless (defined($esc))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $esc) 0 0 0
320 {
321 0 given ($char)
322 {
323 when (/\e/)
324 0 {
325 0 $esc = "";
326 }
327 when (/\x01/) # ^A
328 0 {
329 0 $home->();
330 }
331 when (/\x04/) # ^D
332 0 {
333 0 $result = undef;
334 0 last;
335 }
336 when (/\x05/) # ^E
337 0 {
338 0 $end->();
339 }
340 when (/\t/) # ^I
341 0 {
342 0 ▶ for (my $i = $history_index; $i >= 0; $i--)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
343 {
344 0 ▶ if ($history->[$i] =~ /^$line/)
Branch: 1 branch
BranchTrueFalseTotal
if ($history->[$i] =~ /^$line/) 0 0 0
345 {
346 0 $set->($history->[$i]);
347 0 last;
348 }
349 }
350 }
351 when (/\n|\r/)
352 0 {
353 0 print $out $char;
354 0 $history->[$#$history] = $line;
355 0 ▶ pop @$history unless defined($minline) and length($line) >= $minline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minline and length $line >= $minline 0 0 0
Condition0%0/3
defined $minline and length $line >= $minline
!ll&&!rl&&r
000
MC/DC0%0/2
defined $minline and length $line >= $minline
defined $minline length $line >= $minline
Truth table0%0/3
A and B
A= defined $minlineB= length $line >= $minline
ABresult
0X0
100
111
356 0 $result = $line;
357 0 last;
358 }
359 when (/[\b]|\x7F/)
360 0 {
361 0 $backspace->();
362 }
363 when (/[\x00-\x1F]|\x7F/)
364 0 {
365 0 $print->($char);
366 }
367 default
368 0 {
369 0 $print->($char);
370 }
371 }
372 0 next;
373 }
374 0 $esc .= $char;
375 0 ▶ if ($esc =~ /^.\d?\D/)
Branch: 1 branch
BranchTrueFalseTotal
if ($esc =~ /^.\d?\D/) 0 0 0
376 {
377 0 given ($esc)
378 {
379 when (/^(\[|O)(A|0A)/)
380 0 {
381 0 $up->();
382 }
383 when (/^(\[|O)(B|0B)/)
384 0 {
385 0 $down->();
386 }
387 when (/^(\[|O)(C|0C)/)
388 0 {
389 0 $right->();
390 }
391 when (/^(\[|O)(D|0D)/)
392 0 {
393 0 $left->();
394 }
395 when (/^(\[|O)(F|0F)/)
396 0 {
397 0 $end->();
398 }
399 when (/^(\[|O)(H|0H)/)
400 0 {
401 0 $home->();
402 }
403 when (/^\[(\d)~/)
404 0 {
405 0 given ($1)
406 {
407 when (1)
408 0 {
409 0 $home->();
410 }
411 when (2)
412 0 {
413 0 $ins_mode = not $ins_mode;
414 }
415 when (3)
416 0 {
417 0 $delete->();
418 }
419 when (4)
420 0 {
421 0 $end->();
422 }
423 when (5)
424 0 {
425 0 $pageup->();
426 }
427 when (6)
428 0 {
429 0 $pagedown->();
430 }
431 when (7)
432 0 {
433 0 $home->();
434 }
435 when (8)
436 0 {
437 0 $end->();
438 }
439 default
440 0 {
441 #$print->("\e$esc");
442 }
443 }
444 }
445 default
446 0 {
447 #$print->("\e$esc");
448 }
449 }
450 0 $esc = undef;
451 }
452 }
453 0 ▶ utf8::encode($result) if defined($result) and utf8::is_utf8($result) and $self->{features}->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"} 0 0 0
Condition0%0/6
defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"}
!ll&&!rl&&r
000
defined $result and utf8::is_utf8($result)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"}
defined $result utf8::is_utf8($result) $self->{"features"}{"utf8"}
Truth table0%0/4
A and B and C
A= defined $resultB= utf8::is_utf8($result)C= $self->{"features"}{"utf8"}
ABCresult
0XX0
10X0
1100
1111
454  
455 0 Term::ReadKey::ReadMode('restore', $self->{IN});
456 0 $self->{readmode} = '';
457 0 return $result;
458 }
459  
460 =head2 addhistory($line1[, $line2[, ...]])
461  
462 B<AddHistory($line1[, $line2[, ...]])>
463  
464 adds lines to the history of input.
465  
466 =cut
467 sub addhistory
468 {
469 0 ▶ my $self = shift;
Subroutine addhistory: not called CC=3 SCAR=24.8
Pod documented
470 0 ▶ if (grep(":utf8", PerlIO::get_layers($self->{IN})))
Branch: 1 branch
BranchTrueFalseTotal
if (grep ":utf8", &PerlIO::get_layers($self->{"IN"})) 0 0 0
471 {
472 0 ▶ for (my $i = 0; $i < @_; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
473 {
474 0 utf8::decode($_[$i]);
475 }
476 }
477 0 push @{$self->{history}}, @_;
478 0 return (@_);
479 }
480 sub AddHistory
481 {
482 0 ▶ return addhistory(@_);
Subroutine AddHistory: not called CC=1 SCAR=6.9
Pod undocumented
483 }
484  
485 =head2 IN()
486  
487 returns the filehandle for input.
488  
489 =cut
490 sub IN
491 {
492 0 ▶ my $self = shift;
Subroutine IN: not called CC=1 SCAR=6.9
Pod documented
493 0 return $self->{IN};
494 }
495  
496 =head2 OUT()
497  
498 returns the filehandle for output.
499  
500 =cut
501 sub OUT
502 {
503 0 ▶ my $self = shift;
Subroutine OUT: not called CC=1 SCAR=6.9
Pod documented
504 0 return $self->{OUT};
505 }
506  
507 =head2 MinLine([$minline])
508  
509 B<minline([$minline])>
510  
511 If argument is specified, it is an advice on minimal size of line to be included into history.
512 C<undef> means do not include anything into history (autohistory off).
513  
514 Returns the old value.
515  
516 =cut
517 sub MinLine
518 {
519 0 ▶ my $self = shift;
Subroutine MinLine: not called CC=2 SCAR=17.9
Pod documented
520 0 my ($minline) = @_;
521 0 my $result = $self->{features}->{minline};
522 0 ▶ $self->{features}->{minline} = $minline if @_ >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 1 0 0 0
523 0 $self->{features}->{autohistory} = defined($self->{features}->{minline});
524 0 return $result;
525 }
526 sub minline
527 {
528 0 ▶ return MinLine(@_);
Subroutine minline: not called CC=1 SCAR=6.9
Pod undocumented
529 }
530  
531 =head2 findConsole()
532  
533 returns an array with two strings that give most appropriate names for files for input and output using conventions C<"<$in">, C<">out">.
534  
535 =cut
536 sub findConsole
537 {
538 0 ▶ return (Term::ReadLine::Stub::findConsole(@_));
Subroutine findConsole: not called CC=1 SCAR=6.9
Pod documented
539 }
540  
541 =head2 Attribs()
542  
543 returns a reference to a hash which describes internal configuration of the package. B<Not supported in this package.>
544  
545 =cut
546 sub Attribs
547 {
548 0 ▶ return {};
Subroutine Attribs: not called CC=1 SCAR=6.9
Pod documented
549 }
550  
551 =head2 Features()
552  
553 Returns a reference to a hash with keys being features present in current implementation.
554 This features are present:
555  
556 =over
557  
558 =item *
559  
560 I<appname> is not present and is the name of the application. B<But not supported yet.>
561  
562 =item *
563  
564 I<addhistory> is present, always C<TRUE>.
565  
566 =item *
567  
568 I<minline> is present, default 1. See C<MinLine> method.
569  
570 =item *
571  
572 I<autohistory> is present. C<FALSE> if minline is C<undef>. See C<MinLine> method.
573  
574 =item *
575  
576 I<gethistory> is present, always C<TRUE>.
577  
578 =item *
579  
580 I<sethistory> is present, always C<TRUE>.
581  
582 =item *
583  
584 I<changehistory> is present, default C<TRUE>. See C<changehistory> method.
585  
586 =item *
587  
588 I<utf8> is present, default C<TRUE>. See C<utf8> method.
589  
590 =back
591  
592 =cut
593 sub Features
594 {
595 0 ▶ my $self = shift;
Subroutine Features: not called CC=1 SCAR=6.9
Pod documented
596 0 my %features = %{$self->{features}};
597 0 return \%features;
598 }
599  
600 =head1 Additional Methods and Functions
601  
602 =cut
603  
604 =head2 newTTY([$IN[, $OUT]])
605  
606 takes two arguments which are input filehandle and output filehandle. Switches to use these filehandles.
607  
608 =cut
609 sub newTTY
610 {
611 0 ▶ my $self = shift;
Subroutine newTTY: not called CC=11 SCAR=48.8
Pod documented
612 0 my ($IN, $OUT) = @_;
613  
614 0 my ($console, $consoleOUT) = findConsole();
615 0 ▶ my $console_utf8 = defined($ENV{LANG}) && $ENV{LANG} =~ /\.UTF\-?8$/i;
Condition0%0/3
defined $ENV{'LANG'} && $ENV{'LANG'} =~ /\.UTF\-?8$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{'LANG'} && $ENV{'LANG'} =~ /\.UTF\-?8$/i
defined $ENV{'LANG'} $ENV{'LANG'} =~ /\.UTF\-?8$/i
Truth table0%0/3
A && B
A= defined $ENV{'LANG'}B= $ENV{'LANG'} =~ /\.UTF\-?8$/i
ABresult
0X0
100
111
616 0 my $console_layers = "";
617 0 ▶ $console_layers .= " :utf8" if $console_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $console_utf8 0 0 0
618  
619 0 my $in;
620 0 ▶ $in = $IN if ref($IN) eq "GLOB";
Branch: 1 branch
BranchTrueFalseTotal
if ref $IN eq "GLOB" 0 0 0
621 0 ▶ $in = \$IN if ref(\$IN) eq "GLOB";
Branch: 1 branch
BranchTrueFalseTotal
if ref \$IN eq "GLOB" 0 0 0
622 0 ▶ open($in, "<$console_layers", $console) unless defined($in);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in 0 0 0
623 0 ▶ $in = \*STDIN unless defined($in);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in 0 0 0
624 0 $self->{IN} = $in;
625  
626 0 my $out;
627 0 ▶ $out = $OUT if ref($OUT) eq "GLOB";
Branch: 1 branch
BranchTrueFalseTotal
if ref $OUT eq "GLOB" 0 0 0
628 0 ▶ $out = \$OUT if ref(\$OUT) eq "GLOB";
Branch: 1 branch
BranchTrueFalseTotal
if ref \$OUT eq "GLOB" 0 0 0
629 0 ▶ open($out, ">$console_layers", $consoleOUT) unless defined($out);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 0 0
630 0 ▶ $out = \*STDOUT unless defined($out);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 0 0
631 0 $self->{OUT} = $out;
632  
633 0 return ($self->{IN}, $self->{OUT});
634 }
635  
636 =head2 ornaments
637  
638 This is void implementation. Ornaments is B<not supported>.
639  
640 =cut
641 sub ornaments
642 {
643 0 ▶ return;
Subroutine ornaments: not called CC=1 SCAR=6.9
Pod documented
644 }
645  
646 =head2 gethistory()
647  
648 B<GetHistory()>
649  
650 Returns copy of the history in Array.
651  
652 =cut
653 sub gethistory
654 {
655 0 ▶ my $self = shift;
Subroutine gethistory: not called CC=4 SCAR=30.0
Pod documented
656 0 my @result = @{$self->{history}};
657 0 ▶ if ($self->{features}->{utf8})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"features"}{"utf8"}) 0 0 0
658 {
659 0 ▶ for (my $i = 0; $i < @result; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @result) 0 0 0
660 {
661 0 ▶ utf8::encode($result[$i]) if utf8::is_utf8($result[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($result[$i]) 0 0 0
662 }
663 }
664 0 return @result;
665 }
666 sub GetHistory
667 {
668 0 ▶ return gethistory(@_);
Subroutine GetHistory: not called CC=1 SCAR=6.9
Pod undocumented
669 }
670  
671 =head2 sethistory($line1[, $line2[, ...]])
672  
673 B<SetHistory($line1[, $line2[, ...]])>
674  
675 rewrites all history by argument values.
676  
677 =cut
678 sub sethistory
679 {
680 0 ▶ my $self = shift;
Subroutine sethistory: not called CC=3 SCAR=24.8
Pod documented
681 0 ▶ if (grep(":utf8", PerlIO::get_layers($self->{IN})))
Branch: 1 branch
BranchTrueFalseTotal
if (grep ":utf8", &PerlIO::get_layers($self->{"IN"})) 0 0 0
682 {
683 0 ▶ for (my $i = 0; $i < @_; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
684 {
685 0 utf8::decode($_[$i]);
686 }
687 }
688 0 @{$self->{history}} = @_;
689 0 return 1;
690 }
691 sub SetHistory
692 {
693 0 ▶ return sethistory(@_);
Subroutine SetHistory: not called CC=1 SCAR=6.9
Pod undocumented
694 }
695  
696 =head2 changehistory([$changehistory])
697  
698 If argument is specified, it allows to change history lines when argument value is true.
699  
700 Returns the old value.
701  
702 =cut
703 sub changehistory
704 {
705 0 ▶ my $self = shift;
Subroutine changehistory: not called CC=2 SCAR=17.9
Pod documented
706 0 my ($changehistory) = @_;
707 0 my $result = $self->{features}->{changehistory};
708 0 ▶ $self->{features}->{changehistory} = $changehistory if @_ >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 1 0 0 0
709 0 return $result;
710 }
711  
712 =head1 Other Methods and Functions
713  
714 =cut
715  
716 =head2 readkey([$echo])
717  
718 reads a key from input and echoes if I<echo> argument is C<TRUE>.
719  
720 Returns C<undef> on C<EOF>.
721  
722 =cut
723 sub readkey
724 {
725 0 ▶ my $self = shift;
Subroutine readkey: not called CC=9 SCAR=45.0
Pod documented
726 0 my ($echo) = @_;
727 my ($in, $out) =
728 0 ($self->{IN}, $self->{OUT});
729 0 ▶ unless (-t $in)
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $in) 0 0 0
730 {
731 0 return getc($in);
732 }
733 0 local $\ = undef;
734  
735 0 $self->{readmode} = 'cbreak';
736 0 Term::ReadKey::ReadMode($self->{readmode}, $self->{IN});
737  
738 0 my $result;
739 0 my ($char, $esc) = ("", undef);
740 0 ▶ while (defined($char = getc($in)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($char = getc $in)) 0 0 0
741 {
742 0 ▶ unless (defined($esc))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $esc) 0 0 0
743 {
744 0 given ($char)
745 {
746 when (/\e/)
747 0 {
748 0 $esc = "";
749 }
750 when (/\x04/)
751 0 {
752 0 $result = undef;
753 0 last;
754 }
755 default
756 0 {
757 0 ▶ print $out encode_controlchar($char) if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 0 0
758 0 $result = $char;
759 0 last;
760 }
761 }
762 0 next;
763 }
764 0 $esc .= $char;
765 0 ▶ if ($esc =~ /^.\d?\D/)
Branch: 1 branch
BranchTrueFalseTotal
if ($esc =~ /^.\d?\D/) 0 0 0
766 {
767 0 $result = "\e$esc";
768 0 $esc = undef;
769 0 last;
770 }
771 }
772 0 ▶ utf8::encode($result) if defined($result) and utf8::is_utf8($result) and $self->{features}->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"} 0 0 0
Condition0%0/6
defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"}
!ll&&!rl&&r
000
defined $result and utf8::is_utf8($result)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $result and utf8::is_utf8($result) and $self->{"features"}{"utf8"}
defined $result utf8::is_utf8($result) $self->{"features"}{"utf8"}
Truth table0%0/4
A and B and C
A= defined $resultB= utf8::is_utf8($result)C= $self->{"features"}{"utf8"}
ABCresult
0XX0
10X0
1100
1111
773  
774 0 Term::ReadKey::ReadMode('restore', $self->{IN});
775 0 $self->{readmode} = '';
776 0 return $result;
777 }
778  
779 =head2 utf8([$enable])
780  
781 If C<$enable> is C<TRUE>, all read methods return that binary encoded UTF-8 string as possible.
782  
783 Returns the old value.
784  
785 =cut
786 sub utf8
787 {
788 0 ▶ my $self = shift;
Subroutine utf8: not called CC=2 SCAR=17.9
Pod documented
789 0 my ($enable) = @_;
790 0 my $result = $self->{features}->{utf8};
791 0 ▶ $self->{features}->{utf8} = $enable if @_ >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 1 0 0 0
792 0 return $result;
793 }
794  
795 =head2 encode_controlchar($c)
796  
797 encodes if argument C<$c> is a control character, otherwise returns argument C<c>.
798  
799 =cut
800 sub encode_controlchar
801 {
802 0 ▶ my ($c) = @_;
Subroutine encode_controlchar: not called CC=1 SCAR=6.9
Pod documented
803 0 $c = substr($c, 0, 1);
804 0 my $s;
805 0 given ($c)
806 {
807 when (/[\x00-\x1F]/)
808 0 {
809 0 $s = "^".chr(0x40+ord($c));
810 }
811 when ($c =~ /[\x7F]/)
812 0 {
813 0 $s = "^".chr(0x3F);
814 }
815 default
816 0 {
817 0 $s = $c;
818 }
819 }
820 0 return $s;
821 }
822  
823  
824 1 1;
825 __END__
↑ Summary Tiny/readline.pm »