Skip to content

Commit c3dc382

Browse files
committed
Merge branch 'nd/wt-status-align-i18n'
An attempt to automatically align the names in the "git status" output, taking the display width of (translated) section labels into account. * nd/wt-status-align-i18n: wt-status: take the alignment burden off translators
2 parents c17fa97 + 3651e45 commit c3dc382

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

wt-status.c

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "submodule.h"
1616
#include "column.h"
1717
#include "strbuf.h"
18+
#include "utf8.h"
1819

1920
static char default_wt_status_colors[][COLOR_MAXLEN] = {
2021
GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -264,6 +265,30 @@ static void wt_status_print_unmerged_data(struct wt_status *s,
264265
strbuf_release(&onebuf);
265266
}
266267

268+
static const char *wt_status_diff_status_string(int status)
269+
{
270+
switch (status) {
271+
case DIFF_STATUS_ADDED:
272+
return _("new file");
273+
case DIFF_STATUS_COPIED:
274+
return _("copied");
275+
case DIFF_STATUS_DELETED:
276+
return _("deleted");
277+
case DIFF_STATUS_MODIFIED:
278+
return _("modified");
279+
case DIFF_STATUS_RENAMED:
280+
return _("renamed");
281+
case DIFF_STATUS_TYPE_CHANGED:
282+
return _("typechange");
283+
case DIFF_STATUS_UNKNOWN:
284+
return _("unknown");
285+
case DIFF_STATUS_UNMERGED:
286+
return _("unmerged");
287+
default:
288+
return NULL;
289+
}
290+
}
291+
267292
static void wt_status_print_change_data(struct wt_status *s,
268293
int change_type,
269294
struct string_list_item *it)
@@ -276,6 +301,23 @@ static void wt_status_print_change_data(struct wt_status *s,
276301
const char *one, *two;
277302
struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
278303
struct strbuf extra = STRBUF_INIT;
304+
static char *padding;
305+
const char *what;
306+
int len;
307+
308+
if (!padding) {
309+
int width = 0;
310+
/* If DIFF_STATUS_* uses outside this range, we're in trouble */
311+
for (status = 'A'; status <= 'Z'; status++) {
312+
what = wt_status_diff_status_string(status);
313+
len = what ? strlen(what) : 0;
314+
if (len > width)
315+
width = len;
316+
}
317+
width += 2; /* colon and a space */
318+
padding = xmallocz(width);
319+
memset(padding, ' ', width);
320+
}
279321

280322
one_name = two_name = it->string;
281323
switch (change_type) {
@@ -307,34 +349,18 @@ static void wt_status_print_change_data(struct wt_status *s,
307349
two = quote_path(two_name, s->prefix, &twobuf);
308350

309351
status_printf(s, color(WT_STATUS_HEADER, s), "\t");
310-
switch (status) {
311-
case DIFF_STATUS_ADDED:
312-
status_printf_more(s, c, _("new file: %s"), one);
313-
break;
314-
case DIFF_STATUS_COPIED:
315-
status_printf_more(s, c, _("copied: %s -> %s"), one, two);
316-
break;
317-
case DIFF_STATUS_DELETED:
318-
status_printf_more(s, c, _("deleted: %s"), one);
319-
break;
320-
case DIFF_STATUS_MODIFIED:
321-
status_printf_more(s, c, _("modified: %s"), one);
322-
break;
323-
case DIFF_STATUS_RENAMED:
324-
status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
325-
break;
326-
case DIFF_STATUS_TYPE_CHANGED:
327-
status_printf_more(s, c, _("typechange: %s"), one);
328-
break;
329-
case DIFF_STATUS_UNKNOWN:
330-
status_printf_more(s, c, _("unknown: %s"), one);
331-
break;
332-
case DIFF_STATUS_UNMERGED:
333-
status_printf_more(s, c, _("unmerged: %s"), one);
334-
break;
335-
default:
352+
what = wt_status_diff_status_string(status);
353+
if (!what)
336354
die(_("bug: unhandled diff status %c"), status);
337-
}
355+
/* 1 for colon, which is not part of "what" */
356+
len = strlen(padding) - (utf8_strwidth(what) + 1);
357+
assert(len >= 0);
358+
if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
359+
status_printf_more(s, c, "%s:%.*s%s -> %s",
360+
what, len, padding, one, two);
361+
else
362+
status_printf_more(s, c, "%s:%.*s%s",
363+
what, len, padding, one);
338364
if (extra.len) {
339365
status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
340366
strbuf_release(&extra);

0 commit comments

Comments
 (0)