Usul/TraitFor/Prompting.pm

filter: Statementstmt 67.2% 115 / 171 Branchbran 36.5% 30 / 82 Conditioncond 29.1% 14 / 48 MC/DCmcdc 7.4% 2 / 27 Subroutinesub 77.2% 17 / 22 Podpod 100% 5 / 5 totaltotal 51.5% 183 / 355 CC 62 SCAR 44.0
CC
62
Cov
53%
__ANON__
294.8
yorn
29.3
get_line
23.1
CRAP 81.1
« Usul/IPC.pm ↑ Summary Usul/Schema.pm »
1 package Class::Usul::TraitFor::Prompting;
2  
3 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 ▶use Class::Usul::Constants qw( BRK FAILED FALSE NO NUL QUIT SPC TRUE YES );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Class::Usul::Functions qw( arg_list emit_to is_hashref pad throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use IO::Interactive;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 requires qw( add_leader config loc output );
13  
14 # Private functions
15 4 ▶my $_default_input = sub {
Subroutine __ANON__: called CC=4 SCAR=20.9
16 4 my ($fh, $args) = @_;
17  
18 ($ENV{PERL_MM_USE_DEFAULT} or $ENV{PERL_MB_USE_DEFAULT})
19 4 ▶ and return $args->{default};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PERL_MM_USE_DEFAULT"} || $ENV{"PERL_MB_USE_DEFAULT"} and return $args->{"default"} 4 0 4
Condition33%1/3
$ENV{"PERL_MM_USE_DEFAULT"} or $ENV{"PERL_MB_USE_DEFAULT"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$ENV{"PERL_MM_USE_DEFAULT"} or $ENV{"PERL_MB_USE_DEFAULT"}
$ENV{"PERL_MM_USE_DEFAULT"} $ENV{"PERL_MB_USE_DEFAULT"}
Truth table33%1/3
A or B
A= $ENV{"PERL_MM_USE_DEFAULT"}B= $ENV{"PERL_MB_USE_DEFAULT"}
ABresult
1X1
011
000
20 0 ▶ $args->{onechar} and return getc $fh;
Branch: 1 branch
BranchTrueFalseTotal
$args->{"onechar"} and return getc $fh 0 0 0
21 0 return scalar <$fh>;
22 17 };
23  
24 0 ▶my $_get_control_chars = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 # Returns a string of pipe separated control
26 # characters and a hash of symbolic names and values
27 0 my $handle = shift; my %cntl = GetControlChars $handle;
28  
29 0 return ((join '|', values %cntl), %cntl);
30 17 };
31  
32 0 ▶my $_justify_count = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 return pad $_[ 1 ], int log $_[ 0 ] / log 10, SPC, 'left';
34 17 };
35  
36 4 ▶my $_map_prompt_args = sub { # IO::Prompt equiv. sub has an obscure bug so this
Subroutine __ANON__: called CC=2 SCAR=6.9
37 4 my $args = shift; my %map = ( qw(-1 onechar -d default -e echo -p prompt) );
38  
39 4 for (grep { exists $map{ $_ } } keys %{ $args }) {
40 10 $args->{ $map{ $_ } } = delete $args->{ $_ };
41 }
42  
43 4 return $args;
44 17 };
45  
46 4 ▶my $_opts = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
47 4 ▶ my ($type, @args) = @_; is_hashref $args[ 0 ] and return $args[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
&is_hashref($args[0]) and return $args[0] 1 3 4
48  
49 3 my $attr = { default => $args[ 0 ], quit => $args[ 1 ], width => $args[ 2 ]};
50  
51 3 ▶ if ($type eq 'get_line') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'get_line') { } 1 2 3
elsif ($type eq 'get_option') { } 1 1 2
elsif ($type eq 'yorn') { } 1 0 1
52 1 $attr->{multiline} = $args[ 3 ]; $attr->{noecho} = $args[ 4 ];
53 }
54 1 elsif ($type eq 'get_option') { $attr->{options} = $args[ 3 ] }
55 1 elsif ($type eq 'yorn') { $attr->{newline} = $args[ 3 ] }
56  
57 3 return $attr;
58 17 };
59  
60 0 ▶my $_raw_mode = sub { # Puts the terminal in raw input mode
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my $handle = shift; ReadMode 'raw', $handle; return;
62 17 };
63  
64 0 ▶my $_restore_mode = sub { # Restores line input mode to the terminal
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my $handle = shift; ReadMode 'restore', $handle; return;
66 17 };
67  
68 4 ▶my $_prompt = sub {
Subroutine __ANON__: called CC=20 SCAR=56.9
69 # This was taken from L<IO::Prompt> which has an obscure bug in it
70 4 my $args = $_map_prompt_args->( arg_list @_ );
71 4 my $default = $args->{default};
72 4 my $echo = $args->{echo };
73 4 my $onechar = $args->{onechar};
74 4 my $OUT = \*STDOUT;
75 4 my $IN = \*STDIN;
76 4 my $input = NUL;
77  
78 4 my ($len, $newlines, $next, $text);
79  
80 4 ▶ IO::Interactive::is_interactive() or return $_default_input->( $IN, $args );
Branch: 1 branch
BranchTrueFalseTotal
IO::Interactive::is_interactive() or return &$_default_input($IN, $args) 0 4 4
81  
82 0 my ($cntl, %cntl) = $_get_control_chars->( $IN );
83 0 ▶ local $SIG{INT} = sub { $_restore_mode->( $IN ); exit FAILED };
Subroutine __ANON__: not called CC=1 SCAR=6.9
84  
85 0 emit_to $OUT, $args->{prompt}; $_raw_mode->( $IN );
86  
87 0 while (TRUE) {
88 0 ▶ if (defined ($next = getc $IN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($next = getc $IN)) 0 0 0
89 0 ▶ if ($next eq $cntl{INTERRUPT}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($next eq $cntl{'INTERRUPT'}) { } 0 0 0
elsif ($next eq $cntl{'ERASE'}) { } 0 0 0
elsif ($next eq $cntl{'EOF'}) { } 0 0 0
elsif (not $next =~ / $cntl /mx) { } 0 0 0
90 0 $_restore_mode->( $IN ); exit FAILED;
91 }
92 elsif ($next eq $cntl{ERASE}) {
93 0 ▶ if ($len = length $input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = length $input) 0 0 0
94 0 $input = substr $input, 0, $len - 1; emit_to $OUT, "\b \b";
95 }
96  
97 0 next;
98 }
99 elsif ($next eq $cntl{EOF}) {
100 0 $_restore_mode->( $IN );
101 0 ▶ close $IN or throw 'IO error: [_1]', [ $OS_ERROR ];
Branch: 1 branch
BranchTrueFalseTotal
close $IN or &throw("IO error: [_1]", [$Class::Usul::TraitFor::Prompting::OS_ERROR]) 0 0 0
102 0 return $input;
103 }
104 elsif ($next !~ m{ $cntl }mx) {
105 0 $input .= $next;
106  
107 0 ▶ if ($next eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "\n") { } 0 0 0
108 0 ▶ if ($input eq "\n" and defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "\n" and defined $default) 0 0 0
Condition0%0/3
$input eq "\n" and defined $default
!ll&&!rl&&r
000
MC/DC0%0/2
$input eq "\n" and defined $default
$input eq "\n" defined $default
Truth table0%0/3
A and B
A= $input eq "\n"B= defined $default
ABresult
0X0
100
111
109 0 ▶ $text = defined $echo ? $echo x length $default : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $echo ? : 0 0 0
110 0 emit_to $OUT, "[${text}]\n"; $_restore_mode->( $IN );
111  
112 0 ▶ return $onechar ? substr $default, 0, 1 : $default;
Branch: 1 branch
BranchTrueFalseTotal
$onechar ? : 0 0 0
113 }
114  
115 0 $newlines .= "\n";
116 }
117 0 ▶ else { emit_to $OUT, $echo // $next }
Condition0%0/3
$echo // $next
l!l&&r!l&&!r
000
MC/DC0%0/2
$echo // $next
$echo $next
Truth table0%0/3
A // B
A= $echoB= $next
ABresult
1X1
011
000
118 }
119 0 else { $input .= $next }
120 }
121  
122 0 ▶ if ($onechar or not defined $next or $input =~ m{ \Q$RS\E \z }mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($onechar or not defined $next or $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx) 0 0 0
Condition0%0/6
$onechar or not defined $next or $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx
l!l&&r!l&&!r
000
$onechar or not defined $next
l!l&&r!l&&!r
000
MC/DC0%0/3
$onechar or not defined $next or $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx
$onechar not defined $next $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx
Truth table0%0/4
A or not(B) or C
A= $onecharB= not defined $nextC= $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx
ABCresult
1XX1
01X1
0011
0000
123 0 chomp $input; $_restore_mode->( $IN );
124 0 ▶ defined $newlines and emit_to $OUT, $newlines;
Branch: 1 branch
BranchTrueFalseTotal
defined $newlines and &emit_to($OUT, $newlines) 0 0 0
125 0 ▶ return $onechar ? substr $input, 0, 1 : $input;
Branch: 1 branch
BranchTrueFalseTotal
$onechar ? : 0 0 0
126 }
127 }
128  
129 0 return;
130 17 };
131  
132 # Private methods
133 4 ▶my $_prepare = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
134 4 my ($self, $question) = @_; my $add_leader;
135  
136 4 ▶ '+' eq substr $question, 0, 1 and $add_leader = TRUE
Branch: 1 branch
BranchTrueFalseTotal
"+" eq substr($question, 0, 1) and $add_leader = 1 and $question = substr($question, 1) 1 3 4
Condition100%2/2
"+" eq substr($question, 0, 1) and $add_leader = 1
!ll
31
MC/DC100%1/1
"+" eq substr($question, 0, 1) and $add_leader = 1
"+" eq substr($question, 0, 1)
Truth table100%2/2
A
A= "+" eq substr($question, 0, 1)
Aresult
00
11
137 and $question = substr $question, 1;
138 4 $question = $self->loc( $question );
139 4 ▶ $add_leader and $question = $self->add_leader( $question );
Branch: 1 branch
BranchTrueFalseTotal
$add_leader and $question = $self->add_leader($question) 1 3 4
140 4 return $question;
141 17 };
142  
143 # Public methods
144 1 ▶sub anykey {
Subroutine anykey: called CC=2 SCAR=7.1
Pod documented
145 1 my ($self, $prompt) = @_;
146  
147 1 ▶ $prompt = $self->$_prepare( $prompt // 'Press any key to continue' );
Condition50%1/2
$prompt // "Press any key to continue"
l!l
01
MC/DC0%0/1
$prompt // "Press any key to continue"
$prompt
Truth table50%1/2
A
A= $prompt
Aresult
11
00
148  
149 1 return $_prompt->( -p => "${prompt}...", -d => TRUE, -e => NUL, -1 => TRUE );
150 }
151  
152 2 ▶sub get_line { # General text input routine.
Subroutine get_line: called CC=12 SCAR=31.4
Pod documented
153 2 my ($self, $question, @args) = @_; my $opts = $_opts->( 'get_line', @args );
154  
155 2 ▶ $question = $self->$_prepare( $question // 'Enter your answer' );
Condition100%2/2
$question // "Enter your answer"
l!l
11
MC/DC100%1/1
$question // "Enter your answer"
$question
Truth table100%2/2
A
A= $question
Aresult
11
00
156  
157 2 ▶ my $default = $opts->{default} // NUL;
Condition50%1/2
$opts->{"default"} // ""
l!l
20
MC/DC0%0/1
$opts->{"default"} // ""
$opts->{"default"}
Truth table50%1/2
A
A= $opts->{"default"}
Aresult
11
00
158 2 ▶ my $advice = $opts->{quit} ? $self->loc( '([_1] to quit)', QUIT ) : NUL;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'quit'} ? : 0 2 2
159 2 ▶ my $r_prompt = $advice.($opts->{multiline} ? NUL : " [${default}]");
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'multiline'} ? : 0 2 2
160 2 my $l_prompt = $question;
161  
162 2 ▶ if (defined $opts->{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"width"}) 0 2 2
163 0 ▶ my $total = $opts->{width} || $self->config->pwidth;
Condition0%0/3
$opts->{'width'} || $self->config->pwidth
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'width'} || $self->config->pwidth
$opts->{'width'} $self->config->pwidth
Truth table0%0/3
A || B
A= $opts->{'width'}B= $self->config->pwidth
ABresult
1X1
011
000
164 0 my $left_x = $total - (length $r_prompt);
165  
166 0 $l_prompt = sprintf '%-*s', $left_x, $question;
167 }
168  
169 my $prompt = "${l_prompt} ${r_prompt}"
170 2 ▶ . ($opts->{multiline} ? "\n[${default}]" : NUL).BRK;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'multiline'} ? : 0 2 2
171 my $result = $opts->{noecho}
172 2 ▶ ? $_prompt->( -d => $default, -p => $prompt, -e => '*' )
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'noecho'} ? : 0 2 2
173 : $_prompt->( -d => $default, -p => $prompt );
174  
175 2 ▶ $opts->{quit} and defined $result and lc $result eq QUIT and exit FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{"quit"} and defined $result and lc $result eq "q" and exit 1 0 2 2
Condition33%2/6
$opts->{"quit"} and defined $result and lc $result eq "q"
!ll&&!rl&&r
200
$opts->{"quit"} and defined $result
!ll&&!rl&&r
200
MC/DC0%0/3
$opts->{"quit"} and defined $result and lc $result eq "q"
$opts->{"quit"} defined $result lc $result eq "q"
Truth table25%1/4
A and B and C
A= $opts->{"quit"}B= defined $resultC= lc $result eq "q"
ABCresult
0XX0
10X0
1100
1111
176  
177 2 return "${result}";
178 }
179  
180 1 ▶sub get_option { # Select from an numbered list of options
Subroutine get_option: called CC=8 SCAR=22.4
Pod documented
181 1 my ($self, $prompt, @args) = @_; my $opts = $_opts->( 'get_option', @args );
182  
183 1 ▶ $prompt //= '+Select one option from the following list:';
Condition50%1/2
$prompt //= "+Select one option from the following list:"
l!l
01
MC/DC0%0/1
$prompt //= "+Select one option from the following list:"
$prompt
Truth table50%1/2
A
A= $prompt
Aresult
11
00
184  
185 1 ▶ my $no_lead = ('+' eq substr $prompt, 0, 1) ? FALSE : TRUE;
Branch: 1 branch
BranchTrueFalseTotal
'+' eq substr($prompt, 0, 1) ? : 1 0 1
186 1 ▶ my $leader = $no_lead ? NUL : '+'; $prompt =~ s{ \A \+ }{}mx;
Branch: 1 branch
BranchTrueFalseTotal
$no_lead ? : 0 1 1
187 1 ▶ my $max = @{ $opts->{options} // [] };
Condition50%1/2
$opts->{"options"} // []
l!l
01
MC/DC0%0/1
$opts->{"options"} // []
$opts->{"options"}
Truth table50%1/2
A
A= $opts->{"options"}
Aresult
11
00
188  
189 1 $self->output( $prompt, { no_lead => $no_lead } ); my $count = 1;
190  
191 0 my $text = join "\n", map { $_justify_count->( $max, $count++ )." - ${_}" }
192 1 ▶ @{ $opts->{options} // [] };
Condition50%1/2
$opts->{"options"} // []
l!l
01
MC/DC0%0/1
$opts->{"options"} // []
$opts->{"options"}
Truth table50%1/2
A
A= $opts->{"options"}
Aresult
11
00
193  
194 1 $self->output( $text, { cl => TRUE, nl => TRUE, no_lead => $no_lead } );
195  
196 1 my $question = "${leader}Select option";
197 1 my $opt = $self->get_line( $question, $opts );
198  
199 1 ▶ $opt !~ m{ \A \d+ \z }mx and $opt = $opts->{default} // 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt =~ / \A \d+ \z /mx or $opt = $opts->{"default"} // 0 1 0 1
Condition0%0/2
$opts->{"default"} // 0
l!l
00
MC/DC0%0/1
$opts->{"default"} // 0
$opts->{"default"}
Truth table0%0/2
A
A= $opts->{"default"}
Aresult
11
00
200  
201 1 return $opt - 1;
202 }
203  
204 1 ▶sub is_interactive {
Subroutine is_interactive: called CC=1 SCAR=0.0
Pod documented
205 1 my $self = shift; return IO::Interactive::is_interactive( @_ );
206 }
207  
208 1 ▶sub yorn { # General yes or no input routine
Subroutine yorn: called CC=13 SCAR=33.8
Pod documented
209 1 my ($self, $question, @args) = @_; my $opts = $_opts->( 'yorn', @args );
210  
211 1 ▶ $question = $self->$_prepare( $question // 'Choose' );
Condition50%1/2
$question // "Choose"
l!l
01
MC/DC0%0/1
$question // "Choose"
$question
Truth table50%1/2
A
A= $question
Aresult
11
00
212  
213 1 my $no = NO; my $yes = YES; my $result;
214  
215 1 ▶ my $default = $opts->{default} ? $yes : $no;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'default'} ? : 1 0 1
216 1 ▶ my $quit = $opts->{quit } ? QUIT : NUL;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'quit'} ? : 0 1 1
217 1 ▶ my $advice = $quit ? "(${yes}/${no}, ${quit}) " : "(${yes}/${no}) ";
Branch: 1 branch
BranchTrueFalseTotal
$quit ? : 0 1 1
218 1 my $r_prompt = "${advice}[${default}]";
219 1 my $l_prompt = $question;
220  
221 1 ▶ if (defined $opts->{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"width"}) 0 1 1
222 0 ▶ my $max_width = $opts->{width} || $self->config->pwidth;
Condition0%0/3
$opts->{'width'} || $self->config->pwidth
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'width'} || $self->config->pwidth
$opts->{'width'} $self->config->pwidth
Truth table0%0/3
A || B
A= $opts->{'width'}B= $self->config->pwidth
ABresult
1X1
011
000
223 0 my $right_x = length $r_prompt;
224 0 my $left_x = $max_width - $right_x;
225  
226 0 $l_prompt = sprintf '%-*s', $left_x, $question;
227 }
228  
229 1 ▶ my $prompt = "${l_prompt} ${r_prompt}".BRK.($opts->{newline} ? "\n" : NUL);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'newline'} ? : 0 1 1
230  
231 1 ▶ while ($result = $_prompt->( -d => $default, -p => $prompt )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result = &$_prompt("-d", $default, "-p", $prompt)) 1 0 1
232 1 ▶ $quit and $result =~ m{ \A (?: $quit | [\e] ) }imx and exit FAILED;
Branch: 1 branch
BranchTrueFalseTotal
$quit and $result =~ / \A (?: $quit | [\e] ) /mix and exit 1 0 1 1
Condition33%1/3
$quit and $result =~ / \A (?: $quit | [\e] ) /mix
!ll&&!rl&&r
100
MC/DC0%0/2
$quit and $result =~ / \A (?: $quit | [\e] ) /mix
$quit $result =~ / \A (?: $quit | [\e] ) /mix
Truth table33%1/3
A and B
A= $quitB= $result =~ / \A (?: $quit | [\e] ) /mix
ABresult
0X0
100
111
233 1 ▶ $result =~ m{ \A $yes }imx and return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$result =~ / \A $yes /mix and return 1 1 0 1
234 0 ▶ $result =~ m{ \A $no }imx and return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$result =~ / \A $no /mix and return 0 0 0 0
235 }
236  
237 0 return;
238 }
239  
240 17 1;
241  
242 __END__
« Usul/IPC.pm ↑ Summary Usul/Schema.pm »