Merge branch 'al/ansi-color'

* al/ansi-color:
  builtin-branch.c: Rename branch category color names
  Clean up use of ANSI color sequences
This commit is contained in:
Junio C Hamano 2009-03-05 15:41:19 -08:00
commit 4a2caf6912
6 changed files with 51 additions and 43 deletions

View file

@ -32,18 +32,18 @@ static unsigned char head_sha1[20];
static int branch_use_color = -1; static int branch_use_color = -1;
static char branch_colors[][COLOR_MAXLEN] = { static char branch_colors[][COLOR_MAXLEN] = {
"\033[m", /* reset */ GIT_COLOR_RESET,
"", /* PLAIN (normal) */ GIT_COLOR_NORMAL, /* PLAIN */
"\033[31m", /* REMOTE (red) */ GIT_COLOR_RED, /* REMOTE */
"", /* LOCAL (normal) */ GIT_COLOR_NORMAL, /* LOCAL */
"\033[32m", /* CURRENT (green) */ GIT_COLOR_GREEN, /* CURRENT */
}; };
enum color_branch { enum color_branch {
COLOR_BRANCH_RESET = 0, BRANCH_COLOR_RESET = 0,
COLOR_BRANCH_PLAIN = 1, BRANCH_COLOR_PLAIN = 1,
COLOR_BRANCH_REMOTE = 2, BRANCH_COLOR_REMOTE = 2,
COLOR_BRANCH_LOCAL = 3, BRANCH_COLOR_LOCAL = 3,
COLOR_BRANCH_CURRENT = 4, BRANCH_COLOR_CURRENT = 4,
}; };
static enum merge_filter { static enum merge_filter {
@ -56,15 +56,15 @@ static unsigned char merge_filter_ref[20];
static int parse_branch_color_slot(const char *var, int ofs) static int parse_branch_color_slot(const char *var, int ofs)
{ {
if (!strcasecmp(var+ofs, "plain")) if (!strcasecmp(var+ofs, "plain"))
return COLOR_BRANCH_PLAIN; return BRANCH_COLOR_PLAIN;
if (!strcasecmp(var+ofs, "reset")) if (!strcasecmp(var+ofs, "reset"))
return COLOR_BRANCH_RESET; return BRANCH_COLOR_RESET;
if (!strcasecmp(var+ofs, "remote")) if (!strcasecmp(var+ofs, "remote"))
return COLOR_BRANCH_REMOTE; return BRANCH_COLOR_REMOTE;
if (!strcasecmp(var+ofs, "local")) if (!strcasecmp(var+ofs, "local"))
return COLOR_BRANCH_LOCAL; return BRANCH_COLOR_LOCAL;
if (!strcasecmp(var+ofs, "current")) if (!strcasecmp(var+ofs, "current"))
return COLOR_BRANCH_CURRENT; return BRANCH_COLOR_CURRENT;
die("bad config variable '%s'", var); die("bad config variable '%s'", var);
} }
@ -310,20 +310,20 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
switch (item->kind) { switch (item->kind) {
case REF_LOCAL_BRANCH: case REF_LOCAL_BRANCH:
color = COLOR_BRANCH_LOCAL; color = BRANCH_COLOR_LOCAL;
break; break;
case REF_REMOTE_BRANCH: case REF_REMOTE_BRANCH:
color = COLOR_BRANCH_REMOTE; color = BRANCH_COLOR_REMOTE;
break; break;
default: default:
color = COLOR_BRANCH_PLAIN; color = BRANCH_COLOR_PLAIN;
break; break;
} }
c = ' '; c = ' ';
if (current) { if (current) {
c = '*'; c = '*';
color = COLOR_BRANCH_CURRENT; color = BRANCH_COLOR_CURRENT;
} }
if (verbose) { if (verbose) {
@ -342,14 +342,14 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color), printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
maxwidth, item->name, maxwidth, item->name,
branch_get_color(COLOR_BRANCH_RESET), branch_get_color(BRANCH_COLOR_RESET),
find_unique_abbrev(item->commit->object.sha1, abbrev), find_unique_abbrev(item->commit->object.sha1, abbrev),
stat.buf, sub); stat.buf, sub);
strbuf_release(&stat); strbuf_release(&stat);
strbuf_release(&subject); strbuf_release(&subject);
} else { } else {
printf("%c %s%s%s\n", c, branch_get_color(color), item->name, printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
branch_get_color(COLOR_BRANCH_RESET)); branch_get_color(BRANCH_COLOR_RESET));
} }
} }

View file

