From 5e4003cd87817211a35fe6fadf79af7b7a3092da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 25 Mar 2026 14:49:55 +0100 Subject: [PATCH] shared/table-format: generalize table_sync_column_width to more columns The column index is moved to the first position. I think we're unlikely to want to synchronize widths of *different* columns, and having just one column argument makes the callers simpler. Also, the type is changed to size_t to match other functions, and this avoids the need to cast to size_t in the callers. --- src/bless-boot/bless-boot.c | 2 +- src/dissect/dissect.c | 2 +- src/id128/id128.c | 2 +- src/shared/format-table.c | 31 ++++++++++++++++++++----------- src/shared/format-table.h | 3 ++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/bless-boot/bless-boot.c b/src/bless-boot/bless-boot.c index bf1c6e7a0c..b82be92dbd 100644 --- a/src/bless-boot/bless-boot.c +++ b/src/bless-boot/bless-boot.c @@ -50,7 +50,7 @@ static int help(void) { if (r < 0) return r; - (void) table_sync_column_width(options, 0, verbs, 0); + (void) table_sync_column_widths(0, options, verbs); printf("%s [OPTIONS...] COMMAND\n" "\n%sMark the boot process as good or bad.%s\n" diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 3597971af9..bcfd7f5a81 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -141,7 +141,7 @@ static int help(void) { return r; /* Make the 1st column same width in both tables */ - (void) table_sync_column_width(options, 0, commands, 0); + (void) table_sync_column_widths(0, options, commands); printf("%1$s [OPTIONS...] IMAGE\n" "%1$s [OPTIONS...] --mount IMAGE PATH\n" diff --git a/src/id128/id128.c b/src/id128/id128.c index 688504c714..fe4d2f2283 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -208,7 +208,7 @@ static int help(void) { return r; /* Make the 1st column same width in both tables */ - (void) table_sync_column_width(options, 0, verbs, 0); + (void) table_sync_column_widths(0, options, verbs); printf("%s [OPTIONS...] COMMAND\n\n" "%sGenerate and print 128-bit identifiers.%s\n" diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 279e7fda68..04552b5b56 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -2185,26 +2185,35 @@ int table_set_column_width(Table *t, size_t column, size_t width) { return r; } -int table_sync_column_width(Table *a, size_t column_a, Table *b, size_t column_b) { - size_t w1, w2; - int r; +int _table_sync_column_widths(size_t column, Table *a, ...) { + size_t max = 0; + va_list ap; + int r = 0; assert(a); - assert(b); - /* Make both tables have specified columns of same width */ + /* Make the specified column have the same width in the tables. */ - r = table_data_requested_width(a, column_a, &w1); - if (r < 0) - return log_error_errno(r, "Failed to query table column width: %m"); + va_start(ap, a); + for (Table *t = a; t; t = va_arg(ap, Table*)) { + size_t w; - r = table_data_requested_width(b, column_b, &w2); + r = table_data_requested_width(t, column, &w); + if (r < 0) + break; + + max = MAX(max, w); + } + va_end(ap); if (r < 0) return log_error_errno(r, "Failed to query table column width: %m"); r = 0; - RET_GATHER(r, table_set_column_width(a, column_a, MAX(w1, w2))); - RET_GATHER(r, table_set_column_width(b, column_b, MAX(w1, w2))); + va_start(ap, a); + for (Table *t = a; t; t = va_arg(ap, Table*)) + RET_GATHER(r, table_set_column_width(t, column, max)); + va_end(ap); + return r; } diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 9a11fb7c30..bb5a68b7e9 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -144,7 +144,8 @@ int table_hide_column_from_display_internal(Table *t, ...); int table_data_requested_width(Table *table, size_t column, size_t *ret); int table_set_column_width(Table *t, size_t column, size_t width); -int table_sync_column_width(Table *a, size_t column_a, Table *b, size_t column_b); +int _table_sync_column_widths(size_t column, Table *a, ...); +#define table_sync_column_widths(column, a, ...) _table_sync_column_widths(column, a, __VA_ARGS__, NULL) int table_print(Table *t, FILE *f); int table_format(Table *t, char **ret);