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

[PATCH] Clean up use of ANSI color sequences

From
Arjen Laarhoven <arjen@yaph.org>
Date
Feb 12, 2009, 20:37 UTC
Message-ID
<1234471059-53625-1-git-send-email-arjen@yaph.org>

Remove the literal ANSI escape sequences and replace them by readable constants.

Signed-off-by: Arjen Laarhoven <arjen@yaph.org>
---
 builtin-branch.c |   10 +++++-----
 color.c          |    4 +---
 color.h          |   10 ++++++++++
 diff.c           |   16 ++++++++--------
 pretty.c         |    8 ++++----
 wt-status.c      |   10 +++++-----
 6 files changed, 33 insertions(+), 25 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index 56a1971..c154500 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -32,11 +32,11 @@ static unsigned char head_sha1[20];
 
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
-	"\033[m",	/* reset */
-	"",		/* PLAIN (normal) */
-	"\033[31m",	/* REMOTE (red) */
-	"",		/* LOCAL (normal) */
-	"\033[32m",	/* CURRENT (green) */
+	COLOR_RESET,
+	COLOR_NORMAL,	/* PLAIN */
+	COLOR_RED,	/* REMOTE */
+	COLOR_NORMAL,	/* LOCAL */
+	COLOR_GREEN,	/* CURRENT */
 };
 enum color_branch {
 	COLOR_BRANCH_RESET = 0,
diff --git a/color.c b/color.c
index db4dccf..5653667 100644
--- a/color.c
+++ b/color.c
@@ -1,8 +1,6 @@
 #include "cache.h"
 #include "color.h"
 
-#define COLOR_RESET "\033[m"
-
 int git_use_color_default = 0;
 
 static int parse_color(const char *name, int len)
@@ -54,7 +52,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
 	int bg = -2;
 
 	if (!strncasecmp(value, "reset", len)) {
-		strcpy(dst, "\033[m");
+		strcpy(dst, COLOR_RESET);
 		return;
 	}
 
diff --git a/color.h b/color.h
index 5019df8..c4d2e53 100644
--- a/color.h
+++ b/color.h
@@ -4,6 +4,16 @@
 /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
 #define COLOR_MAXLEN 24
 
+#define COLOR_NORMAL	""
+#define COLOR_RESET	"\033[m"
+#define COLOR_BOLD	"\033[1m"
+#define COLOR_RED	"\033[31m"
+#define COLOR_GREEN	"\033[32m"
+#define COLOR_YELLOW	"\033[33m"
+#define COLOR_BLUE	"\033[34m"
+#define COLOR_CYAN	"\033[36m"
+#define COLOR_BG_RED	"\033[41m"
+
 /*
  * This variable stores the value of color.ui
  */
diff --git a/diff.c b/diff.c
index a5a540f..1ca64d3 100644
--- a/diff.c
+++ b/diff.c
@@ -30,14 +30,14 @@ int diff_auto_refresh_index = 1;
 static int diff_mnemonic_prefix;
 
 static char diff_colors[][COLOR_MAXLEN] = {
-	"\033[m",	/* reset */
-	"",		/* PLAIN (normal) */
-	"\033[1m",	/* METAINFO (bold) */
-	"\033[36m",	/* FRAGINFO (cyan) */
-	"\033[31m",	/* OLD (red) */
-	"\033[32m",	/* NEW (green) */
-	"\033[33m",	/* COMMIT (yellow) */
-	"\033[41m",	/* WHITESPACE (red background) */
+	COLOR_RESET,
+	COLOR_NORMAL,	/* PLAIN */
+	COLOR_BOLD,	/* METAINFO */
+	COLOR_CYAN,	/* FRAGINFO */
+	COLOR_RED,	/* OLD */
+	COLOR_GREEN,	/* NEW */
+	COLOR_YELLOW,	/* COMMIT */
+	COLOR_BG_RED,	/* WHITESPACE */
 };
 
 static void diff_filespec_load_driver(struct diff_filespec *one);
diff --git a/pretty.c b/pretty.c
index cc460b5..a8595f6 100644
--- a/pretty.c
+++ b/pretty.c
@@ -567,16 +567,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 			return end - placeholder + 1;
 		}
 		if (!prefixcmp(placeholder + 1, "red")) {
-			strbuf_addstr(sb, "\033[31m");
+			strbuf_addstr(sb, COLOR_RED);
 			return 4;
 		} else if (!prefixcmp(placeholder + 1, "green")) {
-			strbuf_addstr(sb, "\033[32m");
+			strbuf_addstr(sb, COLOR_GREEN);
 			return 6;
 		} else if (!prefixcmp(placeholder + 1, "blue")) {
-			strbuf_addstr(sb, "\033[34m");
+			strbuf_addstr(sb, COLOR_BLUE);
 			return 5;
 		} else if (!prefixcmp(placeholder + 1, "reset")) {
-			strbuf_addstr(sb, "\033[m");
+			strbuf_addstr(sb, COLOR_RESET);
 			return 6;
 		} else
 			return 0;
diff --git a/wt-status.c b/wt-status.c
index 96ff2f8..432d23a 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -15,11 +15,11 @@ int wt_status_relative_paths = 1;
 int wt_status_use_color = -1;
 int wt_status_submodule_summary;
 static char wt_status_colors[][COLOR_MAXLEN] = {
-	"",         /* WT_STATUS_HEADER: normal */
-	"\033[32m", /* WT_STATUS_UPDATED: green */
-	"\033[31m", /* WT_STATUS_CHANGED: red */
-	"\033[31m", /* WT_STATUS_UNTRACKED: red */
-	"\033[31m", /* WT_STATUS_NOBRANCH: red */
+	COLOR_NORMAL, /* WT_STATUS_HEADER */
+	COLOR_GREEN,  /* WT_STATUS_UPDATED */
+	COLOR_RED,    /* WT_STATUS_CHANGED */
+	COLOR_RED,    /* WT_STATUS_UNTRACKED */
+	COLOR_RED,    /* WT_STATUS_NOBRANCH */
 };
 
 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
-- 
1.6.2.rc0.186.g417c
Next: Junio C Hamano
Message 1 of 7 in “Clean up use of ANSI color sequences”
  1. Clean up use of ANSI color sequencesArjen Laarhoven, Feb 12, 2009
  2. Junio C HamanoFeb 12, 2009
  3. 1/2 Clean up use of ANSI color sequencesArjen Laarhoven, Feb 13, 2009
  4. 2/2 builtin-branch.c: Rename branch category color namesArjen Laarhoven, Feb 13, 2009
  5. Junio C HamanoFeb 14, 2009
  6. Arjen LaarhovenFeb 14, 2009
  7. Junio C HamanoFeb 14, 2009

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.