| 1 | package IO::Prompt::I18N; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2015-01-03'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.80'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(prompt confirm); | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | sub prompt { | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | my ($text, $opts) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prompt: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | $text //= "Enter value"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$text //= "Enter value"
MC/DC0%0/1
$text //= "Enter value"
$text
Truth table0%0/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $answer; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | my $default; | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $default = ${$opts->{var}} if $opts->{var}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | $default = $opts->{default} if defined($opts->{default}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | # prompt | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | print $text; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | print " ($default)" if defined($default) && $opts->{show_default}//1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
defined $default && $opts->{"show_default"} // 1
defined $default and $opts->{"show_default"}
MC/DC0%0/2
defined $default && $opts->{"show_default"} // 1
defined $default
$opts->{"show_default"}
Truth table0%0/3
A and B
A= defined $defaultB= $opts->{"show_default"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | print ":" unless $text =~ /[:?]\s*$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | print " "; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # get input | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $answer = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if (!defined($answer)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | print "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $answer = ""; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | chomp($answer); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # check+process answer | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (defined($default)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $answer = $default if !length($answer); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $success = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($opts->{required}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $success = 0 if !length($answer); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($opts->{regex}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $success = 0 if $answer !~ /$opts->{regex}/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | last if $success; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | ${$opts->{var}} = $answer if $opts->{var}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $answer; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | sub confirm { | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ($text, $opts) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
confirm: not called CC=20 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | state $supported_langs = { | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | en => {yes_words=>[qw/y yes/], no_words=>[qw/n no/] , text=>'Confirm'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | fr => {yes_words=>[qw/o oui/], no_words=>[qw/n non/] , text=>'Confirmer'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | id => {yes_words=>[qw/y ya/] , no_words=>[qw/t tidak/], text=>'Konfirmasi'}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $opts->{lang} //= do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{"lang"} //= do {
if ($ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
elsif ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
else {
"en";
}
}
MC/DC0%0/2
$opts->{"lang"} //= do {
if ($ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
elsif ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
else {
"en";
}
}
$opts->{"lang"}
do {
if ($ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
elsif ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
else {
"en";
}
}
Truth table0%0/3
A //= B
A= $opts->{"lang"}B= do {
if ($ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
elsif ($ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}) {
$1;
}
else {
"en";
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ($ENV{LANG} && $ENV{LANG} =~ /^([a-z]{2})/ && | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
$ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}
$ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/
MC/DC0%0/3
$ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}
$ENV{"LANG"}
$ENV{"LANG"} =~ /^([a-z]{2})/
$supported_langs->{$1}
Truth table0%0/4
A and B and C
A= $ENV{"LANG"}B= $ENV{"LANG"} =~ /^([a-z]{2})/C= $supported_langs->{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $supported_langs->{$1}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | ▶ | } elsif ($ENV{LANGUAGE} && $ENV{LANGUAGE} =~ /^([a-z]{2})/ && | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/
MC/DC0%0/3
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/ and $supported_langs->{$1}
$ENV{"LANGUAGE"}
$ENV{"LANGUAGE"} =~ /^([a-z]{2})/
$supported_langs->{$1}
Truth table0%0/4
A and B and C
A= $ENV{"LANGUAGE"}B= $ENV{"LANGUAGE"} =~ /^([a-z]{2})/C= $supported_langs->{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | $supported_langs->{$1}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | 'en'; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | my $lang = $supported_langs->{$opts->{lang}} | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | or die "Unknown language '$opts->{lang}'"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $text //= $lang->{text}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$text //= $lang->{"text"}
MC/DC0%0/2
$text //= $lang->{"text"}
$text
$lang->{"text"}
Truth table0%0/3
A //= B
A= $textB= $lang->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $opts->{yes_words} //= $lang->{yes_words}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{"yes_words"} //= $lang->{"yes_words"}
MC/DC0%0/2
$opts->{"yes_words"} //= $lang->{"yes_words"}
$opts->{"yes_words"}
$lang->{"yes_words"}
Truth table0%0/3
A //= B
A= $opts->{"yes_words"}B= $lang->{"yes_words"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $opts->{no_words} //= $lang->{no_words}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$opts->{"no_words"} //= $lang->{"no_words"}
MC/DC0%0/2
$opts->{"no_words"} //= $lang->{"no_words"}
$opts->{"no_words"}
$lang->{"no_words"}
Truth table0%0/3
A //= B
A= $opts->{"no_words"}B= $lang->{"no_words"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my $default; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (defined $opts->{default}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($opts->{default}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $default = $opts->{yes_words}[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $default = $opts->{no_words}[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $suffix; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $show_default = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless ($text =~ /[?]/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | $text .= | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | join("", | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | " (", | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | join("/", | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | (map {$opts->{default} ? uc($_) : lc($_)} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | @{ $opts->{yes_words} }), | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | (map {defined($opts->{default}) && !$opts->{default} ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | uc($_) : lc($_)} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $opts->{"default"} and not $opts->{"default"}
MC/DC0%0/2
defined $opts->{"default"} and not $opts->{"default"}
defined $opts->{"default"}
not $opts->{"default"}
Truth table0%0/3
A and not(B)
A= defined $opts->{"default"}B= not $opts->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | @{ $opts->{no_words} }), | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ")?", | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $show_default = 0; # because we already indicate which using uppercase | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $re = join("|", map {quotemeta} | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | (@{$opts->{yes_words}}, @{$opts->{no_words}})); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $re = qr/\A($re)\z/i; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $answer = prompt($text, { | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | regex => $re, | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | show_default => $show_default, | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | default => $default, | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | use experimental 'smartmatch'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $answer ~~ @{$opts->{yes_words}} ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | # ABSTRACT: Prompt user question, with some options (including I18N) | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||