diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 3cf6515f5f3..134b2d28c3b 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -1502,35 +1502,15 @@ static int assess(const struct security_info *info, Table *overview_table, Analy if (color) (void) table_set_color(details_table, cell, color); - r = table_add_cell(details_table, &cell, TABLE_STRING, a->id); + r = table_add_many(details_table, + TABLE_STRING, a->id, TABLE_SET_URL, a->url, + TABLE_STRING, description, + TABLE_UINT64, a->weight, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_UINT64, badness, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_UINT64, a->range, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_EMPTY, TABLE_SET_ALIGN_PERCENT, 100); if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - if (a->url) - (void) table_set_url(details_table, cell, a->url); - - r = table_add_cell(details_table, NULL, TABLE_STRING, description); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->weight); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &badness); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->range); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_EMPTY, NULL); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); + return log_error_errno(r, "Failed to add cells to table: %m"); } } diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 11e4d567299..aede59bf346 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -681,6 +681,7 @@ int table_update(Table *t, TableCell *cell, TableDataType type, const void *data int table_add_many_internal(Table *t, TableDataType first_type, ...) { TableDataType type; va_list ap; + TableCell *last_cell = NULL; int r; assert(t); @@ -776,6 +777,55 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { data = &buffer.ifindex; break; + case TABLE_SET_MINIMUM_WIDTH: { + size_t w = va_arg(ap, size_t); + + r = table_set_minimum_width(t, last_cell, w); + break; + } + + case TABLE_SET_MAXIMUM_WIDTH: { + size_t w = va_arg(ap, size_t); + r = table_set_maximum_width(t, last_cell, w); + break; + } + + case TABLE_SET_WEIGHT: { + unsigned w = va_arg(ap, unsigned); + r = table_set_weight(t, last_cell, w); + break; + } + + case TABLE_SET_ALIGN_PERCENT: { + unsigned p = va_arg(ap, unsigned); + r = table_set_align_percent(t, last_cell, p); + break; + } + + case TABLE_SET_ELLIPSIZE_PERCENT: { + unsigned p = va_arg(ap, unsigned); + r = table_set_ellipsize_percent(t, last_cell, p); + break; + } + + case TABLE_SET_COLOR: { + const char *c = va_arg(ap, const char*); + r = table_set_color(t, last_cell, c); + break; + } + + case TABLE_SET_URL: { + const char *u = va_arg(ap, const char*); + r = table_set_url(t, last_cell, u); + break; + } + + case TABLE_SET_UPPERCASE: { + int u = va_arg(ap, int); + r = table_set_uppercase(t, last_cell, u); + break; + } + case _TABLE_DATA_TYPE_MAX: /* Used as end marker */ va_end(ap); @@ -785,7 +835,9 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { assert_not_reached("Uh? Unexpected data type."); } - r = table_add_cell(t, NULL, type, data); + if (type < _TABLE_DATA_TYPE_MAX) + r = table_add_cell(t, &last_cell, type, data); + if (r < 0) { va_end(ap); return r; diff --git a/src/shared/format-table.h b/src/shared/format-table.h index bbfeb10c11e..452f1706c0c 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -28,6 +28,18 @@ typedef enum TableDataType { TABLE_PERCENT, TABLE_IFINDEX, _TABLE_DATA_TYPE_MAX, + + /* The following are not really data types, but commands for table_add_cell_many() to make changes to + * a cell just added. */ + TABLE_SET_MINIMUM_WIDTH, + TABLE_SET_MAXIMUM_WIDTH, + TABLE_SET_WEIGHT, + TABLE_SET_ALIGN_PERCENT, + TABLE_SET_ELLIPSIZE_PERCENT, + TABLE_SET_COLOR, + TABLE_SET_URL, + TABLE_SET_UPPERCASE, + _TABLE_DATA_TYPE_INVALID = -1, } TableDataType; @@ -55,7 +67,7 @@ int table_set_weight(Table *t, TableCell *cell, unsigned weight); int table_set_align_percent(Table *t, TableCell *cell, unsigned percent); int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent); int table_set_color(Table *t, TableCell *cell, const char *color); -int table_set_url(Table *t, TableCell *cell, const char *color); +int table_set_url(Table *t, TableCell *cell, const char *url); int table_set_uppercase(Table *t, TableCell *cell, bool b); int table_update(Table *t, TableCell *cell, TableDataType type, const void *data); diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 5bede5c75b4..def9cbda2f2 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -34,6 +34,7 @@ int main(int argc, char *argv[]) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *formatted = NULL; + assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0); assert_se(setenv("COLUMNS", "40", 1) >= 0); assert_se(t = table_new("one", "two", "three")); @@ -48,6 +49,7 @@ int main(int argc, char *argv[]) { assert_se(table_add_many(t, TABLE_STRING, "a long field", TABLE_STRING, "yyy", + TABLE_SET_UPPERCASE, 1, TABLE_BOOLEAN, false) >= 0); assert_se(table_format(t, &formatted) >= 0); @@ -56,7 +58,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" "xxx yyy yes\n" - "a long field yyy no\n")); + "a long field YYY no\n")); formatted = mfree(formatted); @@ -68,7 +70,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" "xxx yyy yes\n" - "a long field yyy no\n")); + "a long field YYY no\n")); formatted = mfree(formatted); @@ -79,7 +81,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THR…\n" "xxx yyy yes\n" - "a … yyy no\n")); + "a … YYY no\n")); formatted = mfree(formatted); @@ -113,7 +115,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" - "a long field yyy no\n" + "a long field YYY no\n" "xxx yyy yes\n")); formatted = mfree(formatted); @@ -139,7 +141,7 @@ int main(int argc, char *argv[]) { printf("%s\n", formatted); assert_se(streq(formatted, - "a long field yyy no\n" + "a long field YYY no\n" "fäää zzz no\n" "fäää uuu yes\n" "xxx yyy yes\n"