blib/lib/Term/Complete.pm

filter: Statementstmt 82.9% 68 / 82 Branchbran 58.8% 20 / 34 Conditioncond 38.8% 7 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 2 / 2 Podpod 0.0% 0 / 1 totaltotal 66.6% 98 / 147 CC 24 SCAR 38.4
CC
24
Cov
71%
Complete
48.4
BEGIN
1.0
CRAP 46.5
↑ Summary
1 package Term::Complete;
2 1 require 5.000;
3 1 require Exporter;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our @ISA = qw(Exporter);
7 1 our @EXPORT = qw(Complete);
8 1 our $VERSION = '1.402';
9  
10 # @(#)complete.pl,v1.2 (me@anywhere.EBay.Sun.COM) 09/23/91
11  
12 =head1 NAME
13  
14 Term::Complete - Perl word completion module
15  
16 =head1 SYNOPSIS
17  
18 $input = Complete('prompt_string', \@completion_list);
19 $input = Complete('prompt_string', @completion_list);
20  
21 =head1 DESCRIPTION
22  
23 This routine provides word completion on the list of words in
24 the array (or array ref).
25  
26 The tty driver is put into raw mode and restored using an operating
27 system specific command, in UNIX-like environments C<stty>.
28  
29 The following command characters are defined:
30  
31 =over 4
32  
33 =item E<lt>tabE<gt>
34  
35 Attempts word completion.
36 Cannot be changed.
37  
38 =item ^D
39  
40 Prints completion list.
41 Defined by I<$Term::Complete::complete>.
42  
43 =item ^U
44  
45 Erases the current input.
46 Defined by I<$Term::Complete::kill>.
47  
48 =item E<lt>delE<gt>, E<lt>bsE<gt>
49  
50 Erases one character.
51 Defined by I<$Term::Complete::erase1> and I<$Term::Complete::erase2>.
52  
53 =back
54  
55 =head1 DIAGNOSTICS
56  
57 Bell sounds when word completion fails.
58  
59 =head1 BUGS
60  
61 The completion character E<lt>tabE<gt> cannot be changed.
62  
63 =head1 AUTHOR
64  
65 Wayne Thompson
66  
67 =cut
68  
69 1 our($complete, $kill, $erase1, $erase2, $tty_raw_noecho, $tty_restore, $stty, $tty_safe_restore);
70 1 our($tty_saved_state) = '';
71 CONFIG: {
72 1 $complete = "\004";
73 1 $kill = "\025";
74 1 $erase1 = "\177";
75 1 $erase2 = "\010";
76 1 foreach my $s (qw(/bin/stty /usr/bin/stty)) {
77 1 ▶ if (-x $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $s) 1 0 1
78 1 $tty_raw_noecho = "$s raw -echo";
79 1 $tty_restore = "$s -raw echo";
80 1 $tty_safe_restore = $tty_restore;
81 1 $stty = $s;
82 1 last;
83 }
84 }
85 }
86  
87 4 ▶sub Complete {
Subroutine Complete: called CC=24 SCAR=38.8
Pod undocumented
88 4 my($prompt, @cmp_lst, $cmp, $test, $l, @match);
89 4 my ($return, $r) = ("", 0);
90  
91 4 $return = "";
92 4 $r = 0;
93  
94 4 $prompt = shift;
95 4 ▶ if (ref $_[0] || $_[0] =~ /^\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] or $_[0] =~ /^\*/) { } 1 3 4
Condition66%2/3
ref $_[0] or $_[0] =~ /^\*/
l!l&&r!l&&!r
103
MC/DC50%1/2
ref $_[0] or $_[0] =~ /^\*/
ref $_[0] $_[0] =~ /^\*/
Truth table66%2/3
A or B
A= ref $_[0]B= $_[0] =~ /^\*/
ABresult
1X1
011
000
96 1 @cmp_lst = sort @{$_[0]};
97 }
98 else {
99 3 @cmp_lst = sort(@_);
100 }
101  
102 # Attempt to save the current stty state, to be restored later
103 4 ▶ if (defined $stty && defined $tty_saved_state && $tty_saved_state eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Term::Complete::stty and defined $Term::Complete::tty_saved_state and $Term::Complete::tty_saved_state eq "") 0 4 4
Condition33%2/6
defined $Term::Complete::stty and defined $Term::Complete::tty_saved_state and $Term::Complete::tty_saved_state eq ""
!ll&&!rl&&r
400
defined $Term::Complete::stty and defined $Term::Complete::tty_saved_state
!ll&&!rl&&r
400
MC/DC0%0/3
defined $Term::Complete::stty and defined $Term::Complete::tty_saved_state and $Term::Complete::tty_saved_state eq ""
defined $Term::Complete::stty defined $Term::Complete::tty_saved_state $Term::Complete::tty_saved_state eq ""
Truth table25%1/4
A and B and C
A= defined $Term::Complete::sttyB= defined $Term::Complete::tty_saved_stateC= $Term::Complete::tty_saved_state eq ""
ABCresult
0XX0
10X0
1100
1111
104 0 $tty_saved_state = qx($stty -g 2>/dev/null);
105 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) { } 0 0 0
106 # stty -g not supported
107 0 $tty_saved_state = undef;
108 }
109 else {
110 0 $tty_saved_state =~ s/\s+$//g;
111 0 $tty_restore = qq($stty "$tty_saved_state" 2>/dev/null);
112 }
113 }
114 4 ▶ system $tty_raw_noecho if defined $tty_raw_noecho;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Term::Complete::tty_raw_noecho 0 4 4
115 LOOP: {
116 4 local $_;
117 12 print($prompt, $return);
118 12 ▶ while (($_ = getc(STDIN)) ne "\r") {
Branch: 1 branch
BranchTrueFalseTotal
if (($_ = getc STDIN) ne "\r") 26 4 30
119 CASE: {
120 # (TAB) attempt completion
121 26 ▶ $_ eq "\t" && do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\t" and do { @match = grep(/^\Q$return\E/, @cmp_lst); unless ($#match < 0) { $l = length($test = shift @match); foreach $cmp (@match) { until (substr($cmp, 0, $l) eq substr($test, 0, $l)) { --$l; } ; } ; print "\a"; print $test = substr($test, $r, $l - $r); $r = length(($return .= $test)); } ; last CASE } 1 25 26
122 1 @match = grep(/^\Q$return/, @cmp_lst);
123 1 ▶ unless ($#match < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#match < 0) 1 0 1
124 1 $l = length($test = shift(@match));
125 1 foreach $cmp (@match) {
126 2 ▶ until (substr($cmp, 0, $l) eq substr($test, 0, $l)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($cmp, 0, $l) eq substr($test, 0, $l)) 5 2 7
127 5 $l--;
128 }
129 }
130 1 print("\a");
131 1 print($test = substr($test, $r, $l - $r));
132 1 $r = length($return .= $test);
133 }
134 1 last CASE;
135 };
136  
137 # (^D) completion list
138 25 ▶ $_ eq $complete && do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $Term::Complete::complete and do { print join("\r\n", "", grep(/^\Q$return\E/, @cmp_lst)), "\r\n"; redo LOOP } 4 21 25
139 4 print(join("\r\n", '', grep(/^\Q$return/, @cmp_lst)), "\r\n");
140 4 redo LOOP;
141 };
142  
143 # (^U) kill
144 21 ▶ $_ eq $kill && do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $Term::Complete::kill and do { if ($r) { $r = 0; $return = ""; print "\r\n"; redo LOOP; } ; last CASE } 4 17 21
145 4 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 4 0 4
146 4 $r = 0;
147 4 $return = "";
148 4 print("\r\n");
149 4 redo LOOP;
150 }
151 0 last CASE;
152 };
153  
154 # (DEL) || (BS) erase
155 17 ▶ ($_ eq $erase1 || $_ eq $erase2) && do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $Term::Complete::erase1 || $_ eq $Term::Complete::erase2 and do { if ($r) { print "\b \b"; chop $return; --$r; } ; last CASE } 0 17 17
Condition33%1/3
$_ eq $Term::Complete::erase1 or $_ eq $Term::Complete::erase2
l!l&&r!l&&!r
0017
MC/DC0%0/2
$_ eq $Term::Complete::erase1 or $_ eq $Term::Complete::erase2
$_ eq $Term::Complete::erase1 $_ eq $Term::Complete::erase2
Truth table33%1/3
A or B
A= $_ eq $Term::Complete::erase1B= $_ eq $Term::Complete::erase2
ABresult
1X1
011
000
156 0 ▶ if($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
157 0 print("\b \b");
158 0 chop($return);
159 0 $r--;
160 }
161 0 last CASE;
162 };
163  
164 # printable char
165 17 ▶ ord >= 32 && do {
Branch: 1 branch
BranchTrueFalseTotal
ord $_ >= 32 and do { $return .= $_; ++$r; print $_; last CASE } 17 0 17
166 17 $return .= $_;
167 17 $r++;
168 17 print;
169 17 last CASE;
170 };
171 }
172 }
173 }
174  
175 # system $tty_restore if defined $tty_restore;
176 4 ▶ if (defined $tty_saved_state && defined $tty_restore && defined $tty_safe_restore)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Term::Complete::tty_saved_state and defined $Term::Complete::tty_restore and defined $Term::Complete::tty_safe_restore) 0 4 4
Condition33%2/6
defined $Term::Complete::tty_saved_state and defined $Term::Complete::tty_restore and defined $Term::Complete::tty_safe_restore
!ll&&!rl&&r
400
defined $Term::Complete::tty_saved_state and defined $Term::Complete::tty_restore
!ll&&!rl&&r
040
MC/DC0%0/3
defined $Term::Complete::tty_saved_state and defined $Term::Complete::tty_restore and defined $Term::Complete::tty_safe_restore
defined $Term::Complete::tty_saved_state defined $Term::Complete::tty_restore defined $Term::Complete::tty_safe_restore
Truth table25%1/4
A and B and C
A= defined $Term::Complete::tty_saved_stateB= defined $Term::Complete::tty_restoreC= defined $Term::Complete::tty_safe_restore
ABCresult
0XX0
10X0
1100
1111
177 {
178 0 system $tty_restore;
179 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
180 # tty_restore caused error
181 0 system $tty_safe_restore;
182 }
183 }
184 4 print("\n");
185 4 $return;
186 }
187  
188 1 1;
↑ Summary