@matheusbsbs / matheus-bagliano / commits / 14cb50382c
Fork 0

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 a regex search anchored at the beginning of the string 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. Signed-off-by: Wincent Colaiuta <win@wincent.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Wincent Colaiuta committed Nov 29, 2007 at 13:00 UTC 14cb50382c9027ca3b8cf8dbc0a26503a40f50c5
1 file changed +82 -5
git-add--interactive.perl
+82 -5
@@ -47,7 +47,6 @@ my $status_fmt = '%12s %12s %s';
47 my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
48
49 # Returns list of hashes, contents of each of which are:
50 -# PRINT: print message
50 # VALUE: pathname
51 # BINARY: is a binary path
52 # INDEX: is index different from HEAD?
@@ -133,8 +132,6 @@ sub list_modified {
132 }
133 push @return, +{
134 VALUE => $_,
136 - PRINT => (sprintf $status_fmt,
137 - $it->{INDEX}, $it->{FILE}, $_),
135 %$it,
136 };
137 }
@@ -170,10 +167,82 @@ sub find_unique {
167 return $found;
168 }
169
170 +# inserts string into trie and updates count for each character
171 +sub update_trie {
172 + my ($trie, $string) = @_;
173 + foreach (split //, $string) {
174 + $trie = $trie->{$_} ||= {COUNT => 0};
175 + $trie->{COUNT}++;
176 + }
177 +}
178 +
179 +# returns an array of tuples (prefix, remainder)
180 +sub find_unique_prefixes {
181 + my @stuff = @_;
182 + my @return = ();
183 +
184 + # any single prefix exceeding the soft limit is omitted
185 + # if any prefix exceeds the hard limit all are omitted
186 + # 0 indicates no limit
187 + my $soft_limit = 0;
188 + my $hard_limit = 3;
189 +
190 + # build a trie modelling all possible options
191 + my %trie;
192 + foreach my $print (@stuff) {
193 + if ((ref $print) eq 'ARRAY') {
194 + $print = $print->[0];
195 + }
196 + else {
197 + $print = $print->{VALUE};
198 + }
199 + update_trie(\%trie, $print);
200 + push @return, $print;
201 + }
202 +
203 + # use the trie to find the unique prefixes
204 + for (my $i = 0; $i < @return; $i++) {
205 + my $ret = $return[$i];
206 + my @letters = split //, $ret;
207 + my %search = %trie;
208 + my ($prefix, $remainder);
209 + my $j;
210 + for ($j = 0; $j < @letters; $j++) {
211 + my $letter = $letters[$j];
212 + if ($search{$letter}{COUNT} == 1) {
213 + $prefix = substr $ret, 0, $j + 1;
214 + $remainder = substr $ret, $j + 1;
215 + last;
216 + }
217 + else {
218 + my $prefix = substr $ret, 0, $j;
219 + return ()
220 + if ($hard_limit && $j + 1 > $hard_limit);
221 + }
222 + %search = %{$search{$letter}};
223 + }
224 + if ($soft_limit && $j + 1 > $soft_limit) {
225 + $prefix = undef;
226 + $remainder = $ret;
227 + }
228 + $return[$i] = [$prefix, $remainder];
229 + }
230 + return @return;
231 +}
232 +
233 +# given a prefix/remainder tuple return a string with the prefix highlighted
234 +# for now use square brackets; later might use ANSI colors (underline, bold)
235 +sub highlight_prefix {
236 + my $prefix = shift;
237 + my $remainder = shift;
238 + return (defined $prefix) ? "[$prefix]$remainder" : $remainder;
239 +}
240 +
241 sub list_and_choose {
242 my ($opts, @stuff) = @_;
243 my (@chosen, @return);
244 my $i;
245 + my @prefixes = find_unique_prefixes(@stuff) unless $opts->{LIST_ONLY};
246
247 TOPLOOP:
248 while (1) {
@@ -190,10 +259,18 @@ sub list_and_choose {
259 my $print = $stuff[$i];
260 if (ref $print) {
261 if ((ref $print) eq 'ARRAY') {
193 - $print = $print->[0];
262 + $print = @prefixes ?
263 + highlight_prefix(@{$prefixes[$i]}) :
264 + $print->[0];
265 }
266 else {
196 - $print = $print->{PRINT};
267 + my $value = @prefixes ?
268 + highlight_prefix(@{$prefixes[$i]}) :
269 + $print->{VALUE};
270 + $print = sprintf($status_fmt,
271 + $print->{INDEX},
272 + $print->{FILE},
273 + $value);
274 }
275 }
276 printf("%s%2d: %s", $chosen, $i+1, $print);