@matheusbsbs / matheus-bagliano / commits / 180a9f2268
Fork 0

provide a facility for "delayed" progress reporting

This allows for progress to be displayed only if the progress has not reached a specified percentage treshold within a given delay in seconds. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 20, 2007 at 15:05 UTC 180a9f226860e18cdc2c3987ae89c239b318b408
2 files changed +40
progress.c
+34
@@ -13,6 +13,8 @@ static void set_progress_signal(void)
13 struct sigaction sa;
14 struct itimerval v;
15
16 + progress_update = 0;
17 +
18 memset(&sa, 0, sizeof(sa));
19 sa.sa_handler = progress_interval;
20 sigemptyset(&sa.sa_mask);
@@ -35,6 +37,24 @@ static void clear_progress_signal(void)
37
38 int display_progress(struct progress *progress, unsigned n)
39 {
40 + if (progress->delay) {
41 + char buf[80];
42 + if (!progress_update || --progress->delay)
43 + return 0;
44 + if (progress->total) {
45 + unsigned percent = n * 100 / progress->total;
46 + if (percent > progress->delayed_percent_treshold) {
47 + /* inhibit this progress report entirely */
48 + clear_progress_signal();
49 + progress->delay = -1;
50 + progress->total = 0;
51 + return 0;
52 + }
53 + }
54 + if (snprintf(buf, sizeof(buf),
55 + progress->delayed_title, progress->total))
56 + fprintf(stderr, "%s\n", buf);
57 + }
58 if (progress->total) {
59 unsigned percent = n * 100 / progress->total;
60 if (percent != progress->last_percent || progress_update) {
@@ -59,11 +79,25 @@ void start_progress(struct progress *progress, const char *title,
79 progress->prefix = prefix;
80 progress->total = total;
81 progress->last_percent = -1;
82 + progress->delay = 0;
83 if (snprintf(buf, sizeof(buf), title, total))
84 fprintf(stderr, "%s\n", buf);
85 set_progress_signal();
86 }
87
88 +void start_progress_delay(struct progress *progress, const char *title,
89 + const char *prefix, unsigned total,
90 + unsigned percent_treshold, unsigned delay)
91 +{
92 + progress->prefix = prefix;
93 + progress->total = total;
94 + progress->last_percent = -1;
95 + progress->delayed_percent_treshold = percent_treshold;
96 + progress->delayed_title = title;
97 + progress->delay = delay;
98 + set_progress_signal();
99 +}
100 +
101 void stop_progress(struct progress *progress)
102 {
103 clear_progress_signal();
progress.h
+6
@@ -5,11 +5,17 @@ struct progress {
5 const char *prefix;
6 unsigned total;
7 unsigned last_percent;
8 + unsigned delay;
9 + unsigned delayed_percent_treshold;
10 + const char *delayed_title;
11 };
12
13 int display_progress(struct progress *progress, unsigned n);
14 void start_progress(struct progress *progress, const char *title,
15 const char *prefix, unsigned total);
16 +void start_progress_delay(struct progress *progress, const char *title,
17 + const char *prefix, unsigned total,
18 + unsigned percent_treshold, unsigned delay);
19 void stop_progress(struct progress *progress);
20
21 #endif