make struct progress an opaque type
This allows for better management of progress "object" existence, as well as making the progress display implementation more independent from its callers. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
0e54913796
commit
dc6a0757c4
33
progress.c
33
progress.c
@ -1,6 +1,15 @@
|
||||
#include "git-compat-util.h"
|
||||
#include "progress.h"
|
||||
|
||||
struct progress {
|
||||
const char *title;
|
||||
int last_value;
|
||||
unsigned total;
|
||||
unsigned last_percent;
|
||||
unsigned delay;
|
||||
unsigned delayed_percent_treshold;
|
||||
};
|
||||
|
||||
static volatile sig_atomic_t progress_update;
|
||||
|
||||
static void progress_interval(int signum)
|
||||
@ -76,12 +85,18 @@ static int display(struct progress *progress, unsigned n, int done)
|
||||
|
||||
int display_progress(struct progress *progress, unsigned n)
|
||||
{
|
||||
return display(progress, n, 0);
|
||||
return progress ? display(progress, n, 0) : 0;
|
||||
}
|
||||
|
||||
void start_progress_delay(struct progress *progress, const char *title,
|
||||
unsigned total, unsigned percent_treshold, unsigned delay)
|
||||
struct progress *start_progress_delay(const char *title, unsigned total,
|
||||
unsigned percent_treshold, unsigned delay)
|
||||
{
|
||||
struct progress *progress = malloc(sizeof(*progress));
|
||||
if (!progress) {
|
||||
/* unlikely, but here's a good fallback */
|
||||
fprintf(stderr, "%s...\n", title);
|
||||
return NULL;
|
||||
}
|
||||
progress->title = title;
|
||||
progress->total = total;
|
||||
progress->last_value = -1;
|
||||
@ -89,19 +104,25 @@ void start_progress_delay(struct progress *progress, const char *title,
|
||||
progress->delayed_percent_treshold = percent_treshold;
|
||||
progress->delay = delay;
|
||||
set_progress_signal();
|
||||
return progress;
|
||||
}
|
||||
|
||||
void start_progress(struct progress *progress, const char *title, unsigned total)
|
||||
struct progress *start_progress(const char *title, unsigned total)
|
||||
{
|
||||
start_progress_delay(progress, title, total, 0, 0);
|
||||
return start_progress_delay(title, total, 0, 0);
|
||||
}
|
||||
|
||||
void stop_progress(struct progress *progress)
|
||||
void stop_progress(struct progress **p_progress)
|
||||
{
|
||||
struct progress *progress = *p_progress;
|
||||
if (!progress)
|
||||
return;
|
||||
*p_progress = NULL;
|
||||
if (progress->last_value != -1) {
|
||||
/* Force the last update */
|
||||
progress_update = 1;
|
||||
display(progress, progress->last_value, 1);
|
||||
}
|
||||
clear_progress_signal();
|
||||
free(progress);
|
||||
}
|
||||
|
Reference in New Issue
Block a user