This uses the strbuf_nested_expand() mechanism introduced earlier to demonstrate how to implement a nested string function, by plugging Dscho's implementation of strbuf_add_wrapped_text(). The log is much more pleasant to read with $ git log --format='commit %H%nAuthor: %an%n%n%[wrap(66,4,4)%s%n%n%b%]' in some repositories (e.g. git-svn conversion of rockbox). Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> --- Renamed w() to wrap() and actually plugged Dscho's strbuf_add_wrapped_text(). pretty.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 76 insertions(+), 0 deletions(-) diff --git a/pretty.c b/pretty.c index 126be56..bc74c25 100644 --- a/pretty.c +++ b/pretty.c @@ -595,6 +595,72 @@ static void format_decoration(struct strbuf *sb, const struct commit *commit) strbuf_addch(sb, ')'); } +typedef int (*string_fmt_fn)(struct strbuf *sb, const char *placeholder, void *context); +static ssize_t format_commit_item(struct strbuf *, const char *, void *); + +static int wrap_fn(struct strbuf *sb, const char *placeholder, void *context) +{ + const char *template = placeholder; + char *endptr, *nested; + long width = 0, indent1 = 0, indent2 = 0; + + width = strtol(template, &endptr, 10); + if (*endptr == ',') { + template = endptr + 1; + indent1 = strtol(template, &endptr, 10); + if (*endptr == ',') { + template = endptr + 1; + indent2 = strtol(template, &endptr, 10); + } + } + if (*endptr++ != ')') + return 0; + + template = endptr; + strbuf_nested_expand(sb, &template, format_commit_item, context); + if (*template++ != ']') + return 0; + + nested = strbuf_detach(sb, NULL); + strbuf_add_wrapped_text(sb, nested, indent1, indent2, width); + free(nested); + + return template - placeholder; +} + +static struct { + const char *name; + string_fmt_fn fn; +} format_fn_list[] = { + { "wrap(", wrap_fn } +}; + +static size_t format_fn(struct strbuf *sb, const char *placeholder, + void *context) +{ + const char *template = placeholder; + size_t consumed; + struct strbuf substr = STRBUF_INIT; + int i; + + for (i = 0; i < ARRAY_SIZE(format_fn_list); i++) + if (!prefixcmp(template, format_fn_list[i].name)) + break; + if (ARRAY_SIZE(format_fn_list) <= i) + return 0; + template += strlen(format_fn_list[i].name); + consumed = format_fn_list[i].fn(&substr, template, context); + if (!consumed) { + strbuf_release(&substr); + return 0; + } + + strbuf_add(sb, substr.buf, substr.len); + template += consumed; + strbuf_release(&substr); + return template - placeholder; +} + static ssize_t format_commit_item(struct strbuf *sb, const char *placeholder, void *context) { @@ -603,9 +669,19 @@ static ssize_t format_commit_item(struct strbuf *sb, const char *placeholder, const char *msg = commit->buffer; struct commit_list *p; int h1, h2; + ssize_t nested; /* these are independent of the commit */ switch (placeholder[0]) { + case ']': + return -1; + case '[': + /* + * %[func(arg...) string %]: we consumed the opening '[' + * and the callee consumed up to the closing '%]'. + */ + nested = format_fn(sb, placeholder + 1, context); + return nested ? 1 + nested : 0; case 'C': if (placeholder[1] == '(') { const char *end = strchr(placeholder + 2, ')'); -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html