blib/lib/IO/Prompt/I18N.pm

filter: Statementstmt 21.5% 17 / 79 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 12.1% 23 / 190 CC 37 SCAR 57.9
CC
37
Cov
11%
confirm
378.7
prompt
342.0
BEGIN
1.0
CRAP 327.0
↑ Summary
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"
l!l
00
MC/DC0%0/1
$text //= "Enter value"
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
18 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
19  
20 0 my $answer;
21  
22 my $default;
23 0 ▶ $default = ${$opts->{var}} if $opts->{var};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"var"} 0 0 0
24 0 ▶ $default = $opts->{default} if defined($opts->{default});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"default"} 0 0 0
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
BranchTrueFalseTotal
if defined $default && $opts->{"show_default"} // 1 0 0 0
Condition0%0/5
defined $default && $opts->{"show_default"} // 1
l!l
00
defined $default and $opts->{"show_default"}
!ll&&!rl&&r
000
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"}
ABresult
111
0X0
100
30 0 ▶ print ":" unless $text =~ /[:?]\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[:?]\s*$/ 0 0 0
31 0 print " ";
32  
33 # get input
34 0 $answer = <STDIN>;
35 0 ▶ if (!defined($answer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $answer) 0 0 0
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
BranchTrueFalseTotal
if (defined $default) 0 0 0
43 0 ▶ $answer = $default if !length($answer);
Branch: 1 branch
BranchTrueFalseTotal
unless length $answer 0 0 0
44 }
45 0 my $success = 1;
46 0 ▶ if ($opts->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"required"}) 0 0 0
47 0 ▶ $success = 0 if !length($answer);
Branch: 1 branch
BranchTrueFalseTotal
unless length $answer 0 0 0
48 }
49 0 ▶ if ($opts->{regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"regex"}) 0 0 0
50 0 ▶ $success = 0 if $answer !~ /$opts->{regex}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $answer =~ /$opts->{"regex"}/ 0 0 0
51 }
52 0 ▶ last if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
53 }
54 0 ▶ ${$opts->{var}} = $answer if $opts->{var};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"var"} 0 0 0
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 //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
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"; } }
l!l&&r!l&&!r
000
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"; } }
ABresult
1X1
011
000
70 0 ▶ if ($ENV{LANG} && $ENV{LANG} =~ /^([a-z]{2})/ &&
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'LANG'} and $ENV{'LANG'} =~ /^([a-z]{2})/ and $supported_langs->{$1}) { } 0 0 0
elsif ($ENV{'LANGUAGE'} and $ENV{'LANGUAGE'} =~ /^([a-z]{2})/ and $supported_langs->{$1}) { } 0 0 0
Condition0%0/6
$ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/ and $supported_langs->{$1}
!ll&&!rl&&r
000
$ENV{"LANG"} and $ENV{"LANG"} =~ /^([a-z]{2})/
!ll&&!rl&&r
000
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}
ABCresult
0XX0
10X0
1100
1111
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}
!ll&&!rl&&r
000
$ENV{"LANGUAGE"} and $ENV{"LANGUAGE"} =~ /^([a-z]{2})/
!ll&&!rl&&r
000
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}
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
my $lang = $$supported_langs{$opts->{"lang"}} or die qq[Unknown language '$opts->{"lang"}'] 0 0 0
83 0 ▶ $text //= $lang->{text};
Condition0%0/3
$text //= $lang->{"text"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$text //= $lang->{"text"}
$text $lang->{"text"}
Truth table0%0/3
A //= B
A= $textB= $lang->{"text"}
ABresult
1X1
011
000
84 0 ▶ $opts->{yes_words} //= $lang->{yes_words};
Condition0%0/3
$opts->{"yes_words"} //= $lang->{"yes_words"}
l!l&&r!l&&!r
000
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"}
ABresult
1X1
011
000
85 0 ▶ $opts->{no_words} //= $lang->{no_words};
Condition0%0/3
$opts->{"no_words"} //= $lang->{"no_words"}
l!l&&r!l&&!r
000
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"}
ABresult
1X1
011
000
86  
87 0 my $default;
88 0 ▶ if (defined $opts->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"default"}) 0 0 0
89 0 ▶ if ($opts->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'default'}) { } 0 0 0
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
BranchTrueFalseTotal
unless ($text =~ /[?]/) 0 0 0
99 $text .=
100 join("",
101 " (",
102 join("/",
103 0 ▶ (map {$opts->{default} ? uc($_) : lc($_)}
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'default'} ? : 0 0 0
104 0 @{ $opts->{yes_words} }),
105 (map {defined($opts->{default}) && !$opts->{default} ?
106 0 ▶ uc($_) : lc($_)}
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'default'} && !$opts->{'default'} ? : 0 0 0
Condition0%0/3
defined $opts->{"default"} and not $opts->{"default"}
!ll&&!rl&&r
000
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$answer ~~ \@{$$opts{'yes_words'};} ? : 0 0 0
126 }
127  
128 1 1;
129 # ABSTRACT: Prompt user question, with some options (including I18N)
130  
131 __END__
↑ Summary