]> git.ipfire.org Git - pakfire.git/commitdiff
progress: Implement setting the title as a separate call
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 1 Oct 2023 11:20:23 +0000 (11:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 1 Oct 2023 11:20:23 +0000 (11:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/progressbar.c
src/libpakfire/compress.c
src/libpakfire/include/pakfire/progress.h
src/libpakfire/progress.c

index ef7ff1e3ca0ad8898a31f42346a865f1ef4d8c4e..e793c557b271a9028cfd1cf76691dd446ca03dc8 100644 (file)
@@ -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);
index d187ce9b73a1ab0511d46733013136a697f5fe90..dad2a8ee3cda527b481d4104747be2e22b286316 100644 (file)
@@ -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;
 
index 5c003b9253442102527e048a97a7a06fd2d1e04c..0624eaf19e15bb05c8e7bef600f085d4ee6bb140 100644 (file)
@@ -73,7 +73,7 @@ double pakfire_progress_get_transfer_speed(struct pakfire_progress* p);
 #include <pakfire/pakfire.h>
 
 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
 
index 5b15f3330310c781f143fccce1bc35ae67cf5a0a..7f663bad834c581bc91bb1a22494fe93eefd5094 100644 (file)
@@ -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;