From: Andre Noll Date: Fri, 14 Sep 2007 19:59:32 +0000 (+0200) Subject: com_afs_ls(): Fix output width of duration. X-Git-Tag: v0.3.0~415 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=a7feb744f32ed508b05ce7a0c14265a24705a4a7 com_afs_ls(): Fix output width of duration. --- diff --git a/aft.c b/aft.c index 04846a76..d88be97e 100644 --- a/aft.c +++ b/aft.c @@ -600,25 +600,27 @@ static int get_local_time(uint64_t *seconds, char *buf, size_t size) } \ } -static short unsigned get_duration(int seconds_total, char *buf, short unsigned max_width) +static short unsigned get_duration_width(int seconds) { short unsigned width; - int s = seconds_total; - unsigned hours = s / 3600, mins = (s % 3600) / 60, secs = s % 60; - - if (s < 3600) { /* less than one hour => m:ss or mm:ss */ - GET_NUM_DIGITS(mins, &width); /* 1 or 2 */ - width += 3; /* 4 or 5 */ - if (buf) - sprintf(buf, "%*u:%02u", max_width - width + 1, mins, secs); - return width; - } + unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60; + + if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */ + return 4 + (mins > 9); /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */ GET_NUM_DIGITS(hours, &width); - width += 6; - if (buf) - sprintf(buf, "%*u:%02u:%02u", max_width - width + 1, hours, mins, secs); - return width; + return width + 6; +} + +static void get_duration_buf(int seconds, char *buf, short unsigned max_width) +{ + unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60; + + if (!hours) /* m:ss or mm:ss */ + sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60); + else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */ + sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins, + seconds % 60); } static char *make_attribute_line(const char *att_bitmap, struct afs_info *afsi) @@ -673,7 +675,7 @@ static int print_list_item(struct ls_data *d, struct ls_options *opts, sizeof(last_played_time)); if (ret < 0) return ret; - get_duration(afhi->seconds_total, duration_buf, w->duration_width); + get_duration_buf(afhi->seconds_total, duration_buf, w->duration_width); if (have_score) { if (opts->mode == LS_MODE_LONG) sprintf(score_buf, "%*li ", w->score_width, d->score); @@ -941,8 +943,8 @@ static int prepare_ls_row(struct osl_row *row, void *ls_opts) w->frequency_width = PARA_MAX(w->frequency_width, num_digits); GET_NUM_DIGITS(d->afsi.num_played, &num_digits); w->num_played_width = PARA_MAX(w->num_played_width, num_digits); - /* just get the number of chars to print this amount of time */ - tmp = get_duration(d->afhi.seconds_total, NULL, 0); + /* get the number of chars to print this amount of time */ + tmp = get_duration_width(d->afhi.seconds_total); w->duration_width = PARA_MAX(w->duration_width, tmp); if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) { GET_NUM_DIGITS(score, &num_digits);