]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add cur, total to progress bar
authorAaron Merey <amerey@redhat.com>
Thu, 19 May 2022 23:08:08 +0000 (19:08 -0400)
committerAaron Merey <amerey@redhat.com>
Thu, 19 May 2022 23:08:08 +0000 (19:08 -0400)
gdb/cli-out.c
gdb/cli-out.h
gdb/debuginfod-support.c
gdb/mi/mi-out.c
gdb/mi/mi-out.h
gdb/ui-out.h

index 51531d9eda23feb3cf3e4eba0d4375515801038e..3e5af160b305f1825e7fb8e14c62c3bde07d746c 100644 (file)
@@ -293,22 +293,24 @@ cli_ui_out::do_progress_start ()
 */
 
 void
-cli_ui_out::do_progress_notify (const std::string &msg, double howmuch)
+cli_ui_out::do_progress_notify (const std::string &msg,
+                               const std::string &size,
+                               double cur, double total)
 {
   struct ui_file *stream = m_streams.back ();
   cli_progress_info &info (m_progress_info.back ());
 
   if (info.state == progress_update::START)
     {
-      if (!stream->isatty ())
+      if (stream->isatty ())
        {
-         gdb_printf (stream, "%s...\n", msg.c_str ());
-         info.state = progress_update::WORKING;
+         gdb_printf (stream, "%s", msg.c_str ());
+         info.state = progress_update::BAR;
        }
       else
        {
-         gdb_printf (stream, "%s\n", msg.c_str ());
-         info.state = progress_update::BAR;
+         gdb_printf (stream, "%s...\n", msg.c_str ());
+         info.state = progress_update::WORKING;
        }
     }
 
@@ -321,15 +323,25 @@ cli_ui_out::do_progress_notify (const std::string &msg, double howmuch)
       || !stream->isatty ())
     return;
 
-  if (howmuch >= 0)
+  double howmuch = cur / total;
+  if (howmuch >= 0 && howmuch <= 1.0)
     {
-      int width = chars_per_line - 3;
+      std::string progress = string_printf (" %.02f %s / %.02f %s",
+                                           cur, size.c_str (),
+                                           total, size.c_str ());
+      int width = chars_per_line - progress.size () - 3;
       int max = width * howmuch;
 
-      gdb_printf (stream, "\r[");
+      std::string display = "\r[";
+
       for (int i = 0; i < width; ++i)
-       gdb_printf (stream, i < max ? "#" : " ");
-      gdb_printf (stream, "]");
+       if (i < max)
+         display += "#";
+       else
+         display += " ";
+
+      display += "]" + progress;
+      gdb_printf (stream, "%s", display.c_str ());
       gdb_flush (stream);
     }
   else
index 62531ce818d8b339b6b5534013e1708a7db1d357..c162848250e239f0ea6e4cd8e651bcf0beccbf2b 100644 (file)
@@ -73,7 +73,8 @@ protected:
   virtual void do_redirect (struct ui_file *outstream) override;
 
   virtual void do_progress_start () override;
-  virtual void do_progress_notify (const std::string &, double) override;
+  virtual void do_progress_notify (const std::string &, const std::string &,
+                                  double, double) override;
   virtual void do_progress_end () override;
 
   bool suppress_output ()
index 92aca018954c0870ebb7799539ef63e87d3f92a5..77e2839cb8ffade558e5dc8916cd38c440f5f797 100644 (file)
@@ -110,23 +110,51 @@ struct debuginfod_client_deleter
 using debuginfod_client_up
   = std::unique_ptr<debuginfod_client, debuginfod_client_deleter>;
 
+
 /* Convert SIZE into a unit suitable for use with progress updates.
-   SIZE should in given in bytes and will be converted into KB or MB.
-   UNIT will be set to "KB" or "MB" accordingly.  */
+   SIZE should in given in bytes and will be converted into KB, MB, GB
+   or remain unchanged. UNIT will be set to "B", "KB", "MB" or "GB"
+   accordingly.  */
 
 static void
-get_size_and_unit (double *size, const char **unit)
+get_size_and_unit (double &size, std::string &unit)
 {
-  *size /= 1024;
+  if (size < 10.24)
+    {
+      /* If size is less than 0.01 KB then set unit to B.  */
+      unit = "B";
+      return;
+    }
 
-  /* If size is greater than 0.01 MB then set unit to MB.  */
-  if (*size > 10.24)
+  size /= 1024;
+  if (size < 10.24)
     {
-      *size /= 1024;
-      *unit = "MB";
+      /* If size is less than 0.01 MB then set unit to KB.  */
+      unit = "KB";
+      return;
     }
-  else
-    *unit = "KB";
+
+  size /= 1024;
+  if (size < 10.24)
+    {
+      /* If size is less than 0.01 GB then set unit to MB.  */
+      unit = "MB";
+      return;
+    }
+
+  size /= 1024;
+  unit = "GB";
+}
+
+static void
+convert_to_unit (double &size, const std::string &unit)
+{
+  if (unit == "KB")
+    size /= 1024;
+  else if (unit == "MB")
+    size /= 1024 * 1024;
+  else if (unit == "GB")
+    size /= 1024 * 1024 * 1024;
 }
 
 static int
