Skip to content

Commit

Permalink
Clean up use of ANSI color sequences
Browse files Browse the repository at this point in the history
Remove the literal ANSI escape sequences and replace them by readable
constants.

Signed-off-by: Arjen Laarhoven <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
ArjenL authored and gitster committed Feb 14, 2009
1 parent 5cd12b8 commit dc6ebd4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 27 deletions.
10 changes: 5 additions & 5 deletions builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
GIT_COLOR_RESET,
GIT_COLOR_NORMAL, /* PLAIN */
GIT_COLOR_RED, /* REMOTE */
GIT_COLOR_NORMAL, /* LOCAL */
GIT_COLOR_GREEN, /* CURRENT */
};
enum color_branch {
COLOR_BRANCH_RESET = 0,
Expand Down
8 changes: 3 additions & 5 deletions color.c
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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, GIT_COLOR_RESET);
return;
}

Expand Down Expand Up @@ -175,7 +173,7 @@ static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
r += fprintf(fp, "%s", color);
r += vfprintf(fp, fmt, args);
if (*color)
r += fprintf(fp, "%s", COLOR_RESET);
r += fprintf(fp, "%s", GIT_COLOR_RESET);
if (trail)
r += fprintf(fp, "%s", trail);
return r;
Expand Down Expand Up @@ -217,7 +215,7 @@ int color_fwrite_lines(FILE *fp, const char *color,
char *p = memchr(buf, '\n', count);
if (p != buf && (fputs(color, fp) < 0 ||
fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
fputs(COLOR_RESET, fp) < 0))
fputs(GIT_COLOR_RESET, fp) < 0))
return -1;
if (!p)
return 0;
Expand Down
10 changes: 10 additions & 0 deletions color.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
#define COLOR_MAXLEN 24

#define GIT_COLOR_NORMAL ""
#define GIT_COLOR_RESET "\033[m"
#define GIT_COLOR_BOLD "\033[1m"
#define GIT_COLOR_RED "\033[31m"
#define GIT_COLOR_GREEN "\033[32m"
#define GIT_COLOR_YELLOW "\033[33m"
#define GIT_COLOR_BLUE "\033[34m"
#define GIT_COLOR_CYAN "\033[36m"
#define GIT_COLOR_BG_RED "\033[41m"

/*
* This variable stores the value of color.ui
*/
Expand Down
16 changes: 8 additions & 8 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
GIT_COLOR_RESET,
GIT_COLOR_NORMAL, /* PLAIN */
GIT_COLOR_BOLD, /* METAINFO */
GIT_COLOR_CYAN, /* FRAGINFO */
GIT_COLOR_RED, /* OLD */
GIT_COLOR_GREEN, /* NEW */
GIT_COLOR_YELLOW, /* COMMIT */
GIT_COLOR_BG_RED, /* WHITESPACE */
};

static void diff_filespec_load_driver(struct diff_filespec *one);
Expand Down
8 changes: 4 additions & 4 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, GIT_COLOR_RED);
return 4;
} else if (!prefixcmp(placeholder + 1, "green")) {
strbuf_addstr(sb, "\033[32m");
strbuf_addstr(sb, GIT_COLOR_GREEN);
return 6;
} else if (!prefixcmp(placeholder + 1, "blue")) {
strbuf_addstr(sb, "\033[34m");
strbuf_addstr(sb, GIT_COLOR_BLUE);
return 5;
} else if (!prefixcmp(placeholder + 1, "reset")) {
strbuf_addstr(sb, "\033[m");
strbuf_addstr(sb, GIT_COLOR_RESET);
return 6;
} else
return 0;
Expand Down
10 changes: 5 additions & 5 deletions wt-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
GIT_COLOR_RED, /* WT_STATUS_CHANGED */
GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
};

enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
Expand Down

0 comments on commit dc6ebd4

Please sign in to comment.