]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curl: make the progress bar detect terminal width changes
authorDaniel Stenberg <daniel@haxx.se>
Fri, 16 Aug 2024 09:15:36 +0000 (11:15 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 16 Aug 2024 15:05:28 +0000 (17:05 +0200)
And up the widest supported bar to 400 columns.

Fixes #14565
Reported-by: lolbinarycat on github
Closes #14570

src/tool_cb_prg.c

index 4c3d3cdb5a0fb765ac1911f785112c78015ff251..df2e19aaa6372b2c008d74e89b7f6fb0d1d9bd84 100644 (file)
@@ -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;