| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
4
|
|
|
4
|
|
2037
|
use 5.008008; |
|
|
4
|
|
|
|
|
16
|
|
|
2
|
4
|
|
|
4
|
|
20
|
use strict; |
|
|
4
|
|
|
|
|
7
|
|
|
|
4
|
|
|
|
|
83
|
|
|
3
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
|
4
|
|
|
|
|
10
|
|
|
|
4
|
|
|
|
|
228
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Ask::API; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:TOBYINK'; |
|
8
|
|
|
|
|
|
|
our $VERSION = '0.013'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
23
|
use Moo::Role; |
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
25
|
|
|
11
|
4
|
|
|
4
|
|
4227
|
use Path::Tiny 'path'; |
|
|
4
|
|
|
|
|
36472
|
|
|
|
4
|
|
|
|
|
4209
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
requires 'entry'; # get a string of text |
|
14
|
|
|
|
|
|
|
requires 'info'; # display a string of text |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _lang_support { |
|
17
|
3
|
|
|
3
|
|
6
|
my $self = shift; |
|
18
|
3
|
|
|
|
|
1080
|
require Lingua::Boolean::Tiny; |
|
19
|
3
|
|
|
|
|
28749
|
"Lingua::Boolean::Tiny"->new( @_ ); |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub is_usable { |
|
23
|
0
|
|
|
0
|
0
|
0
|
my ( $self ) = @_; |
|
24
|
0
|
|
|
|
|
0
|
return 1; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub quality { |
|
28
|
0
|
|
|
0
|
0
|
0
|
return 50; |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub warning { |
|
32
|
1
|
|
|
1
|
0
|
449
|
my ( $self, %o ) = @_; |
|
33
|
1
|
|
|
|
|
4
|
$o{text} = "WARNING: $o{text}"; |
|
34
|
1
|
|
|
|
|
5
|
return $self->info( %o ); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub error { |
|
38
|
2
|
|
|
2
|
0
|
8221
|
my ( $self, %o ) = @_; |
|
39
|
2
|
|
|
|
|
9
|
$o{text} = "ERROR: $o{text}"; |
|
40
|
2
|
|
|
|
|
12
|
return $self->info( %o ); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub question { |
|
44
|
3
|
|
|
3
|
0
|
1868
|
my ( $self, %o ) = @_; |
|
45
|
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
16
|
my $response = $self->entry( text => $o{text} ); |
|
47
|
3
|
|
|
|
|
21
|
my $lang = $self->_lang_support( $o{lang} ); |
|
48
|
3
|
|
|
|
|
457
|
$lang->boolean( $response ); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub file_selection { |
|
52
|
2
|
|
|
2
|
0
|
1638
|
my ( $self, %opts ) = ( shift, @_ ); |
|
53
|
|
|
|
|
|
|
|
|
54
|
2
|
|
50
|
|
|
8
|
$opts{text} ||= 'Enter file name'; |
|
55
|
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
3
|
my @chosen; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
FILE: { |
|
59
|
2
|
|
|
|
|
3
|
my $got = $self->entry( text => $opts{text} ); |
|
|
5
|
|
|
|
|
12
|
|
|
60
|
|
|
|
|
|
|
|
|
61
|
5
|
100
|
|
|
|
19
|
if ( not length $got ) { |
|
62
|
1
|
50
|
|
|
|
4
|
last FILE if $opts{multiple}; |
|
63
|
0
|
|
|
|
|
0
|
redo FILE; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
4
|
|
|
|
|
11
|
$got = path $got; |
|
67
|
|
|
|
|
|
|
|
|
68
|
4
|
50
|
33
|
|
|
112
|
if ( $opts{existing} and not $got->exists ) { |
|
69
|
0
|
|
|
|
|
0
|
$self->error( text => 'Does not exist.' ); |
|
70
|
0
|
|
|
|
|
0
|
redo FILE; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
4
|
50
|
33
|
|
|
9
|
if ( $opts{directory} and not $got->is_dir ) { |
|
74
|
0
|
|
|
|
|
0
|
$self->error( text => 'Is not a directory.' ); |
|
75
|
0
|
|
|
|
|
0
|
redo FILE; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
6
|
push @chosen, $got; |
|
79
|
|
|
|
|
|
|
|
|
80
|
4
|
100
|
|
|
|
9
|
if ( $opts{multiple} ) { |
|
81
|
3
|
|
|
|
|
9
|
$self->info( text => 'Enter another file, or leave blank to finish.' ); |
|
82
|
3
|
|
|
|
|
10
|
redo FILE; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
} #/ FILE: |
|
85
|
|
|
|
|
|
|
|
|
86
|
2
|
100
|
|
|
|
12
|
$opts{multiple} ? @chosen : $chosen[0]; |
|
87
|
|
|
|
|
|
|
} #/ sub file_selection |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $format_choices = sub { |
|
90
|
|
|
|
|
|
|
my ( $self, $choices ) = @_; |
|
91
|
|
|
|
|
|
|
join q[, ], map { sprintf( '"%s" (%s)', @$_ ) } @$choices; |
|
92
|
|
|
|
|
|
|
}; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $filter_chosen = sub { |
|
95
|
|
|
|
|
|
|
my ( $self, $choices, $response ) = @_; |
|
96
|
|
|
|
|
|
|
my $valid = {}; |
|
97
|
|
|
|
|
|
|
$valid->{ $_->[0] }++ for @$choices; |
|
98
|
|
|
|
|
|
|
my @choices = ( $response =~ /\w+/g ); |
|
99
|
|
|
|
|
|
|
return ( |
|
100
|
|
|
|
|
|
|
[ grep $valid->{$_}, @choices ], |
|
101
|
|
|
|
|
|
|
[ grep !$valid->{$_}, @choices ], |
|
102
|
|
|
|
|
|
|
); |
|
103
|
|
|
|
|
|
|
}; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub multiple_choice { |
|
106
|
0
|
|
|
0
|
0
|
|
my ( $self, %o ) = @_; |
|
107
|
0
|
|
|
|
|
|
my $choices = $self->$format_choices( $o{choices} ); |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my ( $allowed, $disallowed, $repeat ); |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
for ( ; ; ) { |
|
112
|
0
|
|
0
|
|
|
|
my $response = $self->entry( |
|
113
|
|
|
|
|
|
|
text => |
|
114
|
|
|
|
|
|
|
"$o{text}. Choices: $choices. (Separate multiple choices with white space.)", |
|
115
|
|
|
|
|
|
|
entry_text => $repeat || '', |
|
116
|
|
|
|
|
|
|
); |
|
117
|
0
|
|
|
|
|
|
( $allowed, $disallowed ) = $self->$filter_chosen( $o{choices}, $response ); |
|
118
|
0
|
0
|
|
|
|
|
if ( @$disallowed ) { |
|
119
|
0
|
|
|
|
|
|
my $d = join q[, ], @$disallowed; |
|
120
|
0
|
|
|
|
|
|
$self->error( |
|
121
|
|
|
|
|
|
|
text => "Not valid: $d. Please try again.", |
|
122
|
|
|
|
|
|
|
); |
|
123
|
0
|
|
|
|
|
|
$repeat = join q[ ], @$allowed; |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
else { |
|
126
|
0
|
|
|
|
|
|
last; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} #/ for ( ; ; ) |
|
129
|
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
return @$allowed; |
|
131
|
|
|
|
|
|
|
} #/ sub multiple_choice |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub single_choice { |
|
134
|
0
|
|
|
0
|
0
|
|
my ( $self, %o ) = @_; |
|
135
|
0
|
|
|
|
|
|
my $choices = $self->$format_choices( $o{choices} ); |
|
136
|
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my ( $allowed, $disallowed, $repeat ); |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
for ( ; ; ) { |
|
140
|
0
|
|
0
|
|
|
|
my $response = $self->entry( |
|
141
|
|
|
|
|
|
|
text => "$o{text}. Choices: $choices. (Choose one.)", |
|
142
|
|
|
|
|
|
|
entry_text => $repeat || '', |
|
143
|
|
|
|
|
|
|
); |
|
144
|
0
|
|
|
|
|
|
( $allowed, $disallowed ) = $self->$filter_chosen( $o{choices}, $response ); |
|
145
|
0
|
0
|
|
|
|
|
if ( @$disallowed ) { |
|
|
|
0
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $d = join q[, ], @$disallowed; |
|
147
|
0
|
|
|
|
|
|
$self->error( |
|
148
|
|
|
|
|
|
|
text => "Not valid: $d. Please try again.", |
|
149
|
|
|
|
|
|
|
); |
|
150
|
0
|
|
|
|
|
|
$repeat = $allowed->[0]; |
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
elsif ( @$allowed != 1 ) { |
|
153
|
0
|
|
|
|
|
|
$self->error( |
|
154
|
|
|
|
|
|
|
text => "Not valid: choose one.", |
|
155
|
|
|
|
|
|
|
); |
|
156
|
0
|
|
|
|
|
|
$repeat = $allowed->[0]; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
else { |
|
159
|
0
|
|
|
|
|
|
last; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
} #/ for ( ; ; ) |
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
return $allowed->[0]; |
|
164
|
|
|
|
|
|
|
} #/ sub single_choice |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
__END__ |