git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC 6/9] add long-form %(wrap:...) for %w(...)

From
Will Palmer <wmpalmer@gmail.com>
Date
Mar 28, 2011, 23:17 UTC
Message-ID
<1301354251-23380-7-git-send-email-wmpalmer@gmail.com>
In-Reply-To
<1301354251-23380-1-git-send-email-wmpalmer@gmail.com>
the list of user-defined format placeholders has grown steadily longer
since they were first introduced. We currently have about forty
placeholders, and the room for new mnemonics is growing scarce. To make
more room, we introduce "long forms" for placeholders, which take the
form:
	'%(' <label> [ ':' <arg> [ ',' <arg> ]* ] ')'
eg:
	%(wrap: 80, 0, 4)

We start by adding a long-form to the %w(...) placeholder, mostly because as it already takes multiple arguments, it is a good example.

Signed-off-by: Will Palmer <wmpalmer@gmail.com>
---
 Documentation/pretty-formats.txt |    1 +
 pretty.c                         |   47 +++++++++++++++++++++++++++++++++-----
 test-pretty.c                    |    1 +
 3 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 561cc9f..c9f3fb6 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -144,6 +144,7 @@ The placeholders are:
 - '%x00': print a byte from a hex code
 - '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
   linkgit:git-shortlog[1].
+- '%(wrap:[<w>[,<i1>[,<i2>]]])': alternative form of %w(...)
 
 NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will
diff --git a/pretty.c b/pretty.c
index cb02879..8301008 100644
--- a/pretty.c
+++ b/pretty.c
@@ -108,12 +108,10 @@ const char *parse_arg(struct format_part *part, enum format_arg_type type,
 	arg.type = type;
 
 	c += strspn(c, WHITESPACE);
-	if (!isdigit(*c))
-		return NULL;
-	arg.uint = strtoul(c, &t, 10);
-	if (t == c)
-		return NULL;
-	c = t + strspn(t, WHITESPACE);
+	if (isdigit(*c)) {
+		arg.uint = strtoul(c, &t, 10);
+		c = t + strspn(t, WHITESPACE);
+	}
 	if (*c == ',' || *c == ')'){
 		ALLOC_GROW(part->args, part->argc+1, part->args_alloc);
 		memcpy(&(part->args[part->argc]), &arg,
@@ -124,6 +122,41 @@ const char *parse_arg(struct format_part *part, enum format_arg_type type,
 	return NULL;
 }
 
+static struct format_part *parse_extended(const char *unparsed)
+{
+	struct format_part *part = format_part_alloc();
+	const char *c = unparsed + 2; /* "%(..." + strlen("%(") */
+
+	c += strspn(c, WHITESPACE);
+
+	if (!prefixcmp(c, "wrap")) {
+		part->type = FORMAT_PART_WRAP;
+		c += 4;
+		while(part->argc <= 3){
+			c += strspn(c, WHITESPACE);
+			if (*c == ')')
+				goto success;
+			if (*c != (part->argc ? ',' : ':'))
+				goto fail;
+			if (part->argc == 3)
+				goto fail;
+
+			c = parse_arg(part, FORMAT_ARG_UINT, c+1);
+			if (!c)
+				goto fail;
+		}
+		goto fail;
+	}
+
+fail:
+	format_part_free(&part);
+	return NULL;
+
+success:
+	part->format_len = c - unparsed + 1;
+	return part;
+}
+
 static struct format_part *parse_special(const char *unparsed)
 {
 	struct format_part *part = NULL;
@@ -156,6 +189,8 @@ static struct format_part *parse_special(const char *unparsed)
 			}
 		}
 		return part;
+	case '(':
+		return parse_extended(unparsed);
 	}
 
 	part = format_part_alloc();
diff --git a/test-pretty.c b/test-pretty.c
index 57c1c65..64a8218 100644
--- a/test-pretty.c
+++ b/test-pretty.c
@@ -17,6 +17,7 @@ static const char *all = "a"
 "%gD%gd%gs"
 "%Cred%Cgreen%Cblue%Creset%C(reset)"
 "%m%w()%w(1)%w(1,2)%w(1,2,3)"
+"%(wrap)%(wrap:1)%(wrap:1,2)%(wrap:1,2,3)"
 "%x0a%n%%%@";
 
 static struct strbuf *parts_debug(struct format_parts *parts,
-- 
1.7.4.2
Previous: Will PalmerNext: Will Palmer
Message 7 of 14 in “add long forms for format placeholders”
  1. 0/9 add long forms for format placeholdersWill Palmer, Mar 28, 2011
  2. 1/9 mention --date=raw in rev-list and blame helpWill Palmer, Mar 28, 2011
  3. 2/9 add support for --date=unix to complement %atWill Palmer, Mar 28, 2011
  4. 3/9 interpret %C(invalid) as we would %%C(invalid)Will Palmer, Mar 28, 2011
  5. 4/9 add sanity length check to format_person_partWill Palmer, Mar 28, 2011
  6. 5/9 refactor pretty.c into "parse" and "format" stepsWill Palmer, Mar 28, 2011
  7. 6/9 add long-form %(wrap:...) for %w(...)Will Palmer, Mar 28, 2011
  8. 7/9 add long form %(color:...) for %C(...)Will Palmer, Mar 28, 2011
  9. 8/9 add long forms %(authordate) and %(committerdate)Will Palmer, Mar 28, 2011
  10. 9/9 add long forms for author and committer identityWill Palmer, Mar 28, 2011
  11. Junio C HamanoMar 29, 2011
  12. Will PalmerMar 29, 2011
  13. Michael J GruberMar 29, 2011
  14. Will PalmerMar 29, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.