Skip to content

Commit 8d5ef78

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 063bceb commit 8d5ef78

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

builtin/remote.c

Lines changed: 28 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+
struct string_list_item *item;
1287+
1288+
for_each_string_list_item (item, list) {
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;
@@ -1288,14 +1303,23 @@ static int show_all(void)
12881303

12891304
if (!result) {
12901305
int i;
1306+
int maxwidth = 0;
12911307

12921308
string_list_sort(&list);
1309+
if (verbose)
1310+
maxwidth = calc_maxwidth(&list);
12931311
for (i = 0; i < list.nr; i++) {
12941312
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 {
1313+
if (verbose) {
1314+
struct strbuf s = STRBUF_INIT;
1315+
1316+
strbuf_utf8_align(&s, ALIGN_LEFT, maxwidth + 1,
1317+
item->string);
1318+
if (item->util)
1319+
strbuf_addstr(&s, item->util);
1320+
printf("%s\n", s.buf);
1321+
strbuf_release(&s);
1322+
} else {
12991323
if (i && !strcmp((item - 1)->string, item->string))
13001324
continue;
13011325
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)