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

[PATCH v5 09/12] ref-filter: align: introduce long-form syntax

From
Karthik Nayak <karthik.188@gmail.com>
Date
Feb 16, 2016, 19:00 UTC
Message-ID
<1455649215-23260-10-git-send-email-Karthik.188@gmail.com>
In-Reply-To
<1455649215-23260-1-git-send-email-Karthik.188@gmail.com>

Introduce optional prefixes "width=" and "position=" for the align atom so that the atom can be used as "%(align:width=<width>,position=<position>)".

Add Documentation and tests for the same.
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Karthik Nayak <Karthik.188@gmail.com>
---
 Documentation/git-for-each-ref.txt | 20 ++++++++++--------
 ref-filter.c                       | 10 ++++++++-
 t/t6302-for-each-ref-filter.sh     | 42 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 9 deletions(-)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 2e3e96f..012e8f9 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -133,14 +133,18 @@ color::
 
 align::
 	Left-, middle-, or right-align the content between
-	%(align:...) and %(end). The "align:" is followed by `<width>`
-	and `<position>` in any order separated by a comma, where the
-	`<position>` is either left, right or middle, default being
-	left and `<width>` is the total length of the content with
-	alignment. If the contents length is more than the width then
-	no alignment is performed. If used with '--quote' everything
-	in between %(align:...) and %(end) is quoted, but if nested
-	then only the topmost level performs quoting.
+	%(align:...) and %(end). The "align:" is followed by
+	`width=<width>` and `position=<position>` in any order
+	separated by a comma, where the `<position>` is either left,
+	right or middle, default being left and `<width>` is the total
+	length of the content with alignment. For brevity, the
+	"width=" and/or "position=" prefixes may be omitted, and bare
+	<width> and <position> used instead.  For instance,
+	`%(align:<width>,<position>)`. If the contents length is more
+	than the width then no alignment is performed. If used with
+	'--quote' everything in between %(align:...) and %(end) is
+	quoted, but if nested then only the topmost level performs
+	quoting.
 
 In addition to the above, for commit and tag objects, the header
 field names (`tree`, `parent`, `object`, `type`, and `tag`) can
diff --git a/ref-filter.c b/ref-filter.c
index 4a9711f..13a8280 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -77,7 +77,15 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
 		int position;
 		const char *s = v[0]->buf;
 
-		if (!strtoul_ui(s, 10, &width))
+		if (skip_prefix(s, "position=", &s)) {
+			position = parse_align_position(s);
+			if (position < 0)
+				die(_("unrecognized position:%s"), s);
+			align->position = position;
+		} else if (skip_prefix(s, "width=", &s)) {
+			if (strtoul_ui(s, 10, &width))
+				die(_("unrecognized width:%s"), s);
+		} else if (!strtoul_ui(s, 10, &width))
 			;
 		else if ((position = parse_align_position(s)) >= 0)
 			align->position = position;
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index fe4796c..bcf472b 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -133,6 +133,48 @@ test_expect_success 'right alignment' '
 	test_cmp expect actual
 '
 
+cat >expect <<-\EOF
+|       refname is refs/heads/master       |refs/heads/master
+|        refname is refs/heads/side        |refs/heads/side
+|         refname is refs/odd/spot         |refs/odd/spot
+|     refname is refs/tags/double-tag      |refs/tags/double-tag
+|        refname is refs/tags/four         |refs/tags/four
+|         refname is refs/tags/one         |refs/tags/one
+|     refname is refs/tags/signed-tag      |refs/tags/signed-tag
+|        refname is refs/tags/three        |refs/tags/three
+|         refname is refs/tags/two         |refs/tags/two
+EOF
+
+test_align_permutations() {
+	while read -r option
+	do
+		test_expect_success "align:$option" '
+			git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
+			test_cmp expect actual
+		'
+	done
+}
+
+test_align_permutations <<-\EOF
+	middle,42
+	42,middle
+	position=middle,42
+	42,position=middle
+	middle,width=42
+	width=42,middle
+	position=middle,width=42
+	width=42,position=middle
+EOF
+
+# Last one wins (silently) when multiple arguments of the same type are given
+
+test_align_permutations <<-\EOF
+	32,width=42,middle
+	width=30,42,middle
+	width=42,position=right,middle
+	42,right,position=middle
+EOF
+
 # Individual atoms inside %(align:...) and %(end) must not be quoted.
 
 test_expect_success 'alignment with format quote' "
-- 
2.7.1
Previous: Karthik NayakNext: Karthik Nayak
Message 29 of 32 in “ref-filter: use parsing functions”
  1. 00/12 ref-filter: use parsing functionsKarthik Nayak, Feb 16, 2016
  2. 01/12 strbuf: introduce strbuf_split_str_omit_term()Karthik Nayak, Feb 16, 2016
  3. 02/12 ref-filter: use strbuf_split_str_omit_term()Karthik Nayak, Feb 16, 2016
  4. Jeff KingFeb 16, 2016
  5. Jeff KingFeb 16, 2016
  6. Eric SunshineFeb 16, 2016
  7. Jeff KingFeb 16, 2016
  8. Eric SunshineFeb 16, 2016
  9. Jeff KingFeb 16, 2016
  10. Eric SunshineFeb 16, 2016
  11. Jeff KingFeb 16, 2016
  12. Junio C HamanoFeb 17, 2016
  13. Jeff KingFeb 17, 2016
  14. Junio C HamanoFeb 17, 2016
  15. Jeff KingFeb 17, 2016
  16. Junio C HamanoFeb 17, 2016
  17. Karthik NayakFeb 17, 2016
  18. Eric SunshineFeb 17, 2016
  19. Karthik NayakFeb 17, 2016
  20. Eric SunshineFeb 17, 2016
  21. Karthik NayakFeb 17, 2016
  22. Karthik NayakFeb 17, 2016
  23. 03/12 ref-filter: bump 'used_atom' and related code to the topKarthik Nayak, Feb 16, 2016
  24. 04/12 ref-filter: introduce struct used_atomKarthik Nayak, Feb 16, 2016
  25. 05/12 ref-filter: introduce parsing functions for each valid atomKarthik Nayak, Feb 16, 2016
  26. 06/12 ref-filter: introduce color_atom_parser()Karthik Nayak, Feb 16, 2016
  27. 07/12 ref-filter: introduce parse_align_position()Karthik Nayak, Feb 16, 2016
  28. 08/12 ref-filter: introduce align_atom_parser()Karthik Nayak, Feb 16, 2016
  29. 09/12 ref-filter: align: introduce long-form syntaxKarthik Nayak, Feb 16, 2016
  30. 10/12 ref-filter: introduce remote_ref_atom_parser()Karthik Nayak, Feb 16, 2016
  31. 11/12 ref-filter: introduce contents_atom_parser()Karthik Nayak, Feb 16, 2016
  32. 12/12 ref-filter: introduce objectname_atom_parser()Karthik Nayak, Feb 16, 2016

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.