From: Aaron Merey Date: Thu, 19 May 2022 23:08:08 +0000 (-0400) Subject: Add cur, total to progress bar X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d59d427f1f63a3200db16682ebfa8bf462a13f10;p=thirdparty%2Fbinutils-gdb.git Add cur, total to progress bar --- diff --git a/gdb/cli-out.c b/gdb/cli-out.c index 51531d9eda2..3e5af160b30 100644 --- a/gdb/cli-out.c +++ b/gdb/cli-out.c @@ -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 diff --git a/gdb/cli-out.h b/gdb/cli-out.h index 62531ce818d..c162848250e 100644 --- a/gdb/cli-out.h +++ b/gdb/cli-out.h @@ -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 () diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c index 92aca018954..77e2839cb8f 100644 --- a/gdb/debuginfod-support.c +++ b/gdb/debuginfod-support.c @@ -110,23 +110,51 @@ struct debuginfod_client_deleter using debuginfod_client_up = std::unique_ptr; + /* 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); diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c index 6b3e932ffa4..ee0f4db5341 100644 --- a/gdb/mi/mi-out.c +++ b/gdb/mi/mi-out.c @@ -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 ()); diff --git a/gdb/mi/mi-out.h b/gdb/mi/mi-out.h index 1cc1cc1f74e..8298e06ad46 100644 --- a/gdb/mi/mi-out.h +++ b/gdb/mi/mi-out.h @@ -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 { diff --git a/gdb/ui-out.h b/gdb/ui-out.h index 21e7a2bfbe7..e2e5e6f995f 100644 --- a/gdb/ui-out.h +++ b/gdb/ui-out.h @@ -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. */