]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add ability to set cell attributes within table_add_many()
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Jul 2019 10:43:42 +0000 (12:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Jul 2019 10:43:46 +0000 (12:43 +0200)
table_add_many() is so much shorter and easier to read than
table_add_cell() with its accessors. Let's teach table_add_many() more
tricks, so that reverting to table_add_cell() is not needed that often
anymore.

src/shared/format-table.c
src/shared/format-table.h

index 54ca1972bde65cf0d1bfc9b382b795a655211691..0422709f27a15f1dfd7e85b2d7a41cf7ae800c48 100644 (file)
@@ -678,6 +678,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);
@@ -770,6 +771,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);
@@ -779,7 +829,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;
index 62e2d146db70187a3de9fc3e33490877e406a488..e1c0dab4d0e16d8a62298930f8694869af3d4e87 100644 (file)
@@ -25,6 +25,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;