[PATCH 9/9] for-each-ref: support %(...:aligned) for left alignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 builtin/for-each-ref.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 1390da8..3240ca0 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -11,6 +11,7 @@
 #include "remote.h"
 #include "color.h"
 #include "branch.h"
+#include "utf8.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -733,7 +734,7 @@ static void populate_value(struct refinfo *ref)
 		/* look for "short" refname format */
 		if (formatp) {
 			formatp++;
-			if (!strcmp(formatp, "short"))
+			if (!prefixcmp(formatp, "short"))
 				refname = shorten_unambiguous_ref(refname,
 						      warn_ambiguous_refs);
 			else
@@ -994,7 +995,7 @@ static void show_refs(struct refinfo **refs, int maxcount,
 {
 	struct strbuf *sb;
 	const char *cp, *sp, *ep;
-	int i, atom;
+	int i, atom, aligned, max_length, len;
 
 	sb = xmalloc(sizeof(*sb) * maxcount);
 	for (i = 0; i < maxcount; i++) {
@@ -1012,8 +1013,26 @@ static void show_refs(struct refinfo **refs, int maxcount,
 				emit(sb + i, cp, sp);
 		}
 		atom = parse_atom(sp + 2, ep);
-		for (i = 0; i < maxcount; i++)
+		aligned = !suffixcmp(used_atom[atom], ":aligned");
+		for (i = 0, max_length = 0; aligned && i < maxcount; i++) {
+			struct atom_value *v;
+			get_value(refs[i], atom, &v);
+			len = utf8_strnwidth(v->s, -1, 1);
+			if (len > max_length)
+				max_length = len;
+		}
+		for (i = 0; i < maxcount; i++) {
+			int old_len = sb[i].len;
 			print_value(sb + i, refs[i], atom, quote_style);
+			if (aligned) {
+				len = max_length -
+					utf8_strnwidth(sb[i].buf + old_len, -1, 1);
+				while (len) {
+					strbuf_addch(sb + i, ' ');
+					len--;
+				}
+			}
+		}
 	}
 	if (*cp) {
 		sp = cp + strlen(cp);
-- 
1.8.2.83.gc99314b

--
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




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]