add -i: allow prefix highlighting for "Add untracked" as well.
These changes make the automatic prefix highlighting work with the "Add untracked" subcommand in git-add--interactive by explicitly handling arrays, hashes and strings internally (previously only arrays and hashes were handled). In addition, prefixes which have special meaning for list_and_choose (things like "*" for "all" and "-" for "deselect) are explicitly excluded (highlighting these prefixes would be misleading). Signed-off-by: Wincent Colaiuta <win@wincent.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Wincent Colaiuta committed
Dec 2, 2007 at 14:44 UTC
633209898b71df4cf4d9296f224294f9fe252f66
1 file changed
+30
-17
git-add--interactive.perl
+30
-17
@@ -193,7 +193,7 @@ sub find_unique_prefixes {
193
if ((ref $print) eq 'ARRAY') {
194
$print = $print->[0];
195
}
196
- else {
196
+ elsif ((ref $print) eq 'HASH') {
197
$print = $print->{VALUE};
198
}
199
update_trie(\%trie, $print);
@@ -230,12 +230,25 @@ sub find_unique_prefixes {
230
return @return;
231
}
232
233
+# filters out prefixes which have special meaning to list_and_choose()
234
+sub is_valid_prefix {
235
+ my $prefix = shift;
236
+ return (defined $prefix) &&
237
+ !($prefix =~ /[\s,]/) && # separators
238
+ !($prefix =~ /^-/) && # deselection
239
+ !($prefix =~ /^\d+/) && # selection
240
+ ($prefix ne '*'); # "all" wildcard
241
+}
242
+
243
# given a prefix/remainder tuple return a string with the prefix highlighted
244
# for now use square brackets; later might use ANSI colors (underline, bold)
245
sub highlight_prefix {
246
my $prefix = shift;
247
my $remainder = shift;
238
- return (defined $prefix) ? "[$prefix]$remainder" : $remainder;
248
+ return $remainder unless defined $prefix;
249
+ return is_valid_prefix($prefix) ?
250
+ "[$prefix]$remainder" :
251
+ "$prefix$remainder";
252
}
253
254
sub list_and_choose {
@@ -257,21 +270,21 @@ sub list_and_choose {
270
for ($i = 0; $i < @stuff; $i++) {
271
my $chosen = $chosen[$i] ? '*' : ' ';
272
my $print = $stuff[$i];
260
- if (ref $print) {
261
- if ((ref $print) eq 'ARRAY') {
262
- $print = @prefixes ?
263
- highlight_prefix(@{$prefixes[$i]}) :
264
- $print->[0];
265
- }
266
- else {
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
- }
273
+ my $ref = ref $print;
274
+ my $highlighted = highlight_prefix(@{$prefixes[$i]})
275
+ if @prefixes;
276
+ if ($ref eq 'ARRAY') {
277
+ $print = $highlighted || $print->[0];
278
+ }
279
+ elsif ($ref eq 'HASH') {
280
+ my $value = $highlighted || $print->{VALUE};
281
+ $print = sprintf($status_fmt,
282
+ $print->{INDEX},
283
+ $print->{FILE},
284
+ $value);
285
+ }
286
+ else {
287
+ $print = $highlighted || $print;
288
}
289
printf("%s%2d: %s", $chosen, $i+1, $print);
290
if (($opts->{LIST_FLAT}) &&