From: Junio C Hamano <gitster@pobox.com>
To: "Wang Bing-hua via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, shejialuo <shejialuo@gmail.com>,
Wang Bing-hua <louiswpf@gmail.com>
Subject: Re: [PATCH v2] remote: align --verbose output with spaces
Date: Tue, 17 Dec 2024 12:47:19 -0800 [thread overview]
Message-ID: <xmqq4j32yr3c.fsf@gitster.g> (raw)
In-Reply-To: <pull.1837.v2.git.1734455884405.gitgitgadget@gmail.com> (Wang Bing-hua via GitGitGadget's message of "Tue, 17 Dec 2024 17:18:04 +0000")
"Wang Bing-hua via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 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.
While I am still not convinced if this change is a good idea (see
my earlier comment in a separate message)...
> +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);
I wonder if it is a better idea to extend get_one_entry() interface
to take not just a string_list but something like
struct remotes_data {
int maxwidth;
struct string_list *list_of_remotes;
};
if we think it is a good idea to give richer output to show_all()
function (instead of keep it spartan and compatible for the sake of
not breaking machine readers). There may be things other than
maxwidth that future changes to "git remote [-v]" may find needed.
And with such a change, you do not need a separate iteration over
the list of remotes just to call calc_maxwidth() callback. Keeping
a tally of "max length we have seen" inside get_one_entry() regardless
of "--verbose" setting shouldn't be too costly and help reduce the
complexity of the code.
> 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) {
Use of for_each_string_list_item() instead of a manual iteration is
probably a good idea here. If this were a larger change, that may
deserve to be a preparatory step on its own, but it is probably OK
to do so in the same patch.
> + 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);
Wouldn't it work to just do (totally untested code snippet below;
may have off-by-one around maxwidth)
printf("%.*s%s", maxwidth, item->string,
item->util ? "" : item->util);
without using any strbuf operation?
next prev parent reply other threads:[~2024-12-17 20:47 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 ` [PATCH v2] " Wang Bing-hua via GitGitGadget
2024-12-17 20:47 ` Junio C Hamano [this message]
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=xmqq4j32yr3c.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).