pretty-print: add format-string version of draw_progress_bar()

We often format the prefix string via asprintf() before, let's hence add
a helper for that.
This commit is contained in:
Lennart Poettering
2024-10-29 21:37:26 +01:00
parent 21abc0a943
commit 91d640435d
8 changed files with 61 additions and 58 deletions
+8 -11
View File
@@ -124,17 +124,14 @@ static void raw_export_report_progress(RawExport *e) {
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->st.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
if (isatty_safe(STDERR_FILENO))
(void) draw_progress_barf(
percent,
"%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->st.st_size));
else
log_info("Exported %u%%.", percent);
e->last_percent = percent;
+8 -11
View File
@@ -134,17 +134,14 @@ static void tar_export_report_progress(TarExport *e) {
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->quota_referenced)) < 0)
return;
draw_progress_bar(s, percent);
} else
if (isatty_safe(STDERR_FILENO))
(void) draw_progress_barf(
percent,
"%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(e->written_uncompressed),
FORMAT_BYTES(e->quota_referenced));
else
log_info("Exported %u%%.", percent);
e->last_percent = percent;
+8 -11
View File
@@ -151,17 +151,14 @@ static void raw_import_report_progress(RawImport *i) {
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
if (isatty_safe(STDERR_FILENO))
(void) draw_progress_barf(
percent,
"%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size));
else
log_info("Imported %u%%.", percent);
i->last_percent = percent;
+8 -11
View File
@@ -152,17 +152,14 @@ static void tar_import_report_progress(TarImport *i) {
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
if (isatty_safe(STDERR_FILENO)) {
_cleanup_free_ char *s = NULL;
if (asprintf(&s, "%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size)) < 0)
return;
draw_progress_bar(s, percent);
} else
if (isatty_safe(STDERR_FILENO))
(void) draw_progress_barf(
percent,
"%s %s/%s",
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
FORMAT_BYTES(i->written_compressed),
FORMAT_BYTES(i->input_stat.st_size));
else
log_info("Imported %u%%.", percent);
i->last_percent = percent;
+9 -11
View File
@@ -5085,21 +5085,19 @@ static int partition_format_verity_sig(Context *context, Partition *p) {
static int progress_bytes(uint64_t n_bytes, void *userdata) {
Partition *p = ASSERT_PTR(userdata);
_cleanup_free_ char *s = NULL;
p->copy_blocks_done += n_bytes;
if (asprintf(&s, "%s %s %s %s/%s",
strna(p->copy_blocks_path),
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
strna(p->definition_path),
FORMAT_BYTES(p->copy_blocks_done),
FORMAT_BYTES(p->copy_blocks_size)) < 0)
return log_oom();
(void) draw_progress_barf(
p->copy_blocks_done >= p->copy_blocks_size ? 100.0 : /* catch division be zero */
100.0 * (double) p->copy_blocks_done / (double) p->copy_blocks_size,
"%s %s %s %s/%s",
strna(p->copy_blocks_path),
special_glyph(SPECIAL_GLYPH_ARROW_RIGHT),
strna(p->definition_path),
FORMAT_BYTES(p->copy_blocks_done),
FORMAT_BYTES(p->copy_blocks_size));
draw_progress_bar(s,
p->copy_blocks_done >= p->copy_blocks_size ? 100.0 : /* catch division be zero */
100.0 * (double) p->copy_blocks_done / (double) p->copy_blocks_size);
return 0;
}
+16
View File
@@ -548,6 +548,22 @@ void draw_progress_bar(const char *prefix, double percentage) {
draw_progress_bar_unbuffered(prefix, percentage);
}
int draw_progress_barf(double percentage, const char *prefixf, ...) {
_cleanup_free_ char *s = NULL;
va_list ap;
int r;
va_start(ap, prefixf);
r = vasprintf(&s, prefixf, ap);
va_end(ap);
if (r < 0)
return -ENOMEM;
draw_progress_bar(s, percentage);
return 0;
}
void clear_progress_bar(const char *prefix) {
WITH_BUFFERED_STDERR;
clear_progress_bar_unbuffered(prefix);
+1
View File
@@ -54,6 +54,7 @@ int terminal_tint_color(double hue, char **ret);
bool shall_tint_background(void);
void draw_progress_bar(const char *prefix, double percentage);
int draw_progress_barf(double percentage, const char *prefixf, ...) _printf_(2, 3);
void clear_progress_bar(const char *prefix);
void draw_progress_bar_unbuffered(const char *prefix, double percentage);
void clear_progress_bar_unbuffered(const char *prefix);
+3 -3
View File
@@ -14,7 +14,7 @@ TEST(progress_bar) {
for (double d = 0; d <= 100; d += 0.5) {
usleep_safe(random_u64_range(20 * USEC_PER_MSEC));
draw_progress_bar(PROGRESS_PREFIX, d);
draw_progress_barf(d, PROGRESS_PREFIX "[" PID_FMT "]", getpid_cached());
if (!paused && d >= 50) {
clear_progress_bar(PROGRESS_PREFIX);
@@ -25,9 +25,9 @@ TEST(progress_bar) {
}
}
draw_progress_bar(PROGRESS_PREFIX, 100);
draw_progress_barf(100, PROGRESS_PREFIX "[" PID_FMT "]", getpid_cached());
usleep_safe(300 * MSEC_PER_SEC);
clear_progress_bar(PROGRESS_PREFIX);
clear_progress_bar(PROGRESS_PREFIX "[0123456789]" );
fputs("Done.\n", stdout);
}