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?
132
}
133
push @return, +{
134
VALUE => $_,
136
- PRINT => (sprintf $status_fmt,
137
- $it->{INDEX}, $it->{FILE}, $_),
135
%$it,
136
};
137
}
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) {
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);