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

[PATCH 2/2] Let git-add--interactive read colors from git-config

From
DZDan Zwell <dzwell@zwell.net>
Date
Oct 22, 2007, 21:40 UTC
Message-ID
<20071022164048.71a3dceb@danzwell.com>
In-Reply-To
<20071017015152.GN13801@spearce.org>

Colors are specified in color.interactive.{prompt,header,help}. They are specified as git color strings as described in the documentation, then parsed into perl color strings (slightly different). Ugly but visible defaults are still used.

Signed-off-by: Dan Zwell <dzwell@zwell.net>
---
Note: the code to parse git-style color strings to perl-style color
strings should eventually be added to Git.pm so that other (perl)
parts of git can be configured to read colors from .gitconfig in
a nicer way. A git-style string is "ul red black", while perl 
likes strings like "underline red on_black".
 Documentation/config.txt  |    7 ++++
 git-add--interactive.perl |   76
++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79
insertions(+), 4 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index c795a35..75a976a 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -387,6 +387,13 @@ color.interactive::
 	`auto`, use colors only when the output is to the
 	terminal. Defaults to false.
 
+color.interactive.<slot>::
+	Use customized color for `git add --interactive`
+	output. `<slot>` may be `prompt`, `header`, or `help`, for
+	three distinct types of normal output from interactive
+	programs.  The values of these variables may be specified as
+	in color.branch.<slot>.
+
 color.pager::
 	A boolean to enable/disable colored output when the pager is in
 	use (default is true).
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index c66ed4d..d85ec92 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -6,10 +6,78 @@ my ($use_color, $prompt_color, $header_color, $help_color);
 my $color_config = qx(git config --get color.interactive);
 if ($color_config=~/true|always/ || -t STDOUT && $color_config=~/auto/) {
 	$use_color = "true";
-        # Sane (visible) defaults:
-        $prompt_color = "blue bold";
-        $header_color = "bold";
-        $help_color = "red bold";
+	# Grab the 3 main colors in git color string format:
+	my @git_prompt_color =
+		split(/\s+/, qx(git config --get color.interactive.prompt));
+	my @git_header_color =
+		split(/\s+/, qx(git config --get color.interactive.header));
+	my @git_help_color =
+		split(/\s+/, qx(git config --get color.interactive.help));
+
+	# Sane (visible) defaults:
+	if (! @git_prompt_color) {
+		@git_prompt_color = ("blue", "bold");
+	}
+	if (! @git_header_color) {
+		@git_header_color = ("bold");
+	}
+	if (! @git_help_color) {
+		@git_help_color = ("red", "bold");
+	}
+
+	# Parse the git colors into perl colors:
+	my %attrib_mappings = (
+		"bold"    => "bold",
+		"ul"      => "underline",
+		"blink"   => "blink",
+		# not supported:
+		#"dim"     => "",
+		"reverse" => "reverse"
+	);
+
+	my @tmp_perl_colors;
+	my $color_list;
+	# Loop over the array of (arrays of) git-style colors
+	foreach $color_list ([@git_prompt_color], [@git_header_color],
+	                     [@git_help_color]) {
+		my $fg_done;
+		my @perl_attribs;
+		my $word;
+		foreach $word (@{$color_list}) {
+			if ($word =~ /normal/) {
+				$fg_done = "true";
+			}
+			elsif ($word =~ /black|red|green|yellow/ ||
+			       $word =~ /blue|magenta|cyan|white/) {
+				# is a color.
+				if ($fg_done) {
+					# this is the background
+					push @perl_attribs, "on_" . $word;
+				}
+				else {
+					# this is foreground
+					$fg_done = "true";
+					push @perl_attribs, $word;
+				}
+			}
+			else {
+				# this is an attribute, not a color.
+				if ($attrib_mappings{$word}) {
+					push(@perl_attribs,
+						 $attrib_mappings{$word});
+				}
+			}
+		}
+		if (@perl_attribs) {
+			push @tmp_perl_colors, join(" ", @perl_attribs);
+		}
+		else {
+			#@perl_attribs is empty, need a placeholder
+			push @tmp_perl_colors, "reset";
+		}
+	}
+	($prompt_color, $header_color, $help_color) =
+		@tmp_perl_colors;
 
 	require Term::ANSIColor;
 }
-- 
1.5.3.4.207.gc0ee
Previous: Wincent ColaiutaNext: Jeff King
Message 30 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.