Merge pull request #13074 from poettering/format-tree-many

table_add_many() improvements
This commit is contained in:
Lennart Poettering 2019-07-16 16:03:54 +02:00 committed by GitHub
commit c1b71f3a61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 35 deletions

View File

@ -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");
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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"