Skip to content

Commit dc6ebd4

Browse files
ArjenLgitster
authored andcommitted
Clean up use of ANSI color sequences
Remove the literal ANSI escape sequences and replace them by readable constants. Signed-off-by: Arjen Laarhoven <arjen@yaph.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5cd12b8 commit dc6ebd4

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

builtin-branch.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ static unsigned char head_sha1[20];
3232

3333
static int branch_use_color = -1;
3434
static char branch_colors[][COLOR_MAXLEN] = {
35-
"\033[m", /* reset */
36-
"", /* PLAIN (normal) */
37-
"\033[31m", /* REMOTE (red) */
38-
"", /* LOCAL (normal) */
39-
"\033[32m", /* CURRENT (green) */
35+
GIT_COLOR_RESET,
36+
GIT_COLOR_NORMAL, /* PLAIN */
37+
GIT_COLOR_RED, /* REMOTE */
38+
GIT_COLOR_NORMAL, /* LOCAL */
39+
GIT_COLOR_GREEN, /* CURRENT */
4040
};
4141
enum color_branch {
4242
COLOR_BRANCH_RESET = 0,

color.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "cache.h"
22
#include "color.h"
33

4-
#define COLOR_RESET "\033[m"
5-
64
int git_use_color_default = 0;
75

86
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,
5452
int bg = -2;
5553

5654
if (!strncasecmp(value, "reset", len)) {
57-
strcpy(dst, "\033[m");
55+
strcpy(dst, GIT_COLOR_RESET);
5856
return;
5957
}
6058

@@ -175,7 +173,7 @@ static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
175173
r += fprintf(fp, "%s", color);
176174
r += vfprintf(fp, fmt, args);
177175
if (*color)
178-
r += fprintf(fp, "%s", COLOR_RESET);
176+
r += fprintf(fp, "%s", GIT_COLOR_RESET);
179177
if (trail)
180178
r += fprintf(fp, "%s", trail);
181179
return r;
@@ -217,7 +215,7 @@ int color_fwrite_lines(FILE *fp, const char *color,
217215
char *p = memchr(buf, '\n', count);
218216
if (p != buf && (fputs(color, fp) < 0 ||
219217
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
220-
fputs(COLOR_RESET, fp) < 0))
218+
fputs(GIT_COLOR_RESET, fp) < 0))
221219
return -1;
222220
if (!p)
223221
return 0;

color.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
55
#define COLOR_MAXLEN 24
66

7+
#define GIT_COLOR_NORMAL ""
8+
#define GIT_COLOR_RESET "\033[m"
9+
#define GIT_COLOR_BOLD "\033[1m"
10+
#define GIT_COLOR_RED "\033[31m"
11+
#define GIT_COLOR_GREEN "\033[32m"
12+
#define GIT_COLOR_YELLOW "\033[33m"
13+
#define GIT_COLOR_BLUE "\033[34m"
14+
#define GIT_COLOR_CYAN "\033[36m"
15+
#define GIT_COLOR_BG_RED "\033[41m"
16+
717
/*
818
* This variable stores the value of color.ui
919
*/

diff.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ int diff_auto_refresh_index = 1;
3030
static int diff_mnemonic_prefix;
3131

3232
static char diff_colors[][COLOR_MAXLEN] = {
33-
"\033[m", /* reset */
34-
"", /* PLAIN (normal) */
35-
"\033[1m", /* METAINFO (bold) */
36-
"\033[36m", /* FRAGINFO (cyan) */
37-
"\033[31m", /* OLD (red) */
38-
"\033[32m", /* NEW (green) */
39-
"\033[33m", /* COMMIT (yellow) */
40-
"\033[41m", /* WHITESPACE (red background) */
33+
GIT_COLOR_RESET,
34+
GIT_COLOR_NORMAL, /* PLAIN */
35+
GIT_COLOR_BOLD, /* METAINFO */
36+
GIT_COLOR_CYAN, /* FRAGINFO */
37+
GIT_COLOR_RED, /* OLD */
38+
GIT_COLOR_GREEN, /* NEW */
39+
GIT_COLOR_YELLOW, /* COMMIT */
40+
GIT_COLOR_BG_RED, /* WHITESPACE */
4141
};
4242

4343
static void diff_filespec_load_driver(struct diff_filespec *one);

pretty.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
567567
return end - placeholder + 1;
568568
}
569569
if (!prefixcmp(placeholder + 1, "red")) {
570-
strbuf_addstr(sb, "\033[31m");
570+
strbuf_addstr(sb, GIT_COLOR_RED);
571571
return 4;
572572
} else if (!prefixcmp(placeholder + 1, "green")) {
573-
strbuf_addstr(sb, "\033[32m");
573+
strbuf_addstr(sb, GIT_COLOR_GREEN);
574574
return 6;
575575
} else if (!prefixcmp(placeholder + 1, "blue")) {
576-
strbuf_addstr(sb, "\033[34m");
576+
strbuf_addstr(sb, GIT_COLOR_BLUE);
577577
return 5;
578578
} else if (!prefixcmp(placeholder + 1, "reset")) {
579-
strbuf_addstr(sb, "\033[m");
579+
strbuf_addstr(sb, GIT_COLOR_RESET);
580580
return 6;
581581
} else
582582
return 0;

wt-status.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ int wt_status_relative_paths = 1;
1515
int wt_status_use_color = -1;
1616
int wt_status_submodule_summary;
1717
static char wt_status_colors[][COLOR_MAXLEN] = {
18-
"", /* WT_STATUS_HEADER: normal */
19-
"\033[32m", /* WT_STATUS_UPDATED: green */
20-
"\033[31m", /* WT_STATUS_CHANGED: red */
21-
"\033[31m", /* WT_STATUS_UNTRACKED: red */
22-
"\033[31m", /* WT_STATUS_NOBRANCH: red */
18+
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
19+
GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
20+
GIT_COLOR_RED, /* WT_STATUS_CHANGED */
21+
GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
22+
GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
2323
};
2424

2525
enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;

0 commit comments

Comments
 (0)