@ -1,8 +1,6 @@
#include "cache.h" #include "cache.h"
#include "color.h" #include "color.h"
#define COLOR_RESET "\033[m"
int git_use_color_default = 0; int git_use_color_default = 0;
static int parse_color(const char *name, int len) 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; int bg = -2;
if (!strncasecmp(value, "reset", len)) { if (!strncasecmp(value, "reset", len)) {
strcpy(dst, "\033[m"); strcpy(dst, GIT_COLOR_RESET);
return; return;
} }
@ -175,7 +173,7 @@ static int color_vfprintf(FILE *fp, const char *color, const char *fmt,
r += fprintf(fp, "%s", color); r += fprintf(fp, "%s", color);
r += vfprintf(fp, fmt, args); r += vfprintf(fp, fmt, args);
if (*color) if (*color)
r += fprintf(fp, "%s", COLOR_RESET); r += fprintf(fp, "%s", GIT_COLOR_RESET);
if (trail) if (trail)
r += fprintf(fp, "%s", trail); r += fprintf(fp, "%s", trail);
return r; return r;
@ -217,7 +215,7 @@ int color_fwrite_lines(FILE *fp, const char *color,
char *p = memchr(buf, '\n', count); char *p = memchr(buf, '\n', count);
if (p != buf && (fputs(color, fp) < 0 || if (p != buf && (fputs(color, fp) < 0 ||
fwrite(buf, p ? p - buf : count, 1, fp) != 1 || fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
fputs(COLOR_RESET, fp) < 0)) fputs(GIT_COLOR_RESET, fp) < 0))
return -1; return -1;
if (!p) if (!p)
return 0; return 0;

10
color.h
View file

@ -4,6 +4,16 @@
/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */ /* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
#define COLOR_MAXLEN 24 #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 * This variable stores the value of color.ui
*/ */

16
diff.c
View file

@ -30,14 +30,14 @@ int diff_auto_refresh_index = 1;
static int diff_mnemonic_prefix; static int diff_mnemonic_prefix;
static char diff_colors[][COLOR_MAXLEN] = { static char diff_colors[][COLOR_MAXLEN] = {
"\033[m", /* reset */ GIT_COLOR_RESET,
"", /* PLAIN (normal) */ GIT_COLOR_NORMAL, /* PLAIN */
"\033[1m", /* METAINFO (bold) */ GIT_COLOR_BOLD, /* METAINFO */
"\033[36m", /* FRAGINFO (cyan) */ GIT_COLOR_CYAN, /* FRAGINFO */
"\033[31m", /* OLD (red) */ GIT_COLOR_RED, /* OLD */
"\033[32m", /* NEW (green) */ GIT_COLOR_GREEN, /* NEW */
"\033[33m", /* COMMIT (yellow) */ GIT_COLOR_YELLOW, /* COMMIT */
"\033[41m", /* WHITESPACE (red background) */ GIT_COLOR_BG_RED, /* WHITESPACE */
}; };
static void diff_filespec_load_driver(struct diff_filespec *one); static void diff_filespec_load_driver(struct diff_filespec *one);

View file

@ -568,16 +568,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
return end - placeholder + 1; return end - placeholder + 1;
} }
if (!prefixcmp(placeholder + 1, "red")) { if (!prefixcmp(placeholder + 1, "red")) {
strbuf_addstr(sb, "\033[31m"); strbuf_addstr(sb, GIT_COLOR_RED);
return 4; return 4;
} else if (!prefixcmp(placeholder + 1, "green")) { } else if (!prefixcmp(placeholder + 1, "green")) {
strbuf_addstr(sb, "\033[32m"); strbuf_addstr(sb, GIT_COLOR_GREEN);
return 6; return 6;
} else if (!prefixcmp(placeholder + 1, "blue")) { } else if (!prefixcmp(placeholder + 1, "blue")) {
strbuf_addstr(sb, "\033[34m"); strbuf_addstr(sb, GIT_COLOR_BLUE);
return 5; return 5;
} else if (!prefixcmp(placeholder + 1, "reset")) { } else if (!prefixcmp(placeholder + 1, "reset")) {
strbuf_addstr(sb, "\033[m"); strbuf_addstr(sb, GIT_COLOR_RESET);
return 6; return 6;
} else } else
return 0; return 0;

View file

@ -15,11 +15,11 @@ int wt_status_relative_paths = 1;
int wt_status_use_color = -1; int wt_status_use_color = -1;
int wt_status_submodule_summary; int wt_status_submodule_summary;
static char wt_status_colors[][COLOR_MAXLEN] = { static char wt_status_colors[][COLOR_MAXLEN] = {
"", /* WT_STATUS_HEADER: normal */ GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
"\033[32m", /* WT_STATUS_UPDATED: green */ GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
"\033[31m", /* WT_STATUS_CHANGED: red */ GIT_COLOR_RED, /* WT_STATUS_CHANGED */
"\033[31m", /* WT_STATUS_UNTRACKED: red */ GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
"\033[31m", /* WT_STATUS_NOBRANCH: red */ GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
}; };
enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;