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>
This commit is contained in:
Wincent Colaiuta
2007-12-02 14:44:11 +01:00
committed by Junio C Hamano
parent 14cb50382c
commit 633209898b

View File

@ -193,7 +193,7 @@ sub find_unique_prefixes {
if ((ref $print) eq 'ARRAY') { if ((ref $print) eq 'ARRAY') {
$print = $print->[0]; $print = $print->[0];
} }
else { elsif ((ref $print) eq 'HASH') {
$print = $print->{VALUE}; $print = $print->{VALUE};
} }
update_trie(\%trie, $print); update_trie(\%trie, $print);
@ -230,12 +230,25 @@ sub find_unique_prefixes {
return @return; 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 # given a prefix/remainder tuple return a string with the prefix highlighted
# for now use square brackets; later might use ANSI colors (underline, bold) # for now use square brackets; later might use ANSI colors (underline, bold)
sub highlight_prefix { sub highlight_prefix {
my $prefix = shift; my $prefix = shift;
my $remainder = shift; my $remainder = shift;
return (defined $prefix) ? "[$prefix]$remainder" : $remainder; return $remainder unless defined $prefix;
return is_valid_prefix($prefix) ?
"[$prefix]$remainder" :
"$prefix$remainder";
} }
sub list_and_choose { sub list_and_choose {
@ -257,21 +270,21 @@ sub list_and_choose {
for ($i = 0; $i < @stuff; $i++) { for ($i = 0; $i < @stuff; $i++) {
my $chosen = $chosen[$i] ? '*' : ' '; my $chosen = $chosen[$i] ? '*' : ' ';
my $print = $stuff[$i]; my $print = $stuff[$i];
if (ref $print) { my $ref = ref $print;
if ((ref $print) eq 'ARRAY') { my $highlighted = highlight_prefix(@{$prefixes[$i]})
$print = @prefixes ? if @prefixes;
highlight_prefix(@{$prefixes[$i]}) : if ($ref eq 'ARRAY') {
$print->[0]; $print = $highlighted || $print->[0];
} }
else { elsif ($ref eq 'HASH') {
my $value = @prefixes ? my $value = $highlighted || $print->{VALUE};
highlight_prefix(@{$prefixes[$i]}) : $print = sprintf($status_fmt,
$print->{VALUE}; $print->{INDEX},
$print = sprintf($status_fmt, $print->{FILE},
$print->{INDEX}, $value);
$print->{FILE}, }
$value); else {
} $print = $highlighted || $print;
} }
printf("%s%2d: %s", $chosen, $i+1, $print); printf("%s%2d: %s", $chosen, $i+1, $print);
if (($opts->{LIST_FLAT}) && if (($opts->{LIST_FLAT}) &&