All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Wang Bing-hua via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: shejialuo <shejialuo@gmail.com>,
	Wang Bing-hua <louiswpf@gmail.com>,
	Wang Bing-hua <louiswpf@gmail.com>
Subject: [PATCH v2] remote: align --verbose output with spaces
Date: Tue, 17 Dec 2024 17:18:04 +0000	[thread overview]
Message-ID: <pull.1837.v2.git.1734455884405.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1837.git.1734439176360.gitgitgadget@gmail.com>

From: Wang Bing-hua <louiswpf@gmail.com>

Remote names exceeding a tab width could cause misalignment.
Align --verbose output with spaces instead of a tab.

Signed-off-by: Wang Bing-hua <louiswpf@gmail.com>
---
    remote: align --verbose output with spaces
    
    Changes in v2:
    
     * Use for_each_string_list_item() to traverse string lists.
     * Calculate the max width outside of the loop.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1837%2Flouiswpf%2Fremote-align-verbose-output-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1837/louiswpf/remote-align-verbose-output-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1837

Range-diff vs v1:

 1:  960a18efc36 ! 1:  648881dbf0d remote: align --verbose output with spaces
     @@ builtin/remote.c: static int get_one_entry(struct remote *remote, void *priv)
      +static int calc_maxwidth(struct string_list *list)
      +{
      +	int max = 0;
     ++	struct string_list_item *item;
      +
     -+	for (int i = 0; i < list->nr; i++) {
     -+		struct string_list_item *item = list->items + i;
     ++	for_each_string_list_item (item, list) {
      +		int w = utf8_strwidth(item->string);
      +
      +		if (w > max)
     @@ builtin/remote.c: static int get_one_entry(struct remote *remote, void *priv)
       {
       	struct string_list list = STRING_LIST_INIT_DUP;
      @@ builtin/remote.c: static int show_all(void)
     + 	result = for_each_remote(get_one_entry, &list);
     + 
     + 	if (!result) {
     +-		int i;
     ++		int maxwidth = 0;
     ++		struct string_list_item *item;
     + 
     ++		if (verbose)
     ++			maxwidth = calc_maxwidth(&list);
       		string_list_sort(&list);
     - 		for (i = 0; i < list.nr; i++) {
     - 			struct string_list_item *item = list.items + i;
     +-		for (i = 0; i < list.nr; i++) {
     +-			struct string_list_item *item = list.items + i;
      -			if (verbose)
      -				printf("%s\t%s\n", item->string,
      -					item->util ? (const char *)item->util : "");
      -			else {
     +-				if (i && !strcmp((item - 1)->string, item->string))
     ++		for_each_string_list_item (item, &list) {
      +			if (verbose) {
      +				struct strbuf s = STRBUF_INIT;
      +
     -+				strbuf_utf8_align(&s, ALIGN_LEFT,
     -+						  calc_maxwidth(&list) + 1,
     ++				strbuf_utf8_align(&s, ALIGN_LEFT, maxwidth + 1,
      +						  item->string);
      +				if (item->util)
      +					strbuf_addstr(&s, item->util);
      +				printf("%s\n", s.buf);
      +				strbuf_release(&s);
      +			} else {
     - 				if (i && !strcmp((item - 1)->string, item->string))
     ++				if (item != list.items &&
     ++				    !strcmp((item - 1)->string, item->string))
       					continue;
       				printf("%s\n", item->string);
     + 			}
      
       ## t/t5505-remote.sh ##
      @@ t/t5505-remote.sh: test_expect_success 'without subcommand' '


 builtin/remote.c  | 40 ++++++++++++++++++++++++++++++++--------
 t/t5505-remote.sh |  4 ++--
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/builtin/remote.c b/builtin/remote.c
index 1ad3e70a6b4..1e9106530c0 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -16,6 +16,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]",
@@ -1279,6 +1280,20 @@ static int get_one_entry(struct remote *remote, void *priv)
 	return 0;
 }
 
+static int calc_maxwidth(struct string_list *list)
+{
+	int max = 0;
+	struct string_list_item *item;
+
+	for_each_string_list_item (item, list) {
+		int w = utf8_strwidth(item->string);
+
+		if (w > max)
+			max = w;
+	}
+	return max;
+}
+
 static int show_all(void)
 {
 	struct string_list list = STRING_LIST_INIT_DUP;
@@ -1287,16 +1302,25 @@ static int show_all(void)
 	result = for_each_remote(get_one_entry, &list);
 
 	if (!result) {
-		int i;
+		int maxwidth = 0;
+		struct string_list_item *item;
 
+		if (verbose)
+			maxwidth = calc_maxwidth(&list);
 		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,
-					item->util ? (const char *)item->util : "");
-			else {
-				if (i && !strcmp((item - 1)->string, item->string))
+		for_each_string_list_item (item, &list) {
+			if (verbose) {
+				struct strbuf s = STRBUF_INIT;
+
+				strbuf_utf8_align(&s, ALIGN_LEFT, maxwidth + 1,
+						  item->string);
+				if (item->util)
+					strbuf_addstr(&s, item->util);
+				printf("%s\n", s.buf);
+				strbuf_release(&s);
+			} else {
+				if (item != list.items &&
+				    !strcmp((item - 1)->string, item->string))
 					continue;
 				printf("%s\n", item->string);
 			}
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 08424e878e1..6586f020f74 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -249,8 +249,8 @@ test_expect_success 'without subcommand' '
 
 test_expect_success 'without subcommand accepts -v' '
 	cat >expect <<-EOF &&
-	origin	$(pwd)/one (fetch)
-	origin	$(pwd)/one (push)
+	origin $(pwd)/one (fetch)
+	origin $(pwd)/one (push)
 	EOF
 	git -C test remote -v >actual &&
 	test_cmp expect actual

base-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086
-- 
gitgitgadget

  parent reply	other threads:[~2024-12-17 17:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 12:39 [PATCH] remote: align --verbose output with spaces Wang Bing-hua via GitGitGadget
2024-12-17 13:23 ` shejialuo
2024-12-17 15:24   ` Wang Bing-hua
2024-12-17 20:21   ` Junio C Hamano
2024-12-18  5:49     ` Wang Bing-hua
2024-12-18 13:52     ` shejialuo
2024-12-18 14:09       ` Wang Bing-hua
2024-12-17 17:18 ` Wang Bing-hua via GitGitGadget [this message]
2024-12-17 20:47   ` [PATCH v2] " Junio C Hamano
2024-12-18  8:37     ` Wang Bing-hua
2024-12-18 15:39       ` Junio C Hamano
2024-12-19  2:14         ` Wang Bing-hua

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1837.v2.git.1734455884405.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=louiswpf@gmail.com \
    --cc=shejialuo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.