* jk/color-parse:
  Optimize color_parse_mem
  expand --pretty=format color options
  color: make it easier for non-config to parse color specs
This commit is contained in:
Junio C Hamano 2009-01-21 16:50:34 -08:00
Родитель a14f15427b 2c2dc7c82c
Коммит 35e6afd4c6
5 изменённых файлов: 44 добавлений и 12 удалений

Просмотреть файл

@ -124,6 +124,7 @@ The placeholders are:
- '%Cgreen': switch color to green - '%Cgreen': switch color to green
- '%Cblue': switch color to blue - '%Cblue': switch color to blue
- '%Creset': reset color - '%Creset': reset color
- '%C(...)': color specification, as described in color.branch.* config option
- '%m': left, right or boundary mark - '%m': left, right or boundary mark
- '%n': newline - '%n': newline
- '%x00': print a byte from a hex code - '%x00': print a byte from a hex code

31
color.c
Просмотреть файл

@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
} }
void color_parse(const char *value, const char *var, char *dst) void color_parse(const char *value, const char *var, char *dst)
{
color_parse_mem(value, strlen(value), var, dst);
}
void color_parse_mem(const char *value, int value_len, const char *var,
char *dst)
{ {
const char *ptr = value; const char *ptr = value;
int len = value_len;
int attr = -1; int attr = -1;
int fg = -2; int fg = -2;
int bg = -2; int bg = -2;
if (!strcasecmp(value, "reset")) { if (!strncasecmp(value, "reset", len)) {
strcpy(dst, "\033[m"); strcpy(dst, "\033[m");
return; return;
} }
/* [fg [bg]] [attr] */ /* [fg [bg]] [attr] */
while (*ptr) { while (len > 0) {
const char *word = ptr; const char *word = ptr;
int val, len = 0; int val, wordlen = 0;
while (word[len] && !isspace(word[len])) while (len > 0 && !isspace(word[wordlen])) {
len++; wordlen++;
len--;
}
ptr = word + len; ptr = word + wordlen;
while (*ptr && isspace(*ptr)) while (len > 0 && isspace(*ptr)) {
ptr++; ptr++;
len--;
}
val = parse_color(word, len); val = parse_color(word, wordlen);
if (val >= -1) { if (val >= -1) {
if (fg == -2) { if (fg == -2) {
fg = val; fg = val;
@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
} }
goto bad; goto bad;
} }
val = parse_attr(word, len); val = parse_attr(word, wordlen);
if (val < 0 || attr != -1) if (val < 0 || attr != -1)
goto bad; goto bad;
attr = val; attr = val;
@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
*dst = 0; *dst = 0;
return; return;
bad: bad:
die("bad config value '%s' for variable '%s'", value, var); die("bad color value '%.*s' for variable '%s'", value_len, value, var);
} }
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty) int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)

Просмотреть файл

@ -16,7 +16,8 @@ extern int git_use_color_default;
int git_color_default_config(const char *var, const char *value, void *cb); int git_color_default_config(const char *var, const char *value, void *cb);
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty); int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
void color_parse(const char *var, const char *value, char *dst); void color_parse(const char *value, const char *var, char *dst);
void color_parse_mem(const char *value, int len, const char *var, char *dst);
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...); int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...); int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);

Просмотреть файл

@ -6,6 +6,7 @@
#include "string-list.h" #include "string-list.h"
#include "mailmap.h" #include "mailmap.h"
#include "log-tree.h" #include "log-tree.h"
#include "color.h"
static char *user_format; static char *user_format;
@ -554,6 +555,17 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
/* these are independent of the commit */ /* these are independent of the commit */
switch (placeholder[0]) { switch (placeholder[0]) {
case 'C': case 'C':
if (placeholder[1] == '(') {
const char *end = strchr(placeholder + 2, ')');
char color[COLOR_MAXLEN];
if (!end)
return 0;
color_parse_mem(placeholder + 2,
end - (placeholder + 2),
"--pretty format", color);
strbuf_addstr(sb, color);
return end - placeholder + 1;
}
if (!prefixcmp(placeholder + 1, "red")) { if (!prefixcmp(placeholder + 1, "red")) {
strbuf_addstr(sb, "\033[31m"); strbuf_addstr(sb, "\033[31m");
return 4; return 4;

Просмотреть файл

@ -14,7 +14,7 @@ touch foo && git add foo && git commit -m "added foo" &&
test_format() { test_format() {
cat >expect.$1 cat >expect.$1
test_expect_success "format $1" " test_expect_success "format $1" "
git rev-list --pretty=format:$2 master >output.$1 && git rev-list --pretty=format:'$2' master >output.$1 &&
test_cmp expect.$1 output.$1 test_cmp expect.$1 output.$1
" "
} }
@ -101,6 +101,13 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foobarbazxyzzy foobarbazxyzzy
EOF EOF
test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
commit 131a310eb913d107dd3c09a65d1651175898735d
foo
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foo
EOF
cat >commit-msg <<'EOF' cat >commit-msg <<'EOF'
Test printing of complex bodies Test printing of complex bodies