summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Villemoes <rv@rasmusvillemoes.dk>2013-08-21 19:04:21 +0000
committerJunio C Hamano <gitster@pobox.com>2013-08-21 16:39:21 -0700
commit2be95bd6a73c486544bd08ca3d00b240d451360c (patch)
treee25613f84b0c5eb91f6a31371d050de2555280ae
parent4d06473928ee574910accbde05c19ef2263abdf6 (diff)
downloadgit-2be95bd6a73c486544bd08ca3d00b240d451360c.tar.gz
git-send-email: add optional 'choices' parameter to the ask sub
Make it possible for callers of ask() to provide a list of choices. Entering an appropriate integer chooses from that list, otherwise the input is treated as usual. Each choice can either be a single string, which is used both for the prompt and for the return value, or a two-element array ref, where the zeroth element is the choice and the first is used for the prompt. Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rwxr-xr-xgit-send-email.perl11
1 files changed, 11 insertions, 0 deletions
diff --git a/git-send-email.perl b/git-send-email.perl
index 2162478392..ac3b02da68 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -680,11 +680,18 @@ sub ask {
my $valid_re = $arg{valid_re};
my $default = $arg{default};
my $confirm_only = $arg{confirm_only};
+ my $choices = $arg{choices} || [];
my $resp;
my $i = 0;
return defined $default ? $default : undef
unless defined $term->IN and defined fileno($term->IN) and
defined $term->OUT and defined fileno($term->OUT);
+ for (@$choices) {
+ printf "(%d) %s\n", $i++, ref($_) eq 'ARRAY' ? $_->[1] : $_;
+ }
+ printf "Enter 0-%d to choose from the above list\n", $i-1
+ if (@$choices);
+ $i = 0;
while ($i++ < 10) {
$resp = $term->readline($prompt);
if (!defined $resp) { # EOF
@@ -694,6 +701,10 @@ sub ask {
if ($resp eq '' and defined $default) {
return $default;
}
+ if (@$choices && $resp =~ m/^[0-9]+$/ && $resp < @$choices) {
+ my $c = $choices->[$resp];
+ return ref($c) eq 'ARRAY' ? $c->[0] : $c;
+ }
if (!defined $valid_re or $resp =~ /$valid_re/) {
return $resp;
}