git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[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
Previous: Rasmus VillemoesNext: Rasmus Villemoes
Message 5 of 7 in “[RFC] git-send-email: Cache generated message-ids, use them when prompting”
  1. Rasmus VillemoesAug 17, 2013
  2. Junio C HamanoAug 18, 2013
  3. brian m. carlsonAug 18, 2013
  4. 0/2 git-send-email: Message-ID cachingRasmus Villemoes, Aug 21, 2013
  5. 1/2 git-send-email: add optional 'choices' parameter to the ask subRasmus Villemoes, Aug 21, 2013
  6. 2/2 git-send-email: Cache generated message-ids, use them when promptingRasmus Villemoes, Aug 21, 2013
  7. Junio C HamanoAug 22, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.