[PATCH 1/2] git-send-email: add optional 'choices' parameter to the ask sub
- From
Rasmus Villemoes <rv@rasmusvillemoes.dk>
- Date
- Aug 21, 2013, 19:04 UTC
- Message-ID
- <1377111862-13199-2-git-send-email-rv@rasmusvillemoes.dk>
- In-Reply-To
- <1377111862-13199-1-git-send-email-rv@rasmusvillemoes.dk>
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> --- git-send-email.perl | 11 +++++++++++ 1 file changed, 11 insertions(+)
diff --git a/git-send-email.perl b/git-send-email.perl index 2162478..ac3b02d 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; }
-- 1.7.9.5