1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-03 15:46:17 +02:00

Merge branch 'jk/color-parse'

* 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
commit 35e6afd4c6
5 changed files with 44 additions and 12 deletions

View File

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

31
color.c
View File

@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
}
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;
int len = value_len;
int attr = -1;
int fg = -2;
int bg = -2;
if (!strcasecmp(value, "reset")) {
if (!strncasecmp(value, "reset", len)) {
strcpy(dst, "\033[m");
return;
}
/* [fg [bg]] [attr] */
while (*ptr) {
while (len > 0) {
const char *word = ptr;
int val, len = 0;
int val, wordlen = 0;
while (word[len] && !isspace(word[len]))
len++;
while (len > 0 && !isspace(word[wordlen])) {
wordlen++;
len--;
}
ptr = word + len;
while (*ptr && isspace(*ptr))
ptr = word + wordlen;
while (len > 0 && isspace(*ptr)) {
ptr++;
len--;
}
val = parse_color(word, len);
val = parse_color(word, wordlen);
if (val >= -1) {
if (fg == -2) {
fg = val;
@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
}
goto bad;
}
val = parse_attr(word, len);
val = parse_attr(word, wordlen);
if (val < 0 || attr != -1)
goto bad;
attr = val;
@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
*dst = 0;
return;
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)

View File

@ -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_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_ln(FILE *fp, const char *color, const char *fmt, ...);

View File

@ -6,6 +6,7 @@
#include "string-list.h"
#include "mailmap.h"
#include "log-tree.h"
#include "color.h"
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 */
switch (placeholder[0]) {
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")) {
strbuf_addstr(sb, "\033[31m");
return 4;

View File

@ -14,7 +14,7 @@ touch foo && git add foo && git commit -m "added foo" &&
test_format() {
cat >expect.$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
"
}
@ -101,6 +101,13 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foobarbazxyzzy
EOF
test_format advanced-colors '%C(red yellow bold)foo%C(reset)' <<'EOF'
commit 131a310eb913d107dd3c09a65d1651175898735d
foo
commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
foo
EOF
cat >commit-msg <<'EOF'
Test printing of complex bodies