blib/lib/Term/Screen/ReadLine.pm

filter: Statementstmt 9.3% 14 / 150 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 41.6% 5 / 12 Podpod 66.6% 4 / 6 totaltotal 7.5% 23 / 303 CC 60 SCAR 69.3
CC
60
Cov
5%
readline
1722.0
_print_line
110.0
getch
72.0
CRAP 1019.3
↑ Summary
1 package Term::Screen::ReadLine;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(Term::Screen);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Term::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION=0.38;
10 }
11  
12 0 ▶sub readline {
Subroutine readline: not called CC=41 SCAR=74.5
Pod documented
13 0 my $self = shift;
14 0 my $args = {
15 ROW => 0,
16 COL => 0,
17 LEN => 40,
18 DISPLAYLEN => undef,
19 LINE => "",
20 EXITS => {},
21 ONLYVALID => undef,
22 CONVERT => undef,
23 PASSWORD => undef,
24 NOCOMMIT => 0,
25 READONLY => 0,
26 OVERWRITE => 0,
27 @_
28 };
29 0 my $row = $args->{ROW};
30 0 my $column = $args->{COL};
31 0 my $lineLen = $args->{LEN};
32 0 my $displayLen = $args->{DISPLAYLEN};
33 0 my $line = $args->{LINE};
34 0 my %exits = %{ $args->{EXITS} };
35 0 my $onlyvalid = $args->{ONLYVALID};
36 0 my $convert = $args->{CONVERT};
37 0 my $cursor = length $line;
38 0 my $nocommit = $args->{NOCOMMIT};
39 0 my $readonly = $args->{READONLY};
40 0 my $overwrite = $args->{OVERWRITE};
41
42 0 ▶ if (length $line == 1) { $cursor=0; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $line == 1) 0 0 0
43  
44 0 $self->{PASSWORD}=$args->{PASSWORD};
45  
46 0 ▶ if (defined $convert) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $convert) { } 0 0 0
47 0 $convert=~tr/a-z/A-Z/;
48 0 ▶ if ("UPPERCASE"=~/^$convert/) {
Branch: 2 branches
BranchTrueFalseTotal
if ('UPPERCASE' =~ /^$convert/) { } 0 0 0
elsif ('LOWERCASE' =~ /^$convert/) { } 0 0 0
49 0 $convert="up";
50 }
51 elsif ("LOWERCASE"=~/^$convert/) {
52 0 $convert="lo";
53 }
54 else {
55 0 $convert="";
56 }
57 }
58 else {
59 0 $convert="";
60 }
61  
62 0 ▶ if ($convert eq "up") { $line=~tr/a-z/A-Z/; }
Branch: 2 branches
BranchTrueFalseTotal
if ($convert eq 'up') { } 0 0 0
elsif ($convert eq 'lo') { } 0 0 0
63 0 elsif ($convert eq "lo") { $line=~tr/A-Z/a-z/; }
64  
65 0 my $c;
66 my $ordc;
67  
68 0 ▶ if (not defined $displayLen) { $displayLen = $args->{LEN}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $displayLen) 0 0 0
69  
70 0 $self->_print_line($line,$displayLen,$row,$column,$cursor,2,$overwrite);
71  
72 0 ▶ $exits{"013"}="enter" if not exists $exits{"013"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"013"} 0 0 0
73 0 ▶ $exits{"ku"}="ku" if not exists $exits{"ku"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"ku"} 0 0 0
74 0 ▶ $exits{"kd"}="kd" if not exists $exits{"kd"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"kd"} 0 0 0
75 0 ▶ $exits{"027"}="esc" if not exists $exits{"027"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"027"} 0 0 0
76 0 ▶ $exits{"009"}="tab" if not exists $exits{"009"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"009"} 0 0 0
77 0 ▶ $exits{"010"}="ctrl-enter" if not exists $exits{"010"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exits{"010"} 0 0 0
78  
79 0 $self->noecho();
80  
81 0 $c=$self->getch();
82 0 $ordc=sprintf("%03d",ord($c));
83  
84 0 ▶ while ( (not exists $exits{$c}) and (not exists $exits{$ordc}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $exits{$c} or exists $exits{$ordc}) 0 0 0
Condition0%0/3
exists $exits{$c} or exists $exits{$ordc}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $exits{$c} or exists $exits{$ordc}
exists $exits{$c} exists $exits{$ordc}
Truth table0%0/3
A or B
A= exists $exits{$c}B= exists $exits{$ordc}
ABresult
1X1
011
000
85  
86  
87 0 ▶ if ($readonly) {
Branch: 2 branches
BranchTrueFalseTotal
if ($readonly) { } 0 0 0
elsif ($lineLen == 1) { } 0 0 0
88 ## next if readonly
89 }
90 elsif ($lineLen == 1) {
91 ## If the requested line length is 1 don't try any line processing
92  
93 0 ▶ if (($c ge " ") and ($c le "~") and (length $c == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ge " " and $c le "~" and length $c == 1) 0 0 0
Condition0%0/6
$c ge " " and $c le "~" and length $c == 1
!ll&&!rl&&r
000
$c ge " " and $c le "~"
!ll&&!rl&&r
000
MC/DC0%0/3
$c ge " " and $c le "~" and length $c == 1
$c ge " " $c le "~" length $c == 1
Truth table0%0/4
A and B and C
A= $c ge " "B= $c le "~"C= length $c == 1
ABCresult
0XX0
10X0
1100
1111
94 0 my $input=0;
95  
96 0 ▶ if ($convert eq "up") { $c=~tr/a-z/A-Z/; }
Branch: 2 branches
BranchTrueFalseTotal
if ($convert eq 'up') { } 0 0 0
elsif ($convert eq 'lo') { } 0 0 0
97 0 elsif ($convert eq "lo") { $c=~tr/A-Z/a-z/; }
98  
99 0 ▶ if (defined $onlyvalid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $onlyvalid) { } 0 0 0
100 0 ▶ if ($c=~/^$onlyvalid$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^$onlyvalid$/) 0 0 0
101 0 $input=1;
102 }
103 }
104 else {
105 0 $input=1;
106 }
107  
108 0 ▶ if ($input==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input == 1) 0 0 0
109 0 $line=$c;
110 0 $self->_print_line($line,$displayLen,$row,$column,$cursor,2);
111 0 ▶ if ($nocommit) { $c=chr(13);$ordc="013";last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($nocommit) 0 0 0
112 }
113  
114 }
115  
116 }
117 else {
118  
119 ## Else do difficult processing
120 #print "ordc=$ordc\n";
121  
122 0 ▶ if ($ordc==8 || $ordc==127) {my $L;
Branch: 2 branches
BranchTrueFalseTotal
if ($ordc == 8 or $ordc == 127) { } 0 0 0
elsif ($c ge ' ' and $c le '~' and length $c == 1) { } 0 0 0
Condition0%0/3
$ordc == 8 or $ordc == 127
l!l&&r!l&&!r
000
MC/DC0%0/2
$ordc == 8 or $ordc == 127
$ordc == 8 $ordc == 127
Truth table0%0/3
A or B
A= $ordc == 8B= $ordc == 127
ABresult
1X1
011
000
123 0 $self->_print_line($line,$displayLen,$row,$column,$cursor,1);
124 0 $L=length $line;
125 0 $L--;
126 0 $line=substr($line,0,$L);
127 }
128 ▶ elsif (($c ge ' ') and ($c le '~') and (length $c == 1)) {
Condition0%0/6
$c ge " " and $c le "~" and length $c == 1
!ll&&!rl&&r
000
$c ge " " and $c le "~"
!ll&&!rl&&r
000
MC/DC0%0/3
$c ge " " and $c le "~" and length $c == 1
$c ge " " $c le "~" length $c == 1
Truth table0%0/4
A and B and C
A= $c ge " "B= $c le "~"C= length $c == 1
ABCresult
0XX0
10X0
1100
1111
129  
130 0 ▶ if ($convert eq "up") { $c=~tr/a-z/A-Z/; }
Branch: 2 branches
BranchTrueFalseTotal
if ($convert eq 'up') { } 0 0 0
elsif ($convert eq 'lo') { } 0 0 0
131 0 elsif ($convert eq "lo") { $c=~tr/A-Z/a-z/; }
132  
133 0 my $L=length $line;
134 0 ▶ if ($L < $lineLen) {my $extend=0;
Branch: 1 branch
BranchTrueFalseTotal
if ($L < $lineLen) 0 0 0
135 0 ▶ if (defined $onlyvalid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $onlyvalid) 0 0 0
136 0 my $s=$line.$c;
137 0 ▶ if ($s=~/^$onlyvalid$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^$onlyvalid$/) 0 0 0
138 0 $extend=1;
139 }
140 }
141 0 ▶ if (not defined $onlyvalid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $onlyvalid) 0 0 0
142 0 $extend=1;
143 }
144 0 ▶ if ($extend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extend) 0 0 0
145 0 $line=$line.$c;
146 0 $self->_print_line($line,$displayLen,$row,$column,$cursor,0);
147 }
148 }
149 }
150 }
151 0 $c=$self->getch();
152 0 $ordc=sprintf("%03d",ord($c));
153 }
154  
155 #$self->at(20,0)->puts("$ordc")->getch();
156  
157 0 $self->{LASTKEY}=$c;
158 0 ▶ $self->{LASTKEY}=$exits{$c} if exists $exits{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $exits{$c} 0 0 0
159 0 ▶ $self->{LASTKEY}=$exits{$ordc} if exists $exits{$ordc};
Branch: 1 branch
BranchTrueFalseTotal
if exists $exits{$ordc} 0 0 0
160  
161 0 return $line;
162 }
163  
164 0 ▶sub two_esc {
Subroutine two_esc: not called CC=1 SCAR=6.9
Pod documented
165 0 my $self=shift;
166 0 $self->{READLINEGETCH}=0;
167 }
168  
169 0 ▶sub one_esc {
Subroutine one_esc: not called CC=1 SCAR=6.9
Pod documented
170 0 my $self=shift;
171 0 $self->{READLINEGETCH}=1;
172 }
173  
174 # Redefinition of getch() to have the Escape key fixed.
175 # This one only works from STDIN!
176  
177 0 ▶sub getch {
Subroutine getch: not called CC=8 SCAR=42.8
Pod documented
178 0 my $self = shift;
179 0 my $c;
180 my $L;
181  
182 0 ▶ if (not defined $self->{READLINEGETCH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"READLINEGETCH"}) 0 0 0
183 0 $self->one_esc;
184 }
185 0 ▶ if (not $self->{READLINEGETCH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"READLINEGETCH"}) 0 0 0
186 0 return $self->Term::Screen::getch();
187 }
188  
189 0 sysread STDIN,$c,1;
190 0 ▶ if (ord($c)==0) { exit 100; }
Branch: 1 branch
BranchTrueFalseTotal
if (ord $c == 0) 0 0 0
191  
192 0 ▶ if ($c eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\e") { } 0 0 0
193 0 ▶ while ($self->key_pressed(0)) {my $cc;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->key_pressed(0)) 0 0 0
194 0 sysread STDIN,$cc,1;
195 0 $c.=$cc;
196 0 ▶ if ($self->{KEYS}{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"KEYS"}{$c}) 0 0 0
197 0 $c=$self->{KEYS}{$c};
198 }
199 }
200 }
201 else {
202 0 ▶ if (exists $self->{KEYS}{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"KEYS"}{$c}) 0 0 0
203 0 $c=$self->{KEYS}{$c};
204 }
205 }
206 0 return $c;
207 }
208  
209 0 ▶sub lastkey {
Subroutine lastkey: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my $self=shift;
211 0 return $self->{LASTKEY};
212 }
213  
214 #####################################################################
215 # Internal routines
216 #####################################################################
217  
218 #
219 # $mode = 0 --> normal mode
220 # $mode = 1 --> delete mode
221 # $mode = 2 --> initial mode
222 #
223  
224 0 ▶sub _print_line {
Subroutine _print_line: not called CC=10 SCAR=47.0
225 0 my ($self, $line, $displaylen, $row, $column, $cursor, $mode, $overwrite) = @_;
226 0 my $L;
227  
228 0 $L=length $line;
229  
230 0 ▶ if ($self->{PASSWORD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PASSWORD"}) 0 0 0
231 0 $line=$self->setstr("*",$L);
232 }
233 #my $a=$L-$displaylen;
234 #my $ll=length $line;
235 #$self->at(22,0)->puts("$line - $ll - $a - $L - $displaylen");
236  
237  
238 0 ▶ if ($L>$displaylen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($L > $displaylen) { } 0 0 0
239 0 ▶ if ($mode == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 1) { } 0 0 0
240 0 $self->at($row,$column)->puts(substr($line,$L-$displaylen-1,$displaylen));
241 }
242 else {
243 0 $self->at($row,$column)->puts(substr($line,$L-$displaylen,$displaylen));
244 }
245 }
246 else {
247 0 ▶ if ($mode == 1 and $L > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode == 1 and $L > 0) { } 0 0 0
elsif ($mode == 2) { } 0 0 0
elsif ($L <= $displaylen) { } 0 0 0
Condition0%0/3
$mode == 1 and $L > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$mode == 1 and $L > 0
$mode == 1 $L > 0
Truth table0%0/3
A and B
A= $mode == 1B= $L > 0
ABresult
0X0
100
111
248 0 print chr(8).chr(32).chr(8);
249 }
250 elsif ($mode == 2 ) {
251 0 my $str='';
252 0 my $i;
253 0 ▶ unless($overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($overwrite) 0 0 0
254 0 for(1..$displaylen-$L) {
255 0 $str.=" ";
256 }
257 }
258 0 $self->at($row,$column)->puts($line)->puts($str);
259 0 $self->at($row,$column+$L);
260 }
261 elsif ($L <= $displaylen) {
262 0 print substr($line,$L-1,1);
263 }
264 }
265 }
266  
267 0 ▶sub setstr {
Subroutine setstr: not called CC=4 SCAR=30.0
Pod undocumented
268 0 my ($self,$char,$len)=@_;
269  
270 0 ▶ if ($len <= 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($len <= 0) { } 0 0 0
elsif ($len == 1) { } 0 0 0
elsif ($len % 2 == 1) { } 0 0 0
271 0 return "";
272 }
273 elsif ($len==1) {
274 0 return $char;
275 }
276 elsif ($len%2==1) {
277 0 return $char.$self->setstr($char,$len-1);
278 }
279 else {
280 0 return $self->setstr($char.$char,$len>>1);
281 }
282 }
283  
284  
285 =pod
286  
287 =head1 NAME
288  
289 Term::Screen::ReadLine - Term::Screen extended with ReadLine
290  
291 =head1 SYNOPSIS
292  
293 use lib "./blib/lib";
294  
295 use Term::Screen::ReadLine;
296  
297 $scr = new Term::Screen::ReadLine;
298  
299 $scr->clrscr();
300 $a=$scr->getch();
301 print $a," ",length $a," ",ord($a),"\n";
302 $scr->two_esc;
303 $a=$scr->getch();
304 print $a," ",length $a," ",ord($a),"\n";
305 $scr->one_esc;
306  
307  
308 $scr->clrscr();
309 $scr->at(4,4)->puts("input? ");
310 $line=$scr->readline(ROW => 4, COL => 12);
311 $line=$scr->readline(ROW => 5, COL => 12, DISPLAYLEN => 20);
312 $scr->at(10,4)->puts($line);
313 $scr->two_esc;
314 $line=$scr->readline(ROW => 6, COL => 12, DISPLAYLEN => 20, ONLYVALID => "[ieIE]+", CONVERT => "up");
315  
316 print "\n";
317 print $scr->lastkey(),"\n";
318  
319 $r=$scr->getch();
320 print $r,ord($r),"\n";
321 $r=ord($r);
322 print $r,"\n";
323 if ($r eq 13) {
324 print "aja!\n";
325 }
326  
327  
328 exit;
329  
330  
331 =head1 DESCRIPTION
332  
333 This module extends Term::Screen with a readline() function.
334 It also makes it possible to use a *single* Esc to escape instead
335 of the Term::Screen double Esc.
336  
337 =head1 USAGE
338  
339 =head2 readline()
340  
341 readline(
342 ROW => 0,
343 COL => 0,
344 LEN => 40,
345 DISPLAYLEN => undef,
346 LINE => "",
347 ONLYVALID => undef,
348 CONVERT => undef,
349 PASSWORD => undef,
350 OVERWRITE => undef,
351 )
352  
353 I<B<Parameters>>
354  
355 =over 1
356  
357 =item ROW,COL
358  
359 'at(ROW,COL) readline()...'.
360  
361 =item LEN
362  
363 The maximum length of the line to read.
364  
365 =item DISPLAYLEN
366  
367 The maximum length of the displayed field. The display will scroll if C<DISPLAYLEN> is exceeded.
368  
369 =item EXITS
370  
371 Explained below.
372  
373 =item LINE
374  
375 A default value for readline to use.
376  
377 =item ONLYVALID
378  
379 A regex to validate the input.
380  
381 =item CONVERT
382  
383 B<"up"> or B<"lo"> for uppercase or lowercase. Empty (B<"">) if not used.
384 I<Note: conversion will take place B<after> validation>.
385  
386 =item PASSWORD
387  
388 Display stars ('*') instead of what is being typed in.
389  
390 =item OVERWRITE
391  
392 By default readline will clear characters from the current position to the end
393 of the given field length. By setting OVERWRITE to a non-zero value will any
394 characters visible in the field to be overwritten rather than removed.
395  
396 =back
397  
398 I<B<Return value>>
399  
400 =over 1
401  
402 Returns The inputted line.
403  
404 =back
405  
406 I<B<Notes>>
407  
408 =over 1
409  
410 =item *
411  
412 The readline() function does always return on the following keys:
413 C<Enter>, C<Arrow Up>, C<Arrow Down>, C<Esc>, C<Tab> and C<Ctrl-Enter/F4>.
414  
415 This can be extended using the EXITS argument, which must be a hash of
416 keys (see Term::Screen) and a description that will be returned for that key.
417  
418 example: C<EXITS =E<gt> { "k1" =E<gt> "help", "k3" =E<gt> "cancel" }>.
419  
420 This will bind 'F1' to a 'help' message and 'F3' to a 'cancel' message.
421  
422 =item *
423  
424 The readline() function will issue an C<exit(100)>, if a '\0' character is
425 read. This is what usually happens when reading from STDIN does not
426 give 'eof()' condition as would be nice, if a telnet session is suddenly
427 killed. Not exiting on a '\0' character will result in a racing perl
428 script.
429  
430 =back
431  
432 =head2 last_key()
433  
434 returns the last key pressed, that made the readline function return.
435  
436 =head2 one_esc()
437  
438 Makes it possible to press only one time Esc to make readline return.
439 This is the default for Term::Screen::ReadLine.
440  
441 =head2 two_esc()
442  
443 Revert back to the standard Term::Screen behaviour for the Esc key.
444  
445 =head1 AUTHOR
446  
447 Hans Dijkema <oesterhol@cpan.org>
448  
449 =head1 LICENSE
450  
451 Artistic
452  
453 =cut
454  
455 1 1;
456  
↑ Summary