]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: optionally show a specific string in empty cells
authorLennart Poettering <lennart@poettering.net>
Mon, 29 Jul 2019 16:36:19 +0000 (18:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 29 Jul 2019 16:48:08 +0000 (18:48 +0200)
For some cases it might make sense to show "-" instead of just spaces
for empty cells.

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

index aede59bf3463d38e23495bb0342d5f8cbf073ca4..51ddd1ac519806d8c8867cf9150ca9516a028312 100644 (file)
@@ -121,6 +121,8 @@ struct Table {
         size_t n_sort_map;
 
         bool *reverse_map;
+
+        char *empty_string;
 };
 
 Table *table_new_raw(size_t n_columns) {
@@ -212,6 +214,7 @@ Table *table_unref(Table *t) {
         free(t->display_map);
         free(t->sort_map);
         free(t->reverse_map);
+        free(t->empty_string);
 
         return mfree(t);
 }
@@ -859,6 +862,12 @@ void table_set_width(Table *t, size_t width) {
         t->width = width;
 }
 
+int table_set_empty_string(Table *t, const char *empty) {
+        assert(t);
+
+        return free_and_strdup(&t->empty_string, empty);
+}
+
 int table_set_display(Table *t, size_t first_column, ...) {
         size_t allocated, column;
         va_list ap;
@@ -1016,7 +1025,7 @@ static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
         return CMP(*a, *b);
 }
 
-static const char *table_data_format(TableData *d) {
+static const char *table_data_format(Table *t, TableData *d) {
         assert(d);
 
         if (d->formatted)
@@ -1024,7 +1033,7 @@ static const char *table_data_format(TableData *d) {
 
         switch (d->type) {
         case TABLE_EMPTY:
-                return "";
+                return strempty(t->empty_string);
 
         case TABLE_STRING:
                 if (d->uppercase) {
@@ -1225,11 +1234,11 @@ static const char *table_data_format(TableData *d) {
         return d->formatted;
 }
 
-static int table_data_requested_width(TableData *d, size_t *ret) {
+static int table_data_requested_width(Table *table, TableData *d, size_t *ret) {
         const char *t;
         size_t l;
 
-        t = table_data_format(d);
+        t = table_data_format(table, d);
         if (!t)
                 return -ENOMEM;
 
@@ -1372,7 +1381,7 @@ int table_print(Table *t, FILE *f) {
 
                         assert_se(d = row[t->display_map ? t->display_map[j] : j]);
 
-                        r = table_data_requested_width(d, &req);
+                        r = table_data_requested_width(t, d, &req);
                         if (r < 0)
                                 return r;
 
@@ -1539,7 +1548,7 @@ int table_print(Table *t, FILE *f) {
 
                         assert_se(d = row[t->display_map ? t->display_map[j] : j]);
 
-                        field = table_data_format(d);
+                        field = table_data_format(t, d);
                         if (!field)
                                 return -ENOMEM;
 
index 452f1706c0cea8d0f82bf96e5f38bd39b79b1754..8afb4257a8a4bdb9668d008e808af260fc689044 100644 (file)
@@ -77,6 +77,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...);
 
 void table_set_header(Table *table, bool b);
 void table_set_width(Table *t, size_t width);
+int table_set_empty_string(Table *t, const char *empty);
 int table_set_display(Table *t, size_t first_column, ...);
 int table_set_sort(Table *t, size_t first_column, ...);
 int table_set_reverse(Table *t, size_t column, bool b);