{"thread":{"id":"7759","subject":"[PATCH 2/3] provide a facility for \"delayed\" progress reporting","startedAt":"2007-04-20T19:05:27Z","lastAt":"2007-04-20T19:05:27Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"40003","messageId":"S1767212AbXDTTaU/20070420193020Z+531@vger.kernel.org","threadId":"7759","inReplyTo":null,"subject":"[PATCH 2/3] provide a facility for \"delayed\" progress reporting","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2007-04-20T19:05:27Z","receivedAt":"2007-04-20T19:05:27Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"This allows for progress to be displayed only if the progress has not\nreached a specified percentage treshold within a given delay in seconds.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n progress.c |   34 ++++++++++++++++++++++++++++++++++\n progress.h |    6 ++++++\n 2 files changed, 40 insertions(+), 0 deletions(-)\n\ndiff --git a/progress.c b/progress.c\nindex 478134b..05f7890 100644\n--- a/progress.c\n+++ b/progress.c\n@@ -13,6 +13,8 @@ static void set_progress_signal(void)\n \tstruct sigaction sa;\n \tstruct itimerval v;\n \n+\tprogress_update = 0;\n+\n \tmemset(&sa, 0, sizeof(sa));\n \tsa.sa_handler = progress_interval;\n \tsigemptyset(&sa.sa_mask);\n@@ -35,6 +37,24 @@ static void clear_progress_signal(void)\n \n int display_progress(struct progress *progress, unsigned n)\n {\n+\tif (progress->delay) {\n+\t\tchar buf[80];\n+\t\tif (!progress_update || --progress->delay)\n+\t\t\treturn 0;\n+\t\tif (progress->total) {\n+\t\t\tunsigned percent = n * 100 / progress->total;\n+\t\t\tif (percent > progress->delayed_percent_treshold) {\n+\t\t\t\t/* inhibit this progress report entirely */\n+\t\t\t\tclear_progress_signal();\n+\t\t\t\tprogress->delay = -1;\n+\t\t\t\tprogress->total = 0;\n+\t\t\t\treturn 0;\n+\t\t\t}\n+\t\t}\n+\t\tif (snprintf(buf, sizeof(buf),\n+\t\t\t     progress->delayed_title, progress->total))\n+\t\t\tfprintf(stderr, \"%s\\n\", buf);\n+\t}\n \tif (progress->total) {\n \t\tunsigned percent = n * 100 / progress->total;\n \t\tif (percent != progress->last_percent || progress_update) {\n@@ -59,11 +79,25 @@ void start_progress(struct progress *progress, const char *title,\n \tprogress->prefix = prefix;\n \tprogress->total = total;\n \tprogress->last_percent = -1;\n+\tprogress->delay = 0;\n \tif (snprintf(buf, sizeof(buf), title, total))\n \t\tfprintf(stderr, \"%s\\n\", buf);\n \tset_progress_signal();\n }\n \n+void start_progress_delay(struct progress *progress, const char *title,\n+\t\t\t  const char *prefix, unsigned total,\n+\t\t\t  unsigned percent_treshold, unsigned delay)\n+{\n+\tprogress->prefix = prefix;\n+\tprogress->total = total;\n+\tprogress->last_percent = -1;\n+\tprogress->delayed_percent_treshold = percent_treshold;\n+\tprogress->delayed_title = title;\n+\tprogress->delay = delay;\n+\tset_progress_signal();\n+}\n+\n void stop_progress(struct progress *progress)\n {\n \tclear_progress_signal();\ndiff --git a/progress.h b/progress.h\nindex 1f2661e..4ee851a 100644\n--- a/progress.h\n+++ b/progress.h\n@@ -5,11 +5,17 @@ struct progress {\n \tconst char *prefix;\n \tunsigned total;\n \tunsigned last_percent;\n+\tunsigned delay;\n+\tunsigned delayed_percent_treshold;\n+\tconst char *delayed_title;\n };\n \n int display_progress(struct progress *progress, unsigned n);\n void start_progress(struct progress *progress, const char *title,\n \t\t    const char *prefix, unsigned total);\n+void start_progress_delay(struct progress *progress, const char *title,\n+\t\t\t  const char *prefix, unsigned total,\n+\t\t\t  unsigned percent_treshold, unsigned delay);\n void stop_progress(struct progress *progress);\n \n #endif\n-- \n1.5.1.1.874.g4f52f-dirty\n"}]}