@matheusbsbs / matheus-bagliano / commits / b4c61ed6d3
Fork 0

Color support for "git-add -i"

This is mostly lifted from earlier series by Dan Zwell, but updated to use "git config --get-color" and "git config --get-colorbool" to make it simpler and more consistent with commands written in C. A new configuration color.interactive variable is like color.diff and color.status, and controls if "git-add -i" uses color. A set of configuration variables, color.interactive.<slot>, are used to define what color is used for the prompt, header, and help text. For perl scripts, Git.pm provides $repo->get_color() method, which takes the slot name and the default color, and returns the terminal escape sequence to color the output text. $repo->get_colorbool() method can be used to check if color is set to be used for a given operation. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 5, 2007 at 00:50 UTC b4c61ed6d35ea00494f953a27abbc902617972ec
3 files changed +146 -20
Documentation/config.txt
+12
@@ -391,6 +391,18 @@ color.diff.<slot>::
391 whitespace). The values of these variables may be specified as
392 in color.branch.<slot>.
393
394 +color.interactive::
395 + When set to `always`, always use colors in `git add --interactive`.
396 + When false (or `never`), never. When set to `true` or `auto`, use
397 + colors only when the output is to the terminal. Defaults to false.
398 +
399 +color.interactive.<slot>::
400 + Use customized color for `git add --interactive`
401 + output. `<slot>` may be `prompt`, `header`, or `help`, for
402 + three distinct types of normal output from interactive
403 + programs. The values of these variables may be specified as
404 + in color.branch.<slot>.
405 +
406 color.pager::
407 A boolean to enable/disable colored output when the pager is in
408 use (default is true).
git-add--interactive.perl
+99 -20
@@ -1,6 +1,55 @@
1 #!/usr/bin/perl -w
2
3 use strict;
4 +use Git;
5 +
6 +# Prompt colors:
7 +my ($prompt_color, $header_color, $help_color, $normal_color);
8 +# Diff colors:
9 +my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_color);
10 +
11 +my ($use_color, $diff_use_color);
12 +my $repo = Git->repository();
13 +
14 +$use_color = $repo->get_colorbool('color.interactive');
15 +
16 +if ($use_color) {
17 + # Set interactive colors:
18 +
19 + # Grab the 3 main colors in git color string format, with sane
20 + # (visible) defaults:
21 + $prompt_color = $repo->get_color("color.interactive.prompt", "bold blue");
22 + $header_color = $repo->get_color("color.interactive.header", "bold");
23 + $help_color = $repo->get_color("color.interactive.help", "red bold");
24 + $normal_color = $repo->get_color("", "reset");
25 +
26 + # Do we also set diff colors?
27 + $diff_use_color = $repo->get_colorbool('color.diff');
28 + if ($diff_use_color) {
29 + $new_color = $repo->get_color("color.diff.new", "green");
30 + $old_color = $repo->get_color("color.diff.old", "red");
31 + $fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
32 + $metainfo_color = $repo->get_color("color.diff.meta", "bold");
33 + $whitespace_color = $repo->get_color("color.diff.whitespace", "normal red");
34 + }
35 +}
36 +
37 +sub colored {
38 + my $color = shift;
39 + my $string = join("", @_);
40 +
41 + if ($use_color) {
42 + # Put a color code at the beginning of each line, a reset at the end
43 + # color after newlines that are not at the end of the string
44 + $string =~ s/(\n+)(.)/$1$color$2/g;
45 + # reset before newlines
46 + $string =~ s/(\n+)/$normal_color$1/g;
47 + # codes at beginning and end (if necessary):
48 + $string =~ s/^/$color/;
49 + $string =~ s/$/$normal_color/ unless $string =~ /\n$/;
50 + }
51 + return $string;
52 +}
53
54 # command line options
55 my $patch_mode;
@@ -246,10 +295,20 @@ sub is_valid_prefix {
295 sub highlight_prefix {
296 my $prefix = shift;
297 my $remainder = shift;
249 - return $remainder unless defined $prefix;
250 - return is_valid_prefix($prefix) ?
251 - "[$prefix]$remainder" :
252 - "$prefix$remainder";
298 +
299 + if (!defined $prefix) {
300 + return $remainder;
301 + }
302 +
303 + if (!is_valid_prefix($prefix)) {
304 + return "$prefix$remainder";
305 + }
306 +
307 + if (!$use_color) {
308 + return "[$prefix]$remainder";
309 + }
310 +
311 + return "$prompt_color$prefix$normal_color$remainder";
312 }
313
314 sub list_and_choose {
@@ -266,7 +325,7 @@ sub list_and_choose {
325 if (!$opts->{LIST_FLAT}) {
326 print " ";
327 }
269 - print "$opts->{HEADER}\n";
328 + print colored $header_color, "$opts->{HEADER}\n";
329 }
330 for ($i = 0; $i < @stuff; $i++) {
331 my $chosen = $chosen[$i] ? '*' : ' ';
@@ -304,7 +363,7 @@ sub list_and_choose {
363
364 return if ($opts->{LIST_ONLY});
365
307 - print $opts->{PROMPT};
366 + print colored $prompt_color, $opts->{PROMPT};
367 if ($opts->{SINGLETON}) {
368 print "> ";
369 }
@@ -371,7 +430,7 @@ sub list_and_choose {
430 }
431
432 sub singleton_prompt_help_cmd {
374 - print <<\EOF ;
433 + print colored $help_color, <<\EOF ;
434 Prompt help:
435 1 - select a numbered item
436 foo - select item based on unique prefix
@@ -380,7 +439,7 @@ EOF
439 }
440
441 sub prompt_help_cmd {
383 - print <<\EOF ;
442 + print colored $help_color, <<\EOF ;
443 Prompt help:
444 1 - select a single item
445 3-5 - select a range of items
@@ -477,6 +536,31 @@ sub parse_diff {
536 return @hunk;
537 }
538
539 +sub colored_diff_hunk {
540 + my ($text) = @_;
541 + # return the text, so that it can be passed to print()
542 + my @ret;
543 + for (@$text) {
544 + if (!$diff_use_color) {
545 + push @ret, $_;
546 + next;
547 + }
548 +
549 + if (/^\+/) {
550 + push @ret, colored($new_color, $_);
551 + } elsif (/^\-/) {
552 + push @ret, colored($old_color, $_);
553 + } elsif (/^\@/) {
554 + push @ret, colored($fraginfo_color, $_);
555 + } elsif (/^ /) {
556 + push @ret, colored($normal_color, $_);
557 + } else {
558 + push @ret, colored($metainfo_color, $_);
559 + }
560 + }
561 + return @ret;
562 +}
563 +
564 sub hunk_splittable {
565 my ($text) = @_;
566
@@ -671,7 +755,7 @@ sub coalesce_overlapping_hunks {
755 }
756
757 sub help_patch_cmd {
674 - print <<\EOF ;
758 + print colored $help_color, <<\EOF ;
759 y - stage this hunk
760 n - do not stage this hunk
761 a - stage this and all the remaining hunks in the file
@@ -710,9 +794,7 @@ sub patch_update_file {
794 my ($ix, $num);
795 my $path = shift;
796 my ($head, @hunk) = parse_diff($path);
713 - for (@{$head->{TEXT}}) {
714 - print;
715 - }
797 + print colored_diff_hunk($head->{TEXT});
798 $num = scalar @hunk;
799 $ix = 0;
800
@@ -754,10 +836,8 @@ sub patch_update_file {
836 if (hunk_splittable($hunk[$ix]{TEXT})) {
837 $other .= '/s';
838 }
757 - for (@{$hunk[$ix]{TEXT}}) {
758 - print;
759 - }
760 - print "Stage this hunk [y/n/a/d$other/?]? ";
839 + print colored_diff_hunk($hunk[$ix]{TEXT});
840 + print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
841 my $line = <STDIN>;
842 if ($line) {
843 if ($line =~ /^y/i) {
@@ -811,7 +891,7 @@ sub patch_update_file {
891 elsif ($other =~ /s/ && $line =~ /^s/) {
892 my @split = split_hunk($hunk[$ix]{TEXT});
893 if (1 < @split) {
814 - print "Split into ",
894 + print colored $header_color, "Split into ",
895 scalar(@split), " hunks.\n";
896 }
897 splice(@hunk, $ix, 1,
@@ -894,8 +974,7 @@ sub diff_cmd {
974 HEADER => $status_head, },
975 @mods);
976 return if (!@them);
897 - system(qw(git diff-index -p --cached HEAD --),
898 - map { $_->{VALUE} } @them);
977 + system(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);
978 }
979
980 sub quit_cmd {
@@ -904,7 +983,7 @@ sub quit_cmd {
983 }
984
985 sub help_cmd {
907 - print <<\EOF ;
986 + print colored $help_color, <<\EOF ;
987 status - show paths with changes
988 update - add working tree state to the staged set of changes
989 revert - revert staged set of changes back to the HEAD version
perl/Git.pm
+35
@@ -581,6 +581,41 @@ sub config_int {
581 };
582 }
583
584 +=item get_colorbool ( NAME )
585 +
586 +Finds if color should be used for NAMEd operation from the configuration,
587 +and returns boolean (true for "use color", false for "do not use color").
588 +
589 +=cut
590 +
591 +sub get_colorbool {
592 + my ($self, $var) = @_;
593 + my $stdout_to_tty = (-t STDOUT) ? "true" : "false";
594 + my $use_color = $self->command_oneline('config', '--get-colorbool',
595 + $var, $stdout_to_tty);
596 + return ($use_color eq 'true');
597 +}
598 +
599 +=item get_color ( SLOT, COLOR )
600 +
601 +Finds color for SLOT from the configuration, while defaulting to COLOR,
602 +and returns the ANSI color escape sequence:
603 +
604 + print $repo->get_color("color.interactive.prompt", "underline blue white");
605 + print "some text";
606 + print $repo->get_color("", "normal");
607 +
608 +=cut
609 +
610 +sub get_color {
611 + my ($self, $slot, $default) = @_;
612 + my $color = $self->command_oneline('config', '--get-color', $slot, $default);
613 + if (!defined $color) {
614 + $color = "";
615 + }
616 + return $color;
617 +}
618 +
619 =item ident ( TYPE | IDENTSTR )
620
621 =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )