line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
23198
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
2
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
65
|
|
3
|
|
|
|
|
|
|
package App::Cmd::Plugin::Prompt; |
4
|
|
|
|
|
|
|
{ |
5
|
|
|
|
|
|
|
$App::Cmd::Plugin::Prompt::VERSION = '1.005'; |
6
|
|
|
|
|
|
|
} |
7
|
|
|
|
|
|
|
# ABSTRACT: plug prompting routines into your commands |
8
|
1
|
|
|
|
|
9
|
use App::Cmd::Setup -plugin => { |
9
|
|
|
|
|
|
|
exports => [ qw(prompt_str prompt_yn prompt_any_key) ], |
10
|
1
|
|
|
1
|
|
917
|
}; |
|
1
|
|
|
|
|
90789
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
1570
|
use Term::ReadKey; |
|
1
|
|
|
|
|
4424
|
|
|
1
|
|
|
|
|
647
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub prompt_str { |
16
|
0
|
|
|
0
|
1
|
|
my ($plugin, $cmd, $message, $opt) = @_; |
17
|
0
|
0
|
0
|
|
|
|
if ($opt->{default} && $opt->{valid} && ! $opt->{no_valid_default}) { |
|
|
|
0
|
|
|
|
|
18
|
0
|
0
|
0
|
|
|
|
Carp::croak( |
19
|
|
|
|
|
|
|
$opt->{invalid_default_error} || "'default' must pass 'valid' parameter" |
20
|
|
|
|
|
|
|
) unless $opt->{valid}->($opt->{default}); |
21
|
|
|
|
|
|
|
} |
22
|
0
|
|
0
|
0
|
|
|
$opt->{input} ||= sub { scalar }; |
|
0
|
|
|
|
|
|
|
23
|
0
|
|
0
|
0
|
|
|
$opt->{valid} ||= sub { 1 }; |
|
0
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$opt->{output} ||= sub { |
25
|
0
|
0
|
|
0
|
|
|
if (defined $_[1]) { |
26
|
0
|
|
|
|
|
|
printf "%s [%s]: ", @_; |
27
|
|
|
|
|
|
|
} else { |
28
|
0
|
|
|
|
|
|
printf "%s: ", $_[0]; |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
0
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $response; |
33
|
0
|
|
0
|
|
|
|
while (!defined($response) || !$opt->{valid}->($response)) { |
34
|
0
|
|
0
|
|
|
|
$opt->{output}->( |
35
|
|
|
|
|
|
|
$message, |
36
|
|
|
|
|
|
|
($opt->{choices} || $opt->{default} || undef), |
37
|
|
|
|
|
|
|
); |
38
|
0
|
|
|
|
|
|
$response = $opt->{input}->(); |
39
|
0
|
|
|
|
|
|
chomp($response); |
40
|
0
|
0
|
0
|
|
|
|
if ($opt->{default} && ! length($response)) { |
41
|
0
|
|
|
|
|
|
$response = $opt->{default}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
|
return $response; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub prompt_yn { |
49
|
0
|
|
|
0
|
1
|
|
my ($plugin, $cmd, $message, $opt) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
0
|
|
|
|
Carp::croak("default must be y or n or 0 or 1") |
52
|
|
|
|
|
|
|
if defined $opt->{default} |
53
|
|
|
|
|
|
|
and $opt->{default} !~ /\A[yn01]\z/; |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
my $choices = (not defined $opt->{default}) ? 'y/n' |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
56
|
|
|
|
|
|
|
: $opt->{default} eq 'y' ? 'Y/n' |
57
|
|
|
|
|
|
|
: $opt->{default} eq 'n' ? 'y/N' |
58
|
|
|
|
|
|
|
: $opt->{default} ? 'Y/n' |
59
|
|
|
|
|
|
|
: 'y/N'; |
60
|
|
|
|
|
|
|
|
61
|
0
|
0
|
0
|
|
|
|
my $default = ($opt->{default}||'') =~ /\A\d\z/ |
|
|
0
|
|
|
|
|
|
62
|
|
|
|
|
|
|
? ($opt->{default} ? 'y' : 'n') |
63
|
|
|
|
|
|
|
: $opt->{default}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $response = $plugin->prompt_str( |
66
|
|
|
|
|
|
|
$cmd, |
67
|
|
|
|
|
|
|
$message, |
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
choices => $choices, |
70
|
0
|
0
|
|
0
|
|
|
valid => sub { lc($_[0]) eq 'y' || lc($_[0]) eq 'n' }, |
71
|
0
|
|
|
|
|
|
default => $default, |
72
|
|
|
|
|
|
|
}, |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return lc($response) eq 'y'; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub prompt_any_key { |
80
|
0
|
|
|
0
|
1
|
|
my ($plugin, $cmd, $prompt) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
0
|
|
|
|
$prompt ||= "press any key to continue"; |
83
|
0
|
|
|
|
|
|
print $prompt; |
84
|
0
|
|
|
|
|
|
Term::ReadKey::ReadMode 'cbreak'; |
85
|
0
|
|
|
|
|
|
Term::ReadKey::ReadKey(0); |
86
|
0
|
|
|
|
|
|
Term::ReadKey::ReadMode 'normal'; |
87
|
0
|
|
|
|
|
|
print "\n"; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |