wt-status: extract the code to compute width for labels

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Dec 19, 2013 at 11:43 UTC 335e8250124aea055cbe4fb8a2268fa9e2f34439
1 file changed +15 -7
wt-status.c
+15 -7
@@ -292,6 +292,19 @@ static const char *wt_status_diff_status_string(int status)
292 }
293 }
294
295 +static int maxwidth(const char *(*label)(int), int minval, int maxval)
296 +{
297 + int result = 0, i;
298 +
299 + for (i = minval; i <= maxval; i++) {
300 + const char *s = label(i);
301 + int len = s ? utf8_strwidth(s) : 0;
302 + if (len > result)
303 + result = len;
304 + }
305 + return result;
306 +}
307 +
308 static void wt_status_print_change_data(struct wt_status *s,
309 int change_type,
310 struct string_list_item *it)
@@ -310,13 +323,8 @@ static void wt_status_print_change_data(struct wt_status *s,
323 int len;
324
325 if (!padding) {
313 - /* If DIFF_STATUS_* uses outside this range, we're in trouble */
314 - for (status = 'A'; status <= 'Z'; status++) {
315 - what = wt_status_diff_status_string(status);
316 - len = what ? strlen(what) : 0;
317 - if (len > label_width)
318 - label_width = len;
319 - }
326 + /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
327 + label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
328 label_width += strlen(" ");
329 padding = xmallocz(label_width);
330 memset(padding, ' ', label_width);