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.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
+1
-1
@@ -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"
|
||||
|
||||
+20
-11
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user