From 25bcdd10b49c2b68d2bfe43acdb5a3eb710e2170 Mon Sep 17 00:00:00 2001 From: Wang Bing-hua Date: Fri, 13 Nov 2020 18:42:05 +0800 Subject: [PATCH] remote: align --verbose output with spaces Remote names exceeding a tab width could cause misalignment. Align --verbose output with spaces instead of a tab. Signed-off-by: Wang Bing-hua --- builtin/remote.c | 30 ++++++++++++++++++++++++++---- t/t5505-remote.sh | 4 ++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/builtin/remote.c b/builtin/remote.c index 1ad3e70a6b438a..876274d9dca9ee 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; + + for (int i = 0; i < list->nr; i++) { + struct string_list_item *item = list->items + i; + 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; @@ -1292,10 +1307,17 @@ static int show_all(void) 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 (verbose) { + struct strbuf s = STRBUF_INIT; + + strbuf_utf8_align(&s, ALIGN_LEFT, + calc_maxwidth(&list) + 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)) continue; printf("%s\n", item->string); diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 08424e878e104c..6586f020f74391 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