@@ -141,9 +169,8 @@ progressfn (debuginfod_client *c, long cur, long total)
 
   if (check_quit_flag ())
     {
-      current_uiout->do_progress_end (); ///?
-
-      gdb_printf ("Cancelling download of %s %ps...\n",
+      //current_uiout->do_progress_end (); ///?
+      gdb_printf ("Cancelling download of %s %s...\n",
                  data->desc, styled_fname.c_str ());
       return 1;
     }
@@ -159,21 +186,23 @@ progressfn (debuginfod_client *c, long cur, long total)
 
       if (howmuch >= 0.0 && howmuch <= 1.0)
        {
-         double size = (double) total;
-         const char *unit = "";
+         double d_total = (double) total;
+          double d_cur = (double) cur;
+         std::string unit = "";
 
-         get_size_and_unit (&size, &unit);
+         get_size_and_unit (d_total, unit);
+         convert_to_unit (d_cur, unit);
          std::string msg = string_printf ("Downloading %0.2f %s %s %s\n",
-                                          size, unit, data->desc,
+                                          d_total, unit.c_str (), data->desc,
                                           styled_fname.c_str ());
-         current_uiout->update_progress (msg, howmuch);
+         data->progress.update_progress (msg, unit, d_cur, d_total);
          return 0;
        }
     }
 
   std::string msg = string_printf ("Downloading %s %s\n",
                                   data->desc, styled_fname.c_str ());
-  current_uiout->update_progress (msg, -1);
+  data->progress.update_progress (msg);
   return 0;
 }
 
@@ -277,10 +306,10 @@ print_outcome (user_data &data, int fd)
       if (fstat (fd, &s) == 0)
        {
          double size = (double)s.st_size;
-         const char *unit = "";
+         std::string unit = "";
 
-         get_size_and_unit (&size, &unit);
-         gdb_printf (_("Retrieved %.02f %s %s %s\n"), size, unit,
+         get_size_and_unit (size, unit);
+         gdb_printf (_("Retrieved %.02f %s %s %s\n"), size, unit.c_str (),
                      data.desc, styled_fname.c_str ());
        }
       else
@@ -403,12 +432,7 @@ debuginfod_exec_query (const unsigned char *build_id,
 
   scoped_fd fd (debuginfod_find_executable (c, build_id, build_id_len, &dname));
   debuginfod_set_user_data (c, nullptr);
-
-  if (fd.get () < 0 && fd.get () != -ENOENT)
-    gdb_printf (_("Download failed: %s. " \
-                 "Continuing without executable for %ps.\n"),
-               safe_strerror (-fd.get ()),
-               styled_string (file_name_style.style (),  filename));
+  print_outcome (data, fd.get ());
 
   if (fd.get () >= 0)
     destname->reset (dname);
index 6b3e932ffa4643de612499661cd779b36282af8c..ee0f4db5341df4dc81132643e71480b9c12ce064 100644 (file)
@@ -270,7 +270,8 @@ mi_ui_out::do_progress_start ()
 /* Indicate that a task described by NAME is in progress.  */
 
 void
-mi_ui_out::do_progress_notify (const std::string &msg, double howmuch)
+mi_ui_out::do_progress_notify (const std::string &msg, const std::string &unit,
+                              double cur, double total)
 {
   mi_progress_info &info (m_progress_info.back ());
 
index 1cc1cc1f74ee6d62829521bd1b44357c9f927875..8298e06ad46eec86b61a2d9e564c52fab08872b1 100644 (file)
@@ -83,7 +83,8 @@ protected:
   { return true; }
 
   virtual void do_progress_start () override;
-  virtual void do_progress_notify (const std::string &, double) override;
+  virtual void do_progress_notify (const std::string &, const std::string &,
+                                  double, double) override;
 
   virtual void do_progress_end () override
   {
index 21e7a2bfbe71786e5a5c871daa3307bff65bcf4b..e2e5e6f995fe8315b8477dd7ecd19999528cd6eb 100644 (file)
@@ -309,25 +309,24 @@ class ui_out
     progress_update (const progress_update &) = delete;
     progress_update &operator= (const progress_update &) = delete;
 
-    /* Emit some progress for this progress meter.  HOWMUCH may range
-       from 0.0 to 1.0.  */
-    void progress (const std::string& msg, double howmuch)
+    /* Emit some progress for this progress meter.  Includes current
+       amount of progress made and total amount in the display.  */
+    void update_progress (const std::string& msg, std::string& unit,
+                         double cur, double total)
     {
-      m_uiout->do_progress_notify (msg, howmuch);
+      m_uiout->do_progress_notify (msg, unit, cur, total);
     }
 
+    /* Emit some progress for this progress meter.  */
+    void update_progress (const std::string& msg)
+    {
+      m_uiout->do_progress_notify (msg, "", -1, -1);
+    }
   private:
 
     struct ui_out *m_uiout;
   };
 
-  /* Emit some progress corresponding to the most recently created
-     progress_update object.  */
-  void update_progress (std::string &msg, double howmuch)
-  {
-    do_progress_notify (msg, howmuch);
-  }
-
   virtual void do_progress_end () = 0;
 
  protected:
@@ -365,7 +364,8 @@ class ui_out
   virtual void do_redirect (struct ui_file *outstream) = 0;
 
   virtual void do_progress_start () = 0;
-  virtual void do_progress_notify (const std::string &, double) = 0;
+  virtual void do_progress_notify (const std::string &, const std::string &,
+                                  double, double) = 0;
 
   /* Set as not MI-like by default.  It is overridden in subclasses if
      necessary.  */