Skip to content

Commit

Permalink
Merge branch 'jk/color-parse'
Browse files Browse the repository at this point in the history
* jk/color-parse:
  Optimize color_parse_mem
  expand --pretty=format color options
  color: make it easier for non-config to parse color specs
  • Loading branch information
gitster committed Jan 22, 2009
2 parents a14f154 + 2c2dc7c commit 35e6afd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions Documentation/pretty-formats.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions color.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);

Expand Down
12 changes: 12 additions & 0 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "string-list.h"
#include "mailmap.h"
#include "log-tree.h"
#include "color.h"

static char *user_format;

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion t/t6006-rev-list-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 35e6afd

Please sign in to comment.