Skip to contentSkip to conversation

Highlight keyboard shortcuts in git-add--interactive

PATCH In review Wincent Colaiuta 5 participants 17 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Highlight keyboard shortcuts in git-add--interactive

The user interface provided by the command loop in git-add--interactive gives the impression that subcommands can only be launched by entering an integer identifier from 1 through 8.

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the first matching option.

This patch makes this feature a little more obvious by highlighting the first character of each subcommand (for example "patch" is displayed as "[p]atch"). The mechanism for doing this is to add an optional third element to the array defining each subcommand; if present, it will be used for display purposes, while the actual name of the subcommand (the first element) is still used for matching purposes.

---

And another thought: if the colorization for git-add--interactive goes ahead, we could drop the square brackets used here in favor of underline or boldface, if people find that more attractive.

 git-add--interactive.perl |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
git-add--interactive.perl +9 -9
index 0317ad9..2b1c55a 100755
@@ -179,7 +179,7 @@ sub list_and_choose {
 			my $print = $stuff[$i];
 			if (ref $print) {
 				if ((ref $print) eq 'ARRAY') {
-					$print = $print->[0];
+					$print = $print->[2] || $print->[0];
 				}
 				else {
 					$print = $print->{PRINT};
@@ -774,14 +774,14 @@ EOF
 }
 
 sub main_loop {
-	my @cmd = ([ 'status', \&status_cmd, ],
-		   [ 'update', \&update_cmd, ],
-		   [ 'revert', \&revert_cmd, ],
-		   [ 'add untracked', \&add_untracked_cmd, ],
-		   [ 'patch', \&patch_update_cmd, ],
-		   [ 'diff', \&diff_cmd, ],
-		   [ 'quit', \&quit_cmd, ],
-		   [ 'help', \&help_cmd, ],
+	my @cmd = ([ 'status', \&status_cmd, '[s]tatus', ],
+		   [ 'update', \&update_cmd, '[u]date', ],
+		   [ 'revert', \&revert_cmd, '[r]evert', ],
+		   [ 'add untracked', \&add_untracked_cmd, '[a]dd untracked', ],
+		   [ 'patch', \&patch_update_cmd, '[p]atch', ],
+		   [ 'diff', \&diff_cmd, '[d]iff', ],
+		   [ 'quit', \&quit_cmd, '[q]uit', ],
+		   [ 'help', \&help_cmd, '[h]elp', ],
 	);
 	while (1) {
 		my ($it) = list_and_choose({ PROMPT => 'What now',
Signature · 1 line
1.5.3.6.866.g67e44
#1 lore raw
Quoted 10 lines · Wincent Colaiuta

The user interface provided by the command loop in git-add--interactive gives the impression that subcommands can only be launched by entering an integer identifier from 1 through 8.

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the first matching option.

This patch makes this feature a little more obvious by highlighting the first character of each subcommand (for example "patch" is displayed as "[p]atch"). The mechanism for doing this is to add an optional third

I think this is reasonable. It is also a candidate for colorization in Dan Zwell's patches. Dan, any progress on the next revision of the patch series?

-Peff

#2 lore raw
Quoted 3 lines · Jeff King

Dan, any progress on the next revision of the patch series?

Yeah, I've been busy with school work, but Thanksgiving break has just started. I hope to have something to send tonight, though it probably will not be the final draft, as it has quite a bit of changes from the last version.

Dan

#3 lore raw
In reply to Wincent Colaiuta

git-add--interactive.perl EOF · line 778

Quoted 1 line · Wincent Colaiuta
git-add--interactive.perl +1 -0
+		   [ 'update', \&update_cmd, '[u]date', ],
                                                 ^

"p" missing in "update".

Signature · 1 line
Matthieu
#4 lore raw
In reply to Wincent Colaiuta
Quoted 11 lines · Wincent Colaiuta
git-add--interactive.perl +0 -7
@@ -774,14 +774,14 @@ EOF
 }
 
 sub main_loop {
-	my @cmd = ([ 'status', \&status_cmd, ],
-		   [ 'update', \&update_cmd, ],
-		   [ 'revert', \&revert_cmd, ],
-		   [ 'add untracked', \&add_untracked_cmd, ],
-		   [ 'patch', \&patch_update_cmd, ],
-		   [ 'diff', \&diff_cmd, ],
-		   [ 'quit', \&quit_cmd, ],

git-add--interactive.perl EOF · line 785

Quoted 10 lines
git-add--interactive.perl +8 -1
-		   [ 'help', \&help_cmd, ],
+	my @cmd = ([ 'status', \&status_cmd, '[s]tatus', ],
+		   [ 'update', \&update_cmd, '[u]date', ],
+		   [ 'revert', \&revert_cmd, '[r]evert', ],
+		   [ 'add untracked', \&add_untracked_cmd, '[a]dd untracked', ],
+		   [ 'patch', \&patch_update_cmd, '[p]atch', ],
+		   [ 'diff', \&diff_cmd, '[d]iff', ],
+		   [ 'quit', \&quit_cmd, '[q]uit', ],
+		   [ 'help', \&help_cmd, '[h]elp', ],
 	);

I like the general idea of making it more obvious that you can use the unique prefix, but I think you should make list_and_choose do this automatically without adding a redundant element in the command array.

If you do so, the same highlighting will automatically appear when you are picking which paths to update in the update subcommand, for example.

#5 lore raw

PATCH 1/1 Highlight keyboard shortcuts in git-add--interactive

The user interface provided by the command loop in git-add--interactive gives the impression that subcommands can only be launched by entering an integer identifier from 1 through 8.

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the first matching option.

This patch makes this feature a little more obvious by highlighting the first character of each subcommand (for example "patch" is displayed as "[p]atch").

A new function is added to detect the shortest unique prefix and this is used to decide what to highlight. Highlighting is also applied when choosing files.

In the case where the common prefix may be unreasonably large highlighting is omitted; in this patch the soft limit (above which the highlighting will be omitted for a particular item) is 0 (in other words, there is no soft limit) and the hard limit (above which highlighting will be omitted for all items) is 3, but this can be tweaked.

The actual highlighting is done by the highlight_prefix function, which will enable us to implement ANSI color code-based highlighting (most likely using underline or boldface) in the future.

---
 git-add--interactive.perl |   87 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 82 insertions(+), 5 deletions(-)
git-add--interactive.perl +82 -5
index fb1e92a..6e5781b 100755
@@ -44,7 +44,6 @@ my $status_fmt = '%12s %12s %s';
 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
 
 # Returns list of hashes, contents of each of which are:
-# PRINT:	print message
 # VALUE:	pathname
 # BINARY:	is a binary path
 # INDEX:	is index different from HEAD?
@@ -122,8 +121,6 @@ sub list_modified {
 		}
 		push @return, +{
 			VALUE => $_,
-			PRINT => (sprintf $status_fmt,
-				  $it->{INDEX}, $it->{FILE}, $_),
 			%$it,
 		};
 	}
@@ -159,10 +156,82 @@ sub find_unique {
 	return $found;
 }
 
+# inserts string into trie and updates count for each character
+sub update_trie {
+	my ($trie, $string) = @_;
+	foreach (split //, $string) {
+		$trie = $trie->{$_} ||= {COUNT => 0};
+		$trie->{COUNT}++;
+	}
+}
+
+# returns an array of tuples (prefix, remainder)
+sub find_unique_prefixes {
+	my @stuff = @_;
+	my @return = ();
+
+	# any single prefix exceeding the soft limit is omitted
+	# if any prefix exceeds the hard limit all are omitted
+	# 0 indicates no limit
+	my $soft_limit = 0;
+	my $hard_limit = 3;
+
+	# build a trie modelling all possible options
+	my %trie;
+	foreach my $print (@stuff) {
+		if ((ref $print) eq 'ARRAY') {
+			$print = $print->[0];
+		}
+		else {
+			$print = $print->{VALUE};
+		}
+		update_trie(\%trie, $print);
+		push @return, $print;
+	}
+
+	# use the trie to find the unique prefixes
+	for (my $i = 0; $i < @return; $i++) {
+		my $ret = $return[$i];
+		my @letters = split //, $ret;
+		my %search = %trie;
+		my ($prefix, $remainder);
+		my $j;
+		for ($j = 0; $j < @letters; $j++) {
+			my $letter = $letters[$j];
+			if ($search{$letter}{COUNT} == 1) {
+				$prefix = substr $ret, 0, $j + 1;
+				$remainder = substr $ret, $j + 1;
+				last;
+			}
+			else {
+				my $prefix = substr $ret, 0, $j;
+				return ()
+				    if ($hard_limit && $j + 1 > $hard_limit);
+			}
+			%search = %{$search{$letter}};
+		}
+		if ($soft_limit && $j + 1 > $soft_limit) {
+			$prefix = undef;
+			$remainder = $ret;
+		}
+		$return[$i] = [$prefix, $remainder];
+	}
+	return @return;
+}
+
+# given a prefix/remainder tuple return a string with the prefix highlighted
+# for now use square brackets; later might use ANSI colors (underline, bold)
+sub highlight_prefix {
+	my $prefix = shift;
+	my $remainder = shift;
+	$prefix ? "[$prefix]$remainder" : $remainder;
+}
+
 sub list_and_choose {
 	my ($opts, @stuff) = @_;
 	my (@chosen, @return);
 	my $i;
+	my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
 
       TOPLOOP:
 	while (1) {
@@ -179,10 +248,18 @@ sub list_and_choose {
 			my $print = $stuff[$i];
 			if (ref $print) {
 				if ((ref $print) eq 'ARRAY') {
-					$print = $print->[0];
+					$print = @prefixes ?
+					    highlight_prefix(@{$prefixes[$i]}) :
+					    $print->[0];
 				}
 				else {
-					$print = $print->{PRINT};
+					my $value = @prefixes ?
+					    highlight_prefix(@{$prefixes[$i]}) :
+					    $print->{VALUE};
+					$print = sprintf($status_fmt,
+					    $print->{INDEX},
+					    $print->{FILE},
+					    $value);
 				}
 			}
 			printf("%s%2d: %s", $chosen, $i+1, $print);
Signature · 1 line
1.5.3.6.953.gdffc
Acked-by on v1 1/1
#6 lore raw
Quoted 3 lines · Wincent Colaiuta

A new function is added to detect the shortest unique prefix and this is used to decide what to highlight. Highlighting is also applied when choosing files.

I think this is very nicely implemented.

git-add--interactive.perl sub find_unique { · line 170

Quoted 3 lines
git-add--interactive.perl +3 -0
+# returns an array of tuples (prefix, remainder)
+sub find_unique_prefixes {
+	my @stuff = @_;

I know we generally use this more C-ish argument convention to document "here are the arguments to this function", but it does actually make a copy of the @_ array (and using @_ implies a potentially large number of arguments).

It probably doesn't matter here, though, since add--interactive is not performance critical, and you probably can't have more than a few dozen entries before it becomes unreadable anyway.

-Peff

#16 lore raw
In reply to Junio C Hamano
Quoted 28 lines · Junio C Hamano
Quoted 11 lines · Wincent Colaiuta
git-add--interactive.perl +0 -7
@@ -774,14 +774,14 @@ EOF
 }
 
 sub main_loop {
-	my @cmd = ([ 'status', \&status_cmd, ],
-		   [ 'update', \&update_cmd, ],
-		   [ 'revert', \&revert_cmd, ],
-		   [ 'add untracked', \&add_untracked_cmd, ],
-		   [ 'patch', \&patch_update_cmd, ],
-		   [ 'diff', \&diff_cmd, ],
-		   [ 'quit', \&quit_cmd, ],

git-add--interactive.perl EOF · line 785

Quoted 10 lines
git-add--interactive.perl +8 -1
-		   [ 'help', \&help_cmd, ],
+	my @cmd = ([ 'status', \&status_cmd, '[s]tatus', ],
+		   [ 'update', \&update_cmd, '[u]date', ],
+		   [ 'revert', \&revert_cmd, '[r]evert', ],
+		   [ 'add untracked', \&add_untracked_cmd, '[a]dd untracked', ],
+		   [ 'patch', \&patch_update_cmd, '[p]atch', ],
+		   [ 'diff', \&diff_cmd, '[d]iff', ],
+		   [ 'quit', \&quit_cmd, '[q]uit', ],
+		   [ 'help', \&help_cmd, '[h]elp', ],
 	);

I like the general idea of making it more obvious that you can use the unique prefix, but I think you should make list_and_choose do this automatically without adding a redundant element in the command array.

Quoted 3 lines

If you do so, the same highlighting will automatically appear when you are picking which paths to update in the update subcommand, for example.

Yes, I did consider that, and it's very easy when all the options have a unique, single-letter prefix, as is the case with the main command loop. But what to do if you've got a bunch of paths with lengthy common prefixes? eg. what would you highlight here?

lib/ssl/crypto/foo.c
lib/ssl/crypto/bar.c
lib/ssl/crypto/baz.c

Highlighting "lib/ssl/crypto/f", "lib/ssl/crypto/bar" and "lib/ssl/ crypto/baz" doesn't sound like much help... Maybe there should be some
limit: if you need to go more than 3 characters deep in order to differentiate unique prefixes then perhaps highlighting should be omitted in that case. What do you think of that idea?

Cheers,
Wincent

#7 lore raw
In reply to Wincent Colaiuta
Quoted 2 lines · Wincent Colaiuta

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the first matching option.

I'd run s/the first/the uniquely/ here.

When list_and_choose() function is letting you choose more than one items, its prompt becomes ">> ", instead of "> " that is used for a singleton choice. To that prompt, you can say "3-7" (Add these 5 items to the choice), "*" (I want all of them), "-2-4" (exclude 2 and 3 and 4 from the set I have chosen so far). These are also "hidden", and need to be documented, but that would be a separate patch.

git-add--interactive.perl sub find_unique { · line 228

Quoted 7 lines
git-add--interactive.perl +7 -0
+# given a prefix/remainder tuple return a string with the prefix highlighted
+# for now use square brackets; later might use ANSI colors (underline, bold)
+sub highlight_prefix {
+	my $prefix = shift;
+	my $remainder = shift;
+	$prefix ? "[$prefix]$remainder" : $remainder;
+}

I'd rewrite the last line to:

	return (defined $prefix) ? "[$prefix]$remainder" : $remainder;

just in case the unique prefix is "0". Otherwise you would lose the first letter from "00ReadMe" and show remainder "0ReadMe" alone.

#8 lore raw

PATCH 1/2 Highlight keyboard shortcuts in git-add--interactive

The user interface provided by the command loop in git-add--interactive gives the impression that subcommands can only be launched by entering an integer identifier from 1 through 8.

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the uniquely matching option.

This patch makes this feature a little more obvious by highlighting the first character of each subcommand (for example "patch" is displayed as "[p]atch").

A new function is added to detect the shortest unique prefix and this is used to decide what to highlight. Highlighting is also applied when choosing files.

In the case where the common prefix may be unreasonably large highlighting is omitted; in this patch the soft limit (above which the highlighting will be omitted for a particular item) is 0 (in other words, there is no soft limit) and the hard limit (above which highlighting will be omitted for all items) is 3, but this can be tweaked.

The actual highlighting is done by the highlight_prefix function, which will enable us to implement ANSI color code-based highlighting (most likely using underline or boldface) in the future.

---

Three things to note:

1. I don't actually find the "[p]atch" highlighting using brackets all that attractive, especially when highlighting paths. This is especially true when the common prefix is lengthy. This is why I've set the "hard limit" to a very low 3 in this patch. I am basically waiting on the stalled "color" series; once that's in "next" then I'd like to switch to a highlight style that uses underlining.

2. The follow-up patch tweaks this so that the "Add untracked" subcommand can benefit from it as well. Junio, you might want to squash the two patches into one seeing as the second patch just refactors something in the first patch; but I wanted to send them to the list as two separate patches, at least for the purposes of review, because they really are two separate behaviours.

3. I tried to find the patch that Junio sent out earlier allowing multiple selection in the "Patch" subcommand, to see how this interplays with that, but I couldn't locate it.

 git-add--interactive.perl |   97 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 92 insertions(+), 5 deletions(-)
git-add--interactive.perl +92 -5
index fb1e92a..0fb808f 100755
@@ -44,7 +44,6 @@ my $status_fmt = '%12s %12s %s';
 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
 
 # Returns list of hashes, contents of each of which are:
-# PRINT:	print message
 # VALUE:	pathname
 # BINARY:	is a binary path
 # INDEX:	is index different from HEAD?
@@ -122,8 +121,6 @@ sub list_modified {
 		}
 		push @return, +{
 			VALUE => $_,
-			PRINT => (sprintf $status_fmt,
-				  $it->{INDEX}, $it->{FILE}, $_),
 			%$it,
 		};
 	}
@@ -159,10 +156,92 @@ sub find_unique {
 	return $found;
 }
 
+# inserts string into trie and updates count for each character
+sub update_trie {
+	my ($trie, $string) = @_;
+	foreach (split //, $string) {
+		$trie = $trie->{$_} ||= {COUNT => 0};
+		$trie->{COUNT}++;
+	}
+}
+
+# returns an array of tuples (prefix, remainder)
+sub find_unique_prefixes {
+	my @stuff = @_;
+	my @return = ();
+
+	# any single prefix exceeding the soft limit is omitted
+	# if any prefix exceeds the hard limit all are omitted
+	# 0 indicates no limit
+	my $soft_limit = 0;
+	my $hard_limit = 3;
+
+	# build a trie modelling all possible options
+	my %trie;
+	foreach my $print (@stuff) {
+		if ((ref $print) eq 'ARRAY') {
+			$print = $print->[0];
+		}
+		elsif ((ref $print) eq 'HASH') {
+			$print = $print->{VALUE};
+		}
+		update_trie(\%trie, $print);
+		push @return, $print;
+	}
+
+	# use the trie to find the unique prefixes
+	for (my $i = 0; $i < @return; $i++) {
+		my $ret = $return[$i];
+		my @letters = split //, $ret;
+		my %search = %trie;
+		my ($prefix, $remainder);
+		my $j;
+		for ($j = 0; $j < @letters; $j++) {
+			my $letter = $letters[$j];
+			if ($search{$letter}{COUNT} == 1) {
+				$prefix = substr $ret, 0, $j + 1;
+				$remainder = substr $ret, $j + 1;
+				last;
+			}
+			else {
+				my $prefix = substr $ret, 0, $j;
+				return ()
+				    if ($hard_limit && $j + 1 > $hard_limit);
+			}
+			%search = %{$search{$letter}};
+		}
+		if ($soft_limit && $j + 1 > $soft_limit) {
+			$prefix = undef;
+			$remainder = $ret;
+		}
+		$return[$i] = [$prefix, $remainder];
+	}
+	return @return;
+}
+
+# filters out prefixes which have special meaning to list_and_choose()
+sub is_valid_prefix {
+	my $prefix = shift;
+	my $valid = (defined $prefix) &&
+	    !($prefix =~ /[\s,]/) && # separators
+	    !($prefix =~ /^-/) &&    # deselection
+	    !($prefix =~ /^\d+/) &&  # selection
+	    ($prefix ne '*');        # "all" wildcard
+}
+
+# given a prefix/remainder tuple return a string with the prefix highlighted
+# for now use square brackets; later might use ANSI colors (underline, bold)
+sub highlight_prefix {
+	my $prefix = shift;
+	my $remainder = shift;
+	is_valid_prefix($prefix) ? "[$prefix]$remainder" : $remainder;
+}
+
 sub list_and_choose {
 	my ($opts, @stuff) = @_;
 	my (@chosen, @return);
 	my $i;
+	my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
 
       TOPLOOP:
 	while (1) {
@@ -179,10 +258,18 @@ sub list_and_choose {
 			my $print = $stuff[$i];
 			if (ref $print) {
 				if ((ref $print) eq 'ARRAY') {
-					$print = $print->[0];
+					$print = @prefixes ?
+					    highlight_prefix(@{$prefixes[$i]}) :
+					    $print->[0];
 				}
 				else {
-					$print = $print->{PRINT};
+					my $value = @prefixes ?
+					    highlight_prefix(@{$prefixes[$i]}) :
+					    $print->{VALUE};
+					$print = sprintf($status_fmt,
+					    $print->{INDEX},
+					    $print->{FILE},
+					    $value);
 				}
 			}
 			printf("%s%2d: %s", $chosen, $i+1, $print);
Signature · 1 line
1.5.3.6.953.gdffc
#9 lore raw

PATCH 2/2 Teach git-add--interactive to highlight untracked file prefixes

Tweak the list_and_choose function so that untracked files will use the automatic prefix detection machinery.

This works because while previously we handled arrays (command menus), hashes (patch subcommand) now we explicitly handle strings (add untracked subcommand).

---
 git-add--interactive.perl |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)
git-add--interactive.perl +15 -15
index 0fb808f..a1aee21 100755
@@ -256,21 +256,21 @@ sub list_and_choose {
 		for ($i = 0; $i < @stuff; $i++) {
 			my $chosen = $chosen[$i] ? '*' : ' ';
 			my $print = $stuff[$i];
-			if (ref $print) {
-				if ((ref $print) eq 'ARRAY') {
-					$print = @prefixes ?
-					    highlight_prefix(@{$prefixes[$i]}) :
-					    $print->[0];
-				}
-				else {
-					my $value = @prefixes ?
-					    highlight_prefix(@{$prefixes[$i]}) :
-					    $print->{VALUE};
-					$print = sprintf($status_fmt,
-					    $print->{INDEX},
-					    $print->{FILE},
-					    $value);
-				}
+			my $ref = ref $print;
+			my $highlighted = highlight_prefix(@{$prefixes[$i]})
+			    if @prefixes;
+			if ($ref eq 'ARRAY') {
+				$print = $highlighted || $print->[0];
+			}
+			elsif ($ref eq 'HASH') {
+				my $value = $highlighted || $print->{VALUE};
+				$print = sprintf($status_fmt,
+				    $print->{INDEX},
+				    $print->{FILE},
+				    $value);
+			}
+			else {
+				$print = $highlighted || $print;
 			}
 			printf("%s%2d: %s", $chosen, $i+1, $print);
 			if (($opts->{LIST_FLAT}) &&
Signature · 1 line
1.5.3.6.953.gdffc
#10 lore raw
In reply to Wincent Colaiuta

git-add--interactive.perl sub find_unique { · line 230

Quoted 10 lines · Wincent Colaiuta

+# filters out prefixes which have special meaning to list_and_choose()

git-add--interactive.perl +8 -0
+sub is_valid_prefix {
+	my $prefix = shift;
+	my $valid = (defined $prefix) &&
+	    !($prefix =~ /[\s,]/) && # separators
+	    !($prefix =~ /^-/) &&    # deselection
+	    !($prefix =~ /^\d+/) &&  # selection
+	    ($prefix ne '*');        # "all" wildcard
+}

Doh, that's supposed to be:

return (defined $prefix)...

Not:

my $valid = (defined $prefix)...

It actually works as is, but I had changed the "return" while working on the patch (for debugging) and forgot to change it back afterwards.

And yes, I did proofread the patch before sending it. I just didn't notice the first time around.

Cheers,
Wincent

#11 lore raw

PATCH 1/1 Highlight keyboard shortcuts in git-add--interactive

The user interface provided by the command loop in git-add--interactive gives the impression that subcommands can only be launched by entering an integer identifier from 1 through 8.

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the uniquely matching option.

This patch makes this feature a little more obvious by highlighting the first character of each subcommand (for example "patch" is displayed as "[p]atch").

A new function is added to detect the shortest unique prefix and this is used to decide what to highlight. Highlighting is also applied when choosing files.

In the case where the common prefix may be unreasonably large highlighting is omitted; in this patch the soft limit (above which the highlighting will be omitted for a particular item) is 0 (in other words, there is no soft limit) and the hard limit (above which highlighting will be omitted for all items) is 3, but this can be tweaked.

The actual highlighting is done by the highlight_prefix function, which will enable us to implement ANSI color code-based highlighting (most likely using underline or boldface) in the future.

---

I will go now and crawl under a rock.

 git-add--interactive.perl |  110 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 100 insertions(+), 10 deletions(-)
git-add--interactive.perl +100 -10
index fb1e92a..0e358b5 100755
@@ -44,7 +44,6 @@ my $status_fmt = '%12s %12s %s';
 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
 
 # Returns list of hashes, contents of each of which are:
-# PRINT:	print message
 # VALUE:	pathname
 # BINARY:	is a binary path
 # INDEX:	is index different from HEAD?
@@ -122,8 +121,6 @@ sub list_modified {
 		}
 		push @return, +{
 			VALUE => $_,
-			PRINT => (sprintf $status_fmt,
-				  $it->{INDEX}, $it->{FILE}, $_),
 			%$it,
 		};
 	}
@@ -159,10 +156,95 @@ sub find_unique {
 	return $found;
 }
 
+# inserts string into trie and updates count for each character
+sub update_trie {
+	my ($trie, $string) = @_;
+	foreach (split //, $string) {
+		$trie = $trie->{$_} ||= {COUNT => 0};
+		$trie->{COUNT}++;
+	}
+}
+
+# returns an array of tuples (prefix, remainder)
+sub find_unique_prefixes {
+	my @stuff = @_;
+	my @return = ();
+
+	# any single prefix exceeding the soft limit is omitted
+	# if any prefix exceeds the hard limit all are omitted
+	# 0 indicates no limit
+	my $soft_limit = 0;
+	my $hard_limit = 3;
+
+	# build a trie modelling all possible options
+	my %trie;
+	foreach my $print (@stuff) {
+		if ((ref $print) eq 'ARRAY') {
+			$print = $print->[0];
+		}
+		elsif ((ref $print) eq 'HASH') {
+			$print = $print->{VALUE};
+		}
+		update_trie(\%trie, $print);
+		push @return, $print;
+	}
+
+	# use the trie to find the unique prefixes
+	for (my $i = 0; $i < @return; $i++) {
+		my $ret = $return[$i];
+		my @letters = split //, $ret;
+		my %search = %trie;
+		my ($prefix, $remainder);
+		my $j;
+		for ($j = 0; $j < @letters; $j++) {
+			my $letter = $letters[$j];
+			if ($search{$letter}{COUNT} == 1) {
+				$prefix = substr $ret, 0, $j + 1;
+				$remainder = substr $ret, $j + 1;
+				last;
+			}
+			else {
+				my $prefix = substr $ret, 0, $j;
+				return ()
+				    if ($hard_limit && $j + 1 > $hard_limit);
+			}
+			%search = %{$search{$letter}};
+		}
+		if ($soft_limit && $j + 1 > $soft_limit) {
+			$prefix = undef;
+			$remainder = $ret;
+		}
+		$return[$i] = [$prefix, $remainder];
+	}
+	return @return;
+}
+
+# filters out prefixes which have special meaning to list_and_choose()
+sub is_valid_prefix {
+	my $prefix = shift;
+	return (defined $prefix) &&
+	    !($prefix =~ /[\s,]/) && # separators
+	    !($prefix =~ /^-/) &&    # deselection
+	    !($prefix =~ /^\d+/) &&  # selection
+	    ($prefix ne '*');        # "all" wildcard
+}
+
+# given a prefix/remainder tuple return a string with the prefix highlighted
+# for now use square brackets; later might use ANSI colors (underline, bold)
+sub highlight_prefix {
+	my $prefix = shift;
+	my $remainder = shift;
+	return $remainder unless defined $prefix;
+	return is_valid_prefix($prefix) ?
+	    "[$prefix]$remainder" :
+	    "$prefix$remainder";
+}
+
 sub list_and_choose {
 	my ($opts, @stuff) = @_;
 	my (@chosen, @return);
 	my $i;
+	my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
 
       TOPLOOP:
 	while (1) {
@@ -177,13 +259,21 @@ sub list_and_choose {
 		for ($i = 0; $i < @stuff; $i++) {
 			my $chosen = $chosen[$i] ? '*' : ' ';
 			my $print = $stuff[$i];
-			if (ref $print) {
-				if ((ref $print) eq 'ARRAY') {
-					$print = $print->[0];
-				}
-				else {
-					$print = $print->{PRINT};
-				}
+			my $ref = ref $print;
+			my $highlighted = highlight_prefix(@{$prefixes[$i]})
+			    if @prefixes;
+			if ($ref eq 'ARRAY') {
+				$print = $highlighted || $print->[0];
+			}
+			elsif ($ref eq 'HASH') {
+				my $value = $highlighted || $print->{VALUE};
+				$print = sprintf($status_fmt,
+				    $print->{INDEX},
+				    $print->{FILE},
+				    $value);
+			}
+			else {
+				$print = $highlighted || $print;
 			}
 			printf("%s%2d: %s", $chosen, $i+1, $print);
 			if (($opts->{LIST_FLAT}) &&
Signature · 1 line
1.5.3.6.953.gdffc
#12 lore raw
In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano

When list_and_choose() function is letting you choose more than one items, its prompt becomes ">> ", instead of "> " that is used for a singleton choice. To that prompt, you can say "3-7" (Add these 5 items
to the choice), "*" (I want all of them), "-2-4" (exclude 2 and 3 and 4
from the set I have chosen so far). These are also "hidden", and need to be documented, but that would be a separate patch.

I was just about to prepare some documentation for this when I saw that it is already documented, and by none other than you, Junio! (see 6a5ad23d).

Unless by "documentation" you meant to somehow expose these in the interface at runtime... something like this? (applied on top of the patch I just sent to the list):

git-add--interactive.perl +2 -1
index 32fb9ea..e7b07ee 100755
@@ -237,7 +237,8 @@ sub is_valid_prefix {
  	    !($prefix =~ /[\s,]/) && # separators
  	    !($prefix =~ /^-/) &&    # deselection
  	    !($prefix =~ /^\d+/) &&  # selection
-	    ($prefix ne '*');        # "all" wildcard
+	    ($prefix ne '*') &&      # "all" wildcard
+	    ($prefix ne '?');        # prompt help
  }
 
  # given a prefix/remainder tuple return a string with the prefix  

highlighted
@@ -308,7 +309,7 @@ sub list_and_choose {
print "> ";
}
else {
- print ">> ";
+ print " (?)>> ";
}
my $line = <STDIN>;
if (!$line) {
@@ -318,6 +319,10 @@ sub list_and_choose {
}
chomp $line;
last if $line eq '';
+ if ($line eq '?' && !$opts->{SINGLETON}) {
+ prompt_help_cmd();
+ next TOPLOOP;
+ }
for my $choice (split(/[\s,]+/, $line)) {
my $choose = 1;
my ($bottom, $top);
@@ -363,6 +368,19 @@ sub list_and_choose {
return @return;
}

+sub prompt_help_cmd {
+ print <<\EOF ;
+Prompt help:

+1          - select a single item
+3-5        - select a range of items
+2-3,6-9    - select multiple ranges
+foo        - select item based on unique prefix
+-...       - unselect specified items
+*          - choose all items
+           - (empty) finish selecting

+EOF
+}
+
sub status_cmd {
list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
list_modified());

Cheers,
Wincent

#13 lore raw
Quoted 3 lines · Wincent Colaiuta

Unless by "documentation" you meant to somehow expose these in the interface at runtime... something like this? (applied on top of the patch I just sent to the list):

I did not recall (and was too lazy to check) if they were documented already, but as you suggest, I think letting people type ? at the prompt to get a help is always a good idea. So, instead of doing this part:

Review sub list_and_choose { · line 312

Quoted 6 lines
(quoted diff) +1 -1
@@ -308,7 +309,7 @@ sub list_and_choose {
  			print "> ";
  		}
  		else {
-			print ">> ";
+			print " (?)>> ";

I'd prefer accepting '?' as a valid "help me" input and showing appropriate help for _both_ singleton select and multiple select, without mentioning " (?)". For this, your prompt_help_cmd needs to be enhanced to limit the help to singleton case, though.

#14 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Wincent Colaiuta

Unless by "documentation" you meant to somehow expose these in the interface at runtime... something like this? (applied on top of the patch I just sent to the list):

I did not recall (and was too lazy to check) if they were documented already, but as you suggest, I think letting people type ? at the prompt
to get a help is always a good idea. So, instead of doing this part:

Quoted 11 lines

Review sub list_and_choose { · line 312

Quoted 6 lines
(quoted diff) +1 -1
@@ -308,7 +309,7 @@ sub list_and_choose {
 			print "> ";
 		}
 		else {
-			print ">> ";
+			print " (?)>> ";

I'd prefer accepting '?' as a valid "help me" input and showing appropriate help for _both_ singleton select and multiple select, without mentioning " (?)". For this, your prompt_help_cmd needs to be enhanced to limit the help to singleton case, though.

That's actually the way I did it the first time, but then decided that the singleton prompt help had so little to say that I doubted about including it. Something like this, once again on top of the patch I posted yesterday ("Fixes for automatic prefix highlighting"):

git-add--interactive.perl +2 -1
index 32fb9ea..335c2c6 100755
@@ -237,7 +237,8 @@ sub is_valid_prefix {
  	    !($prefix =~ /[\s,]/) && # separators
  	    !($prefix =~ /^-/) &&    # deselection
  	    !($prefix =~ /^\d+/) &&  # selection
-	    ($prefix ne '*');        # "all" wildcard
+	    ($prefix ne '*') &&      # "all" wildcard
+	    ($prefix ne '?');        # prompt help
  }
 
  # given a prefix/remainder tuple return a string with the prefix  

highlighted
@@ -318,6 +319,12 @@ sub list_and_choose {
}
chomp $line;
last if $line eq '';
+ if ($line eq '?') {
+ $opts->{SINGLETON} ?
+ singleton_prompt_help_cmd() :
+ prompt_help_cmd();
+ next TOPLOOP;
+ }
for my $choice (split(/[\s,]+/, $line)) {
my $choose = 1;
my ($bottom, $top);
@@ -363,6 +370,28 @@ sub list_and_choose {
return @return;
}

+sub singleton_prompt_help_cmd {
+ print <<\EOF ;
+Prompt help:

+1          - select a numbered item
+foo        - select item based on unique prefix
+           - (empty) select nothing

+EOF
+}
+
+sub prompt_help_cmd {
+ print <<\EOF ;
+Prompt help:

+1          - select a single item
+3-5        - select a range of items
+2-3,6-9    - select multiple ranges
+foo        - select item based on unique prefix
+-...       - unselect specified items
+*          - choose all items
+           - (empty) finish selecting

+EOF
+}
+
sub status_cmd {
list_and_choose({ LIST_ONLY => 1, HEADER => $status_head },
list_modified());

Cheers,
Wincent

#15 lore raw
In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano
Quoted 2 lines · Wincent Colaiuta

A "hidden" feature is that any string can be entered, and an anchored regex search is used to find the first matching option.

I'd run s/the first/the uniquely/ here.

Quoted 8 lines

When list_and_choose() function is letting you choose more than one items, its prompt becomes ">> ", instead of "> " that is used for a singleton choice. To that prompt, you can say "3-7" (Add these 5 items
to the choice), "*" (I want all of them), "-2-4" (exclude 2 and 3 and 4
from the set I have chosen so far). These are also "hidden", and need to be documented, but that would be a separate patch.

Agreed that it belongs in a separate patch.

But I'm glad you brought this up as it reminds me of the need to watch out for those characters which have special meaning for list_and_choose().

Quoted 6 lines

I'd rewrite the last line to:

	return (defined $prefix) ? "[$prefix]$remainder" : $remainder;

just in case the unique prefix is "0". Otherwise you would lose the first letter from "00ReadMe" and show remainder "0ReadMe" alone.

Excellent catch. Crazy old perl; I didn't realize that "0" (the string, not the number) would evaluate to false.

Will send a separate mail with a revised, squashed patch with these changes:

- "s/the first/the uniquely/" in the commit message as you suggest

- filter out prefixes which contain characters with special meaning for list_and_choose()

- check "defined $prefix" rather than just "$prefix"

- also fixes a problem discovered while playing with this; it didn't play nicely with untracked files

Cheers,
Wincent