| 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
Condition33%1/3
$ENV{"PERL_MM_USE_DEFAULT"} or $ENV{"PERL_MB_USE_DEFAULT"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $args->{onechar} and return getc $fh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($next eq $cntl{INTERRUPT}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $_restore_mode->( $IN ); exit FAILED; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | elsif ($next eq $cntl{ERASE}) { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($len = length $input) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($input eq "\n" and defined $default) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$input eq "\n" and defined $default
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
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $text = defined $echo ? $echo x length $default : $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | emit_to $OUT, "[${text}]\n"; $_restore_mode->( $IN ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return $onechar ? substr $default, 0, 1 : $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $newlines .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | else { emit_to $OUT, $echo // $next } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$echo // $next
MC/DC0%0/2
$echo // $next
$echo
$next
Truth table0%0/3
A // B
A= $echoB= $next
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$onechar or not defined $next or $input =~ / \Q$Class::Usul::TraitFor::Prompting::RS\E \z /mx
$onechar or not defined $next
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
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | chomp $input; $_restore_mode->( $IN ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | defined $newlines and emit_to $OUT, $newlines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return $onechar ? substr $input, 0, 1 : $input; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
"+" eq substr($question, 0, 1) and $add_leader = 1
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$prompt // "Press any key to continue"
$prompt
Truth table50%1/2
A
A= $prompt
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$question // "Enter your answer"
$question
Truth table100%2/2
A
A= $question
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | ▶ | my $default = $opts->{default} // NUL; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"default"} // ""
MC/DC0%0/1
$opts->{"default"} // ""
$opts->{"default"}
Truth table50%1/2
A
A= $opts->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | ▶ | my $advice = $opts->{quit} ? $self->loc( '([_1] to quit)', QUIT ) : NUL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | ▶ | my $r_prompt = $advice.($opts->{multiline} ? NUL : " [${default}]"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | my $l_prompt = $question; | |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | ▶ | if (defined $opts->{width}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my $total = $opts->{width} || $self->config->pwidth; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{'width'} || $self->config->pwidth
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | my $result = $opts->{noecho} | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | ? $_prompt->( -d => $default, -p => $prompt, -e => '*' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$opts->{"quit"} and defined $result and lc $result eq "q"
$opts->{"quit"} and defined $result
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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:"
MC/DC0%0/1
$prompt //= "+Select one option from the following list:"
$prompt
Truth table50%1/2
A
A= $prompt
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | my $no_lead = ('+' eq substr $prompt, 0, 1) ? FALSE : TRUE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | my $leader = $no_lead ? NUL : '+'; $prompt =~ s{ \A \+ }{}mx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | ▶ | my $max = @{ $opts->{options} // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{"options"} // []
MC/DC0%0/1
$opts->{"options"} // []
$opts->{"options"}
Truth table50%1/2
A
A= $opts->{"options"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$opts->{"options"} // []
$opts->{"options"}
Truth table50%1/2
A
A= $opts->{"options"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$opts->{"default"} // 0
MC/DC0%0/1
$opts->{"default"} // 0
$opts->{"default"}
Truth table0%0/2
A
A= $opts->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$question // "Choose"
$question
Truth table50%1/2
A
A= $question
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | my $no = NO; my $yes = YES; my $result; | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | ▶ | my $default = $opts->{default} ? $yes : $no; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | ▶ | my $quit = $opts->{quit } ? QUIT : NUL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | ▶ | my $advice = $quit ? "(${yes}/${no}, ${quit}) " : "(${yes}/${no}) "; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | my $r_prompt = "${advice}[${default}]"; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | my $l_prompt = $question; | |||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | ▶ | if (defined $opts->{width}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | my $max_width = $opts->{width} || $self->config->pwidth; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{'width'} || $self->config->pwidth
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | while ($result = $_prompt->( -d => $default, -p => $prompt )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | ▶ | $quit and $result =~ m{ \A (?: $quit | [\e] ) }imx and exit FAILED; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$quit and $result =~ / \A (?: $quit | [\e] ) /mix
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
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | ▶ | $result =~ m{ \A $yes }imx and return TRUE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | $result =~ m{ \A $no }imx and return FALSE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | __END__ | ||||||||||||||||||||||||||||||||||||||||||