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

[PATCH] remote: align columns on -v

From
Kir Kolyshkin <kolyshkin@gmail.com>
Date
Feb 7, 2023, 23:52 UTC
Message-ID
<20230207235238.1850757-1-kolyshkin@gmail.com>

Currently, git remote -v produces a misaligned output when a remote name is more than 8 characters long (i.e. longer than a tab step). Here's how it looks like:

giuseppe https://github.com/giuseppe/runc (fetch) giuseppe https://github.com/giuseppe/runc (push) kir git@github.com:kolyshkin/runc.git (fetch) kir git@github.com: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 and use it for alignment.

While at it, let's keep the \t in case some tools depend on it for parsing (there will still be trailing spaces in the remote name).

With this change, the output is like this now:

giuseppe https://github.com/giuseppe/runc (fetch) giuseppe https://github.com/giuseppe/runc (push) kir git@github.com:kolyshkin/runc.git (fetch) kir git@github.com: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)

Reported-by: Roman Dodin <dodin.roman@gmail.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
---
 builtin/remote.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 729f6f3643..116417574d 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1245,13 +1245,21 @@ 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 len = strlen((list.items + i)->string);
+				if (len > width)
+					width = len;
+			}
+		}
 
 		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,
+				printf("%-*s\t%s\n", width, item->string,
 					item->util ? (const char *)item->util : "");
 			else {
 				if (i && !strcmp((item - 1)->string, item->string))
-- 
2.39.0
Next: Torsten Bögershausen
Message 1 of 7 in “remote: align columns on -v”
  1. remote: align columns on -vKir Kolyshkin, Feb 7, 2023
  2. Torsten BögershausenFeb 8, 2023
  3. Junio C HamanoFeb 8, 2023
  4. Kirill KolyshkinFeb 9, 2023
  5. Junio C HamanoFeb 8, 2023
  6. Kirill KolyshkinFeb 9, 2023
  7. remote: align columns on -vKir Kolyshkin, Feb 9, 2023

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.