[PATCH] remote: align columns on -v

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

 



Currently, git remote -v produces a misaligned output when some remote
names are shorter and some are longer than a tab step. For example:

giuseppe	https://github.com/giuseppe/runc (fetch)
giuseppe	https://github.com/giuseppe/runc (push)
kir	git@xxxxxxxxxx:kolyshkin/runc.git (fetch)
kir	git@xxxxxxxxxx:kolyshkin/runc.git (push)
lifubang	https://github.com/lifubang/runc (fetch)
lifubang	https://github.com/lifubang/runc (push)
marquiz	https://github.com/marquiz/runc (fetch)
marquiz	https://github.com/marquiz/runc (push)

Let's find the maximum width of remote and use it for alignment.
With this change in place, the output looks like this now:

giuseppe  https://github.com/giuseppe/runc (fetch)
giuseppe  https://github.com/giuseppe/runc (push)
kir       git@xxxxxxxxxx:kolyshkin/runc.git (fetch)
kir       git@xxxxxxxxxx:kolyshkin/runc.git (push)
lifubang  https://github.com/lifubang/runc (fetch)
lifubang  https://github.com/lifubang/runc (push)
marquiz   https://github.com/marquiz/runc (fetch)
marquiz   https://github.com/marquiz/runc (push)

[v2: use utf8_strwidth and padding, fix description]

Reported-by: Roman Dodin <dodin.roman@xxxxxxxxx>
Signed-off-by: Kir Kolyshkin <kolyshkin@xxxxxxxxx>
---
 builtin/remote.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 729f6f3643..654472a87c 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -13,6 +13,7 @@
 #include "strvec.h"
 #include "commit-reach.h"
 #include "progress.h"
+#include "utf8.h"
 
 static const char * const builtin_remote_usage[] = {
 	"git remote [-v | --verbose]",
@@ -1245,14 +1246,26 @@ static int show_all(void)
 	result = for_each_remote(get_one_entry, &list);
 
 	if (!result) {
-		int i;
+		int i, width = 7;
+
+		if (verbose) {
+			for (i = 0; i < list.nr; i++) {
+				int w = utf8_strwidth((list.items + i)->string);
+				if (w > width)
+					width = w;
+			}
+		}
 
 		string_list_sort(&list);
 		for (i = 0; i < list.nr; i++) {
 			struct string_list_item *item = list.items + i;
-			if (verbose)
-				printf("%s\t%s\n", item->string,
+			if (verbose) {
+				int padding = width - utf8_strwidth(item->string);
+				if (padding < 0)
+					padding = 0;
+				printf("%*s%s %s\n", padding, "", item->string,
 					item->util ? (const char *)item->util : "");
+			}
 			else {
 				if (i && !strcmp((item - 1)->string, item->string))
 					continue;
-- 
2.39.0




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

  Powered by Linux