Skip to content

Commit 960a18e

Browse files
committed
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 <louiswpf@gmail.com>
1 parent 2ccc89b commit 960a18e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

builtin/remote.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "strvec.h"
1717
#include "commit-reach.h"
1818
#include "progress.h"
19+
#include "utf8.h"
1920

2021
static const char * const builtin_remote_usage[] = {
2122
"git remote [-v | --verbose]",
@@ -1279,6 +1280,20 @@ static int get_one_entry(struct remote *remote, void *priv)
12791280
return 0;
12801281
}
12811282

1283+
static int calc_maxwidth(struct string_list *list)
1284+
{
1285+
int max = 0;
1286+
1287+
for (int i = 0; i < list->nr; i++) {
1288+
struct string_list_item *item = list->items + i;
1289+
int w = utf8_strwidth(item->string);
1290+
1291+
if (w > max)
1292+
max = w;
1293+
}
1294+
return max;
1295+
}
1296+
12821297
static int show_all(void)
12831298
{
12841299
struct string_list list = STRING_LIST_INIT_DUP;
@@ -1292,10 +1307,17 @@ static int show_all(void)
12921307
string_list_sort(&list);
12931308
for (i = 0; i < list.nr; i++) {
12941309
struct string_list_item *item = list.items + i;
1295-
if (verbose)
1296-
printf("%s\t%s\n", item->string,
1297-
item->util ? (const char *)item->util : "");
1298-
else {
1310+
if (verbose) {
1311+
struct strbuf s = STRBUF_INIT;
1312+
1313+
strbuf_utf8_align(&s, ALIGN_LEFT,
1314+
calc_maxwidth(&list) + 1,
1315+
item->string);
1316+
if (item->util)
1317+
strbuf_addstr(&s, item->util);
1318+
printf("%s\n", s.buf);
1319+
strbuf_release(&s);
1320+
} else {
12991321
if (i && !strcmp((item - 1)->string, item->string))
13001322
continue;
13011323
printf("%s\n", item->string);

t/t5505-remote.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ test_expect_success 'without subcommand' '
249249

250250
test_expect_success 'without subcommand accepts -v' '
251251
cat >expect <<-EOF &&
252-
origin $(pwd)/one (fetch)
253-
origin $(pwd)/one (push)
252+
origin $(pwd)/one (fetch)
253+
origin $(pwd)/one (push)
254254
EOF
255255
git -C test remote -v >actual &&
256256
test_cmp expect actual

0 commit comments

Comments
 (0)