From: Michael Tremer Date: Sun, 1 Oct 2023 11:20:23 +0000 (+0000) Subject: progress: Implement setting the title as a separate call X-Git-Tag: 0.9.30~1584 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d107656049750c6cc4df10f9b2e408496ef60abb;p=pakfire.git progress: Implement setting the title as a separate call Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/progressbar.c b/src/cli/lib/progressbar.c index ef7ff1e3c..e793c557b 100644 --- a/src/cli/lib/progressbar.c +++ b/src/cli/lib/progressbar.c @@ -309,6 +309,15 @@ static int cli_progressbar_add_string(struct cli_progressbar* p, const char* for return cli_progressbar_add_widget(p, cli_progressbar_string, free, 0, s); } +static const char* cli_progressbar_title(struct cli_progressbar* p, + struct cli_progressbar_widget* widget, unsigned int width, void* data) { + return pakfire_progress_get_title(p->progress); +} + +static int cli_progressbar_add_title(struct cli_progressbar* p) { + return cli_progressbar_add_widget(p, cli_progressbar_title, NULL, 0, NULL); +} + static const char* cli_progressbar_counter(struct cli_progressbar* p, struct cli_progressbar_widget* widget, unsigned int width, void* data) { int r; @@ -546,7 +555,6 @@ static int cli_progressbar_add_transfer_speed(struct cli_progressbar* p) { int cli_setup_progressbar(struct pakfire* pakfire, struct pakfire_progress* p, void* data) { struct cli_progressbar* progressbar = NULL; - const char* title = NULL; int r; // Allocate a new progressbar @@ -567,12 +575,9 @@ int cli_setup_progressbar(struct pakfire* pakfire, struct pakfire_progress* p, v pakfire_progress_set_free_callback(p, __cli_progressbar_free); // Add the title - title = pakfire_progress_get_title(p); - if (title) { - r = cli_progressbar_add_string(progressbar, "%s", title); - if (r) - goto ERROR; - } + r = cli_progressbar_add_title(progressbar); + if (r) + goto ERROR; // Show the bar r = cli_progressbar_add_bar(progressbar); diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index d187ce9b7..dad2a8ee3 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -818,7 +818,12 @@ int pakfire_extract(struct pakfire* pakfire, struct archive* archive, progress_flags |= PAKFIRE_PROGRESS_SHOW_TRANSFER_SPEED; // Create the progressbar indicator - r = pakfire_progress_create(&data.progress, pakfire, message, progress_flags); + r = pakfire_progress_create(&data.progress, pakfire, progress_flags); + if (r) + goto ERROR; + + // Set the title + r = pakfire_progress_set_title(data.progress, "%s", message); if (r) goto ERROR; diff --git a/src/libpakfire/include/pakfire/progress.h b/src/libpakfire/include/pakfire/progress.h index 5c003b925..0624eaf19 100644 --- a/src/libpakfire/include/pakfire/progress.h +++ b/src/libpakfire/include/pakfire/progress.h @@ -73,7 +73,7 @@ double pakfire_progress_get_transfer_speed(struct pakfire_progress* p); #include int pakfire_progress_create(struct pakfire_progress** progress, - struct pakfire* pakfire, const char* title, int flags); + struct pakfire* pakfire, int flags); struct pakfire_progress* pakfire_progress_ref(struct pakfire_progress* p); struct pakfire_progress* pakfire_progress_unref(struct pakfire_progress* p); @@ -81,6 +81,9 @@ struct pakfire_progress* pakfire_progress_unref(struct pakfire_progress* p); int pakfire_progress_start(struct pakfire_progress* p, unsigned long int value); int pakfire_progress_finish(struct pakfire_progress* p); int pakfire_progress_update(struct pakfire_progress* p, unsigned long int value); +int pakfire_progress_increment(struct pakfire_progress* p, unsigned long int value); + +int pakfire_progress_set_title(struct pakfire_progress* p, const char* format, ...); #endif diff --git a/src/libpakfire/progress.c b/src/libpakfire/progress.c index 5b15f3330..7f663bad8 100644 --- a/src/libpakfire/progress.c +++ b/src/libpakfire/progress.c @@ -31,7 +31,7 @@ struct pakfire_progress { int nrefs; // Title - const char* title; + char* title; // Flags what to show int flags; @@ -70,12 +70,14 @@ static void pakfire_progress_free(struct pakfire_progress* p) { if (p->callbacks.free) p->callbacks.free(p->pakfire, p, p->callbacks.data); + if (p->title) + free(p->title); pakfire_unref(p->pakfire); free(p); } int pakfire_progress_create(struct pakfire_progress** progress, - struct pakfire* pakfire, const char* title, int flags) { + struct pakfire* pakfire, int flags) { struct pakfire_progress* p = NULL; int r; @@ -90,9 +92,6 @@ int pakfire_progress_create(struct pakfire_progress** progress, // Initialize the reference counter p->nrefs = 1; - // Store title - p->title = title; - // Store the flags p->flags = flags; @@ -256,6 +255,22 @@ PAKFIRE_EXPORT const char* pakfire_progress_get_title(struct pakfire_progress* p return p->title; } +int pakfire_progress_set_title(struct pakfire_progress* p, const char* format, ...) { + va_list args; + int r; + + // Format the title + va_start(args, format); + r = vasprintf(&p->title, format, args); + va_end(args); + + // Fail + if (r < 0) + return r; + + return 0; +} + PAKFIRE_EXPORT double pakfire_progress_get_percentage(struct pakfire_progress* p) { if (!p->max_value) return 0;