struct cli_progressbar_widget* widget, unsigned int width, void* data);
void (*free)(void* data);
- char buffer[1024];
+ char* buffer;
};
struct cli_progressbar {
if (widget->free && widget->data)
widget->free(widget->data);
+ if (widget->buffer)
+ free(widget->buffer);
+
free(widget);
}
unsigned long int max_value = pakfire_progress_get_value(p->progress);
// Format the result
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%lu/%lu", value, max_value);
+ r = asprintf(&widget->buffer, "%lu/%lu", value, max_value);
if (r < 0)
return NULL;
double percentage = pakfire_progress_get_percentage(p->progress);
// Format to string
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%3.0f%%", percentage);
+ r = asprintf(&widget->buffer, "%3.0f%%", percentage);
if (r < 0)
return NULL;
static const char* cli_progressbar_bar(struct cli_progressbar* p,
struct cli_progressbar_widget* widget, unsigned int width, void* data) {
- if (width >= sizeof(widget->buffer) || width < 2)
+ // Allocate or adjust the buffer
+ widget->buffer = realloc(widget->buffer, width);
+
+ // Fail if we could not allocate the buffer
+ if (!widget->buffer)
return NULL;
// Remove the bar when we are finished so that the terminal is not so cluttered
widget->buffer[i] = '-';
}
- // Terminat the string
+ // Terminate the string
widget->buffer[width] = '\0';
return widget->buffer;
return NULL;
// Format the time
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%02lu:%02lu", t / 60, t % 60);
+ r = asprintf(&widget->buffer, "%02lu:%02lu", t / 60, t % 60);
if (r)
return NULL;
// Print a placeholder when we have no ETA (yet)
if (t <= 0) {
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%-5s: --:--:--", _("ETA"));
+ r = asprintf(&widget->buffer, "%-5s: --:--:--", _("ETA"));
if (r < 0)
return NULL;
// Otherwise show the ETA
} else {
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%-5s: %02lu:%02lu",
- _("ETA"), t / 60, t % 60);
+ r = asprintf(&widget->buffer, "%-5s: %02lu:%02lu", _("ETA"), t / 60, t % 60);
if (r < 0)
return NULL;
}
return cli_progressbar_add_widget(p, cli_progressbar_eta, NULL, 0, NULL);
}
-#define cli_progressbar_format_speed(s, speed) \
- __cli_progressbar_format_speed(s, sizeof(s), speed)
-
-static int __cli_progressbar_format_speed(char* s, const size_t length, double value) {
+static int cli_progressbar_format_speed(char** s, double value) {
const char* units[] = {
"%4.0fB/s",
"%4.0fkB/s",
}
// Format the string
- r = snprintf(s, length, *unit, value);
+ r = asprintf(s, *unit, value);
if (r < 0)
return r;
return NULL;
// Add padding so that the string is always at least five characters long
- r = snprintf(widget->buffer, sizeof(widget->buffer), "%-5s", buffer);
+ r = asprintf(&widget->buffer, "%-5s", buffer);
if (r < 0)
return NULL;
static const char* cli_progressbar_transfer_speed(struct cli_progressbar* p,
struct cli_progressbar_widget* widget, unsigned int width, void* data) {
+ int r;
+
+ // Fetch the speed
double speed = pakfire_progress_get_transfer_speed(p->progress);
if (speed < 0)
return NULL;
// Format the speed
- int r = cli_progressbar_format_speed(widget->buffer, speed);
+ r = cli_progressbar_format_speed(&widget->buffer, speed);
if (r < 0)
return NULL;