Skip to content

Commit

Permalink
remote: align --verbose output with spaces
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
louiswpf committed Dec 14, 2024
1 parent 2ccc89b commit 25bcdd1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 26 additions & 4 deletions builtin/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions t/t5505-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25bcdd1

Please sign in to comment.