progress: narrower time display, multiple fixes

- Each time field is now 7 characters wide, so that the total width
  never exceeds 79 columns so that it works correctly also in Windows
  terminals. The title lines are adjusted accordingly.

  This is accomplished by using h:mm:ss style up to 10 hours, and for
  longer periods switch to "nnX nnY" style output. For hours, days,
  months and years.

  For less than one hour, the hour field is now dropped.

  When no time info is provided, the field is now space-only. No more
  `-:--:--`.

  Also fixed the output for really long times which previously was
  completely broken. The largest time now shows as ">99999y". (Becase
  I can't figure out a better way).

- For sizes, the widths are now properly fixed to 6 characters. When
  displaying a unit with less than 3 digits, it shows two decimal
  precision like "16777215 => 15.99M" and one decmal otherwise: "262143
  => 255.9k"

  Also fixes the decimal math. 131071 is 127.9k, which it previously did
  not show.

- The time and size field outputs are now properly verified in test
  1636.

Fixes #20122
Closes #20173

fixup use only space when no time exists

Drop the hour from the display when zero
This commit is contained in:
Daniel Stenberg 2026-01-03 12:16:06 +01:00
parent 3c8f9c9247
commit 193397bf4e
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 296 additions and 28 deletions

View file

@ -34,38 +34,62 @@
#define MIN_RATE_LIMIT_PERIOD 3000
#ifndef CURL_DISABLE_PROGRESS_METER
/* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
byte) */
static void time2str(char *r, size_t rsize, curl_off_t seconds)
/* Provide a string that is 7 letters long (plus the zero byte).
Unit test 1636.
*/
UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds);
UNITTEST void time2str(char *r, size_t rsize, curl_off_t seconds)
{
curl_off_t h;
if(seconds <= 0) {
curlx_strcopy(r, rsize, "--:--:--", 8);
curlx_strcopy(r, rsize, " ", 7);
return;
}
h = seconds / 3600;
if(h <= 99) {
curl_off_t m = (seconds - (h * 3600)) / 60;
curl_off_t s = (seconds - (h * 3600)) - (m * 60);
curl_msnprintf(r, rsize, "%2" FMT_OFF_T ":%02" FMT_OFF_T ":%02" FMT_OFF_T,
h, m, s);
if(h <= 9) {
curl_off_t s = (seconds - (h * 3600)) - (m * 60);
if(h)
curl_msnprintf(r, rsize, "%" FMT_OFF_T ":%02" FMT_OFF_T ":"
"%02" FMT_OFF_T, h, m, s);
else
curl_msnprintf(r, rsize, " %02" FMT_OFF_T ":%02" FMT_OFF_T, m, s);
}
else
curl_msnprintf(r, rsize, "%" FMT_OFF_T "h %02" FMT_OFF_T "m", h, m);
}
else {
/* this equals to more than 99 hours, switch to a more suitable output
format to fit within the limits. */
curl_off_t d = seconds / 86400;
h = (seconds - (d * 86400)) / 3600;
if(d <= 999)
curl_msnprintf(r, rsize, "%3" FMT_OFF_T "d %02" FMT_OFF_T "h", d, h);
else
curl_msnprintf(r, rsize, "%7" FMT_OFF_T "d", d);
if(d <= 99)
curl_msnprintf(r, rsize, "%2" FMT_OFF_T "d %02" FMT_OFF_T "h", d, h);
else if(d <= 999)
curl_msnprintf(r, rsize, "%6" FMT_OFF_T "d", d);
else { /* more than 999 days */
curl_off_t m = d / 30;
if(m <= 999)
curl_msnprintf(r, rsize, "%6" FMT_OFF_T "m", m);
else { /* more than 999 months */
curl_off_t y = d / 365;
if(y <= 99999)
curl_msnprintf(r, rsize, "%6" FMT_OFF_T "y", y);
else
curlx_strcopy(r, rsize, ">99999y", 7);
}
}
}
}
/* The point of this function would be to return a string of the input data,
but never longer than 6 columns (+ one zero byte).
Add suffix k, M, G when suitable... */
static char *max6out(curl_off_t bytes, char *max6, size_t mlen)
Add suffix k, M, G when suitable...
Unit test 1636
*/
UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen);
UNITTEST char *max6out(curl_off_t bytes, char *max6, size_t mlen)
{
/* a signed 64-bit value is 8192 petabytes maximum, shown as
8.0E (exabytes)*/
@ -75,6 +99,7 @@ static char *max6out(curl_off_t bytes, char *max6, size_t mlen)
const char unit[] = { 'k', 'M', 'G', 'T', 'P', 'E', 0 };
int k = 0;
curl_off_t nbytes;
curl_off_t rest;
do {
nbytes = bytes / 1024;
if(nbytes < 1000)
@ -83,10 +108,17 @@ static char *max6out(curl_off_t bytes, char *max6, size_t mlen)
k++;
DEBUGASSERT(unit[k]);
} while(unit[k]);
/* xxx.yU */
curl_msnprintf(max6, mlen, "%3" CURL_FORMAT_CURL_OFF_T
".%" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
(bytes % 1024) / (1024 / 10), unit[k]);
rest = bytes % 1024;
if(nbytes <= 99)
/* xx.yyU */
curl_msnprintf(max6, mlen, "%2" CURL_FORMAT_CURL_OFF_T
".%02" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
rest * 100 / 1024, unit[k]);
else
/* xxx.yU */
curl_msnprintf(max6, mlen, "%3" CURL_FORMAT_CURL_OFF_T
".%" CURL_FORMAT_CURL_OFF_T "%c", nbytes,
rest * 10 / 1024, unit[k]);
}
return max6;
}
@ -501,9 +533,9 @@ static void progress_meter(struct Curl_easy *data)
curl_off_t total_cur_size;
curl_off_t total_expected_size;
curl_off_t dl_size;
char time_left[10];
char time_total[10];
char time_spent[10];
char time_left[8];
char time_total[8];
char time_spent[8];
curl_off_t cur_secs = (curl_off_t)p->timespent / 1000000; /* seconds */
if(!p->headers_out) {
@ -514,9 +546,9 @@ static void progress_meter(struct Curl_easy *data)
}
curl_mfprintf(data->set.err,
" %% Total %% Received %% Xferd Average Speed "
"Time Time Time Current\n"
"Time Time Time Current\n"
" Dload Upload "
"Total Spent Left Speed\n");
"Total Spent Left Speed\n");
p->headers_out = TRUE; /* headers are shown */
}
@ -567,9 +599,9 @@ static void progress_meter(struct Curl_easy *data)
sizeof(max6[3])), /* avrg dl speed */
max6out(p->ul.speed, max6[4],
sizeof(max6[4])), /* avrg ul speed */
time_total, /* 8 letters */ /* total time */
time_spent, /* 8 letters */ /* time spent */
time_left, /* 8 letters */ /* time left */
time_total, /* 7 letters */ /* total time */
time_spent, /* 7 letters */ /* time spent */
time_left, /* 7 letters */ /* time left */
max6out(p->current_speed, max6[5],
sizeof(max6[5])) /* current speed */
);