Skip to content

Commit

Permalink
wt-status: extract the code to compute width for labels
Browse files Browse the repository at this point in the history
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jonathan Nieder authored and Junio C Hamano committed Mar 12, 2014
1 parent d52cb57 commit 335e825
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions wt-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ static const char *wt_status_diff_status_string(int status)
}
}

static int maxwidth(const char *(*label)(int), int minval, int maxval)
{
int result = 0, i;

for (i = minval; i <= maxval; i++) {
const char *s = label(i);
int len = s ? utf8_strwidth(s) : 0;
if (len > result)
result = len;
}
return result;
}

static void wt_status_print_change_data(struct wt_status *s,
int change_type,
struct string_list_item *it)
Expand All @@ -310,13 +323,8 @@ static void wt_status_print_change_data(struct wt_status *s,
int len;

if (!padding) {
/* If DIFF_STATUS_* uses outside this range, we're in trouble */
for (status = 'A'; status <= 'Z'; status++) {
what = wt_status_diff_status_string(status);
len = what ? strlen(what) : 0;
if (len > label_width)
label_width = len;
}
/* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
label_width += strlen(" ");
padding = xmallocz(label_width);
memset(padding, ' ', label_width);
Expand Down

0 comments on commit 335e825

Please sign in to comment.