git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Color support added to git-add--interactive.

From
Wincent Colaiuta <win@wincent.com>
Date
Oct 13, 2007, 17:14 UTC
Message-ID
<6E1CBEF5-C2EA-447A-9FED-1423A17C2D19@wincent.com>
In-Reply-To
<4710F47D.2070306@gmx.ch>
El 13/10/2007, a las 18:38, Jean-Luc Herren escribió:
Show 12 quoted lines
> Here are my two cents.
>
> Wincent Colaiuta wrote:
>> +sub print_ansi_color($$;$) {
>> +    my $color = shift;
>> +    my $string = shift;
>> +    my $trailer = shift;
>
> None of the other subs in this file have a prototype, so for
> consistency I'd suggest to not add it on this function either.
> However maybe a patch that adds it to all subs would be welcome.
> (I wouldn't see the necessity though.)

Yes, I saw that the other functions didn't use prototypes and I agree that consistency would be a good thing. I liked the idea of it in this case because it makes explicit the fact that the function takes two params plus a third, optional one. So definitely not necessary, but a preference of mine. In any case, a change to all the other functions is a question for a separate patch.

Show 7 quoted lines
> And the common way of getting the arguments is reading @_ (see all
> other subs in the file).  So maybe instead write:
>
> [...]
> sub print_ansi_color {
> 	my ($color, $string, $trailer) = @_;
> [...]

Yes, I actually did write it that way first but then my doubts about Perl made me write it the longer way; but if they are equivalent then I prefer the shorter way.

Show 7 quoted lines
>> +    if ($use_color) {
>> +        printf '%s%s%s', Term::ANSIColor::color($color), $string,
>> +            Term::ANSIColor::color('clear');
>> +    } else {
>
> Why use printf when you could directly use print here?  It's only
> used for concatenating.
True. I had may brain in the C-world.
Show 11 quoted lines
>> +    if ($trailer) {
>> +        print $trailer;
>> +    }
>
> This will fail to print $trailer when $trailer happens to be a
> string that evaluates to false in bool context, like '0'.  Write
> this as:
>
> 	if (defined $trailer) {
> 	    print $trailer;
> 	}

Again, I actually wrote it that way the first time, and then changed it, this time because I thought they were the same. Like I said, not a perl hacker.

> IMHO, parsing the output of 'git diff-files --color' is a very bad
> idea and it makes all regexes uglier and more difficult to read.
> You're much better off recolorizing it yourself, which makes it a
> more localized change.

You're probably right, although it is also duplicating the work that's already done elsewhere. In general I favor making the simplest change that would work, and tweaking a few of the regexes did look simpler than re-implementing the colorization logic.

But the approach you suggest might be more robust, perhaps, seeing as there's not much to the diff output. As far as I can tell there are really only five or six different things to look for, and they'd be fairly easy to catch:

- lines beginning with "@@ " (hunk headers)
- lines beginning with "+" (insertions)
- lines beginning with "-" (deletions)
- lines beginning with " " (context lines, no color)
- lines beginning with "\" (things like "\ No newline at end of  
file", again, no color)
- everything else; ie. the diff header stuff (eg "diff --git a/foo b/ 
foo")

The only special cases seem to be the "+++" and "---" lines in the header, which look like insertions and deletions when they're not.

Trickier would be the highlighting of dubious whitespace, and that's when it starts to sound like re-inventing the wheel and duplicating the logic for the detection that's defined elsewhere (possibly in diff-lib.c? haven't found the exact spot yet).

> Especially, I don't think that you have
> any guarantee that escape sequences won't ever contain the
> characters '+', '-' or ' ' (space)

Yes, that was one of the things I didn't like about the sloppy regexes. I couldn't really make them any stricter though because I wasn't confident about the range of possible characters that might be included in the escape sequences.

> Finally -- and this might be just my eyes -- blue is a very nice
> color, but it looks a bit too dark on black background.  Maybe
> choose a default color that looks reasonable on black *and* white
> background.

Yeah, well I didn't choose the colours and I didn't really want to get into it. Before being considered for inclusion a patch like this would need to tap in to the existing config settings for color.diff and color.diff.<slot> anyway...

Wincent
Previous: Jean-Luc HerrenNext: Andreas Ericsson
Message 7 of 86 in “Color support added to git-add--interactive.”
  1. Color support added to git-add--interactive.Dan Zwell, Oct 13, 2007
  2. Jeff KingOct 13, 2007
  3. Frank LichtenheldOct 13, 2007
  4. Johannes SchindelinOct 13, 2007
  5. Wincent ColaiutaOct 13, 2007
  6. Jean-Luc HerrenOct 13, 2007
  7. Wincent ColaiutaOct 13, 2007
  8. Andreas EricssonOct 13, 2007
  9. Johannes SchindelinOct 13, 2007
  10. Jeff KingOct 13, 2007
  11. Jeff KingOct 13, 2007
  12. Dan ZwellOct 13, 2007
  13. Wincent ColaiutaOct 13, 2007
  14. Dan ZOct 13, 2007
  15. Jean-Luc HerrenOct 13, 2007
  16. Jeff KingOct 15, 2007
  17. Dan ZwellOct 17, 2007
  18. Shawn O. PearceOct 17, 2007
  19. Dan ZwellOct 17, 2007
  20. Shawn O. PearceOct 17, 2007
  21. 1/2 Added basic color support to git add --interactiveDan Zwell, Oct 22, 2007
  22. Dan ZwellOct 23, 2007
  23. Let git-add--interactive read "git colors" from git-configDan Zwell, Oct 23, 2007
  24. Jeff KingOct 23, 2007
  25. Shawn O. PearceOct 23, 2007
  26. Jeff KingOct 23, 2007
  27. Wincent ColaiutaOct 23, 2007
  28. Jeff KingOct 23, 2007
  29. Wincent ColaiutaOct 23, 2007
  30. 2/2 Let git-add--interactive read colors from git-configDan Zwell, Oct 22, 2007
  31. Jeff KingOct 23, 2007
  32. Dan ZwellOct 23, 2007
  33. 1/2 Added basic color support to git add --interactiveDan Zwell, Nov 3, 2007
  34. Jeff KingNov 4, 2007
  35. Junio C HamanoNov 4, 2007
  36. Jeff KingNov 4, 2007
  37. 0/3 Adding colors to git-add--interactiveDan Zwell, Nov 11, 2007
  38. Jeff KingNov 11, 2007
  39. Junio C HamanoNov 11, 2007
  40. Dan ZwellNov 11, 2007
  41. 0/5 Colors for git-add--interactiveDan Zwell, Nov 22, 2007
  42. Jeff KingNov 22, 2007
  43. Junio C HamanoNov 22, 2007
  44. 1/5 Added basic color support to git add --interactiveDan Zwell, Nov 22, 2007
  45. 2/5 Don't return 'undef' in case called in a vector context.Dan Zwell, Nov 22, 2007
  46. Jeff KingNov 22, 2007
  47. Junio C HamanoNov 22, 2007
  48. Dan ZwellNov 23, 2007
  49. 3/5 Added config_default($key, $default) to Git.pmDan Zwell, Nov 22, 2007
  50. Jeff KingNov 22, 2007
  51. 4/5 Let git-add--interactive read colors from configurationDan Zwell, Nov 22, 2007
  52. Jeff KingNov 22, 2007
  53. Junio C HamanoNov 22, 2007
  54. Jeff KingNov 22, 2007
  55. Dan ZwellNov 23, 2007
  56. Jeff KingNov 23, 2007
  57. Junio C HamanoNov 23, 2007
  58. 5/5 Added diff hunk coloring to git-add--interactiveDan Zwell, Nov 22, 2007
  59. Jeff KingNov 22, 2007
  60. Junio C HamanoNov 22, 2007
  61. Jeff KingNov 23, 2007
  62. Junio C HamanoNov 22, 2007
  63. 1/3 Added basic color support to git add --interactiveDan Zwell, Nov 11, 2007
  64. 2/3 Let git-add--interactive read colors from .gitconfigDan Zwell, Nov 11, 2007
  65. 3/3 Added diff hunk coloring to git-add--interactiveDan Zwell, Nov 11, 2007
  66. Junio C HamanoNov 11, 2007
  67. 0/3 Adding colors to git-add--interactiveDan Zwell, Nov 11, 2007
  68. Subject: [PATCH 1/3] Added basic color support to git add --interactiveDan Zwell, Nov 11, 2007
  69. Junio C HamanoNov 11, 2007
  70. Subject: [PATCH 2/3] Let git-add--interactive read colors from .gitconfigDan Zwell, Nov 11, 2007
  71. Junio C HamanoNov 11, 2007
  72. Junio C HamanoNov 11, 2007
  73. Dan ZwellNov 13, 2007
  74. Junio C HamanoNov 13, 2007
  75. Dan ZwellNov 13, 2007
  76. Jeff KingNov 13, 2007
  77. Junio C HamanoNov 13, 2007
  78. Dan ZwellNov 13, 2007
  79. Jakub NarebskiNov 13, 2007
  80. 2/2 Let git-add--interactive read colors from .gitconfigDan Zwell, Nov 3, 2007
  81. Junio C HamanoNov 3, 2007
  82. Dan ZwellNov 3, 2007
  83. Junio C HamanoNov 3, 2007
  84. Jeff KingOct 15, 2007
  85. Tom TobinOct 13, 2007
  86. Tom TobinOct 13, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.