From a26db0bc5d4011de24e5930af6fd9051deeb46be Mon Sep 17 00:00:00 2001 From: NOGISAKA Sadata Date: Mon, 9 Jul 2018 23:49:07 +0900 Subject: [PATCH 1/3] format-table: make all widths be set properly Fixes #9549. --- src/basic/format-table.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/basic/format-table.c b/src/basic/format-table.c index 94e796d1ca..ced3593bc4 100644 --- a/src/basic/format-table.c +++ b/src/basic/format-table.c @@ -1134,14 +1134,12 @@ int table_print(Table *t, FILE *f) { assert(weight_sum >= column_weight[j]); weight_sum -= column_weight[j]; - if (restart) + if (restart && !finalize) break; } - if (finalize) { - assert(!restart); + if (finalize) break; - } if (!restart) finalize = true; From a6e964712c53b877543cf18aff59ae99cffc1f50 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 12 Jul 2018 14:20:09 +0900 Subject: [PATCH 2/3] test: add a reproducer of issue #9549 --- src/test/test-format-table.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index adcc414161..2527cacbbd 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -5,6 +5,30 @@ #include "string-util.h" #include "time-util.h" +static void test_issue_9549(void) { + _cleanup_(table_unrefp) Table *table = NULL; + _cleanup_free_ char *formatted = NULL; + + assert_se(table = table_new("NAME", "TYPE", "RO", "USAGE", "CREATED", "MODIFIED")); + assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0); + assert_se(table_add_many(table, + TABLE_STRING, "foooo", + TABLE_STRING, "raw", + TABLE_BOOLEAN, false, + TABLE_SIZE, (uint64_t) (673.7*1024*1024), + TABLE_STRING, "Wed 2018-07-11 00:10:33 JST", + TABLE_STRING, "Wed 2018-07-11 00:16:00 JST") >= 0); + + table_set_width(table, 75); + assert_se(table_format(table, &formatted) >= 0); + + printf("%s\n", formatted); + assert_se(streq(formatted, + "NAME TYPE RO USAGE CREATED MODIFIED \n" + "foooo raw no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n" + )); +} + int main(int argc, char *argv[]) { _cleanup_(table_unrefp) Table *t = NULL; @@ -135,5 +159,7 @@ int main(int argc, char *argv[]) { " yes xxx yes xxx xxx \n" "5min 5min \n")); + test_issue_9549(); + return 0; } From 475d8599b0821eefb6e6c283f4f9847cffbb4b7b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 12 Jul 2018 17:23:07 +0900 Subject: [PATCH 3/3] format-table: fix typo in comment --- src/basic/format-table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/format-table.c b/src/basic/format-table.c index ced3593bc4..89cdd06b2a 100644 --- a/src/basic/format-table.c +++ b/src/basic/format-table.c @@ -36,7 +36,7 @@ that. The first row is always the header row. If header display is turned off we simply skip outputting the first row. Also, when sorting rows we always leave the first row where it is, as the header shouldn't move. - - Note because there's no row and no column object some properties that might be approproate as row/column properties + - Note because there's no row and no column object some properties that might be appropriate as row/column properties are exposed as cell properties instead. For example, the "weight" of a column (which is used to determine where to add/remove space preferable when expanding/compressing tables horizontally) is actually made the "weight" of a cell. Given that we usually need it per-column though we will calculate the average across every cell of the column