From: Daniel Stenberg Date: Fri, 16 Aug 2024 09:15:36 +0000 (+0200) Subject: curl: make the progress bar detect terminal width changes X-Git-Tag: curl-8_10_0~230 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f562f744cfc2c0be89315105ffc0355d844aa79;p=thirdparty%2Fcurl.git curl: make the progress bar detect terminal width changes And up the widest supported bar to 400 columns. Fixes #14565 Reported-by: lolbinarycat on github Closes #14570 --- diff --git a/src/tool_cb_prg.c b/src/tool_cb_prg.c index 4c3d3cdb5a..df2e19aaa6 100644 --- a/src/tool_cb_prg.c +++ b/src/tool_cb_prg.c @@ -35,7 +35,8 @@ #include "memdebug.h" /* keep this as LAST include */ -#define MAX_BARLENGTH 256 +#define MAX_BARLENGTH 400 +#define MIN_BARLENGTH 20 /* 200 values generated by this perl code: @@ -119,6 +120,17 @@ static void fly(struct ProgressData *bar, bool moved) # define CURL_OFF_T_MAX CURL_OFF_T_C(0x7FFFFFFFFFFFFFFF) #endif +static void update_width(struct ProgressData *bar) +{ + int cols = get_terminal_columns(); + if(cols > MAX_BARLENGTH) + bar->width = MAX_BARLENGTH; + else if(cols > MIN_BARLENGTH) + bar->width = (int)cols; + else + bar->width = MIN_BARLENGTH; +} + int tool_progress_cb(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) @@ -172,6 +184,7 @@ int tool_progress_cb(void *clientp, if(tvdiff(now, bar->prevtime) < 100L) /* limit progress-bar updating to 10 Hz */ return 0; + update_width(bar); fly(bar, point != bar->prev); } } @@ -179,6 +192,7 @@ int tool_progress_cb(void *clientp, /* simply count invokes */ bar->calls++; + update_width(bar); if((total > 0) && (point != bar->prev)) { char line[MAX_BARLENGTH + 1]; char format[40]; @@ -223,7 +237,6 @@ int tool_progress_cb(void *clientp, void progressbarinit(struct ProgressData *bar, struct OperationConfig *config) { - unsigned int cols; memset(bar, 0, sizeof(struct ProgressData)); /* pass the resume from value through to the progress function so it can @@ -231,11 +244,7 @@ void progressbarinit(struct ProgressData *bar, if(config->use_resume) bar->initial_size = config->resume_from; - cols = get_terminal_columns(); - if(cols > MAX_BARLENGTH) - bar->width = MAX_BARLENGTH; - else if(cols > 20) - bar->width = (int)cols; + update_width(bar); bar->out = tool_stderr; bar->tick = 150;