relax usage of the progress API
[git/dscho.git] / progress.c
blobc342e39c5d2e5673fc95e0cdc91856b0bc3cdd35
1 #include "git-compat-util.h"
2 #include "progress.h"
4 struct progress {
5 const char *title;
6 int last_value;
7 unsigned total;
8 unsigned last_percent;
9 unsigned delay;
10 unsigned delayed_percent_treshold;
13 static volatile sig_atomic_t progress_update;
15 static void progress_interval(int signum)
17 progress_update = 1;
20 static void set_progress_signal(void)
22 struct sigaction sa;
23 struct itimerval v;
25 progress_update = 0;
27 memset(&sa, 0, sizeof(sa));
28 sa.sa_handler = progress_interval;
29 sigemptyset(&sa.sa_mask);
30 sa.sa_flags = SA_RESTART;
31 sigaction(SIGALRM, &sa, NULL);
33 v.it_interval.tv_sec = 1;
34 v.it_interval.tv_usec = 0;
35 v.it_value = v.it_interval;
36 setitimer(ITIMER_REAL, &v, NULL);
39 static void clear_progress_signal(void)
41 struct itimerval v = {{0,},};
42 setitimer(ITIMER_REAL, &v, NULL);
43 signal(SIGALRM, SIG_IGN);
44 progress_update = 0;
47 static int display(struct progress *progress, unsigned n, int done)
49 char *eol;
51 if (progress->delay) {
52 if (!progress_update || --progress->delay)
53 return 0;
54 if (progress->total) {
55 unsigned percent = n * 100 / progress->total;
56 if (percent > progress->delayed_percent_treshold) {
57 /* inhibit this progress report entirely */
58 clear_progress_signal();
59 progress->delay = -1;
60 progress->total = 0;
61 return 0;
66 progress->last_value = n;
67 eol = done ? ", done. \n" : " \r";
68 if (progress->total) {
69 unsigned percent = n * 100 / progress->total;
70 if (percent != progress->last_percent || progress_update) {
71 progress->last_percent = percent;
72 fprintf(stderr, "%s: %3u%% (%u/%u)%s", progress->title,
73 percent, n, progress->total, eol);
74 progress_update = 0;
75 return 1;
77 } else if (progress_update) {
78 fprintf(stderr, "%s: %u%s", progress->title, n, eol);
79 progress_update = 0;
80 return 1;
83 return 0;
86 int display_progress(struct progress *progress, unsigned n)
88 return progress ? display(progress, n, 0) : 0;
91 struct progress *start_progress_delay(const char *title, unsigned total,
92 unsigned percent_treshold, unsigned delay)
94 struct progress *progress = malloc(sizeof(*progress));
95 if (!progress) {
96 /* unlikely, but here's a good fallback */
97 fprintf(stderr, "%s...\n", title);
98 return NULL;
100 progress->title = title;
101 progress->total = total;
102 progress->last_value = -1;
103 progress->last_percent = -1;
104 progress->delayed_percent_treshold = percent_treshold;
105 progress->delay = delay;
106 set_progress_signal();
107 return progress;
110 struct progress *start_progress(const char *title, unsigned total)
112 return start_progress_delay(title, total, 0, 0);
115 void stop_progress(struct progress **p_progress)
117 struct progress *progress = *p_progress;
118 if (!progress)
119 return;
120 *p_progress = NULL;
121 if (progress->last_value != -1) {
122 /* Force the last update */
123 progress_update = 1;
124 display(progress, progress->last_value, 1);
126 clear_progress_signal();
127 free(progress);