make struct progress an opaque type
[git] / progress.c
1 #include "git-compat-util.h"
2 #include "progress.h"
3
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;
11 };
12
13 static volatile sig_atomic_t progress_update;
14
15 static void progress_interval(int signum)
16 {
17         progress_update = 1;
18 }
19
20 static void set_progress_signal(void)
21 {
22         struct sigaction sa;
23         struct itimerval v;
24
25         progress_update = 0;
26
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);
32
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);
37 }
38
39 static void clear_progress_signal(void)
40 {
41         struct itimerval v = {{0,},};
42         setitimer(ITIMER_REAL, &v, NULL);
43         signal(SIGALRM, SIG_IGN);
44         progress_update = 0;
45 }
46
47 static int display(struct progress *progress, unsigned n, int done)
48 {
49         char *eol;
50
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;
62                         }
63                 }
64         }
65
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;
76                 }
77         } else if (progress_update) {
78                 fprintf(stderr, "%s: %u%s", progress->title, n, eol);
79                 progress_update = 0;
80                 return 1;
81         }
82
83         return 0;
84 }
85
86 int display_progress(struct progress *progress, unsigned n)
87 {
88         return progress ? display(progress, n, 0) : 0;
89 }
90
91 struct progress *start_progress_delay(const char *title, unsigned total,
92                                        unsigned percent_treshold, unsigned delay)
93 {
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;
99         }
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;
108 }
109
110 struct progress *start_progress(const char *title, unsigned total)
111 {
112         return start_progress_delay(title, total, 0, 0);
113 }
114
115 void stop_progress(struct progress **p_progress)
116 {
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);
125         }
126         clear_progress_signal();
127         free(progress);
128 }