]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: support coloring the gap right of each cell individually
authorLennart Poettering <lennart@poettering.net>
Wed, 6 May 2020 13:36:50 +0000 (15:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 May 2020 13:40:15 +0000 (15:40 +0200)
This is useful for underlining a whole row of cells: previously we
couldn't underline the gap. Now we can.

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

index cef96343abd98e5fea857789e0050128b3408fab..f12ad2d03850e69c3abb8c3550abbe7467aedda6 100644 (file)
@@ -73,6 +73,7 @@ typedef struct TableData {
         bool uppercase;             /* Uppercase string on display */
 
         const char *color;          /* ANSI color string to use for this cell. When written to terminal should not move cursor. Will automatically be reset after the cell */
+        const char *rgap_color;     /* The ANSI color to use for the gap right of this cell. Usually used to underline entire rows in a gapless fashion */
         char *url;                  /* A URL to use for a clickable hyperlink */
         char *formatted;            /* A cached textual representation of the cell data, before ellipsation/alignment */
 
@@ -334,7 +335,7 @@ static bool table_data_matches(
                 return false;
 
         /* If a color/url/uppercase flag is set, refuse to merge */
-        if (d->color)
+        if (d->color || d->rgap_color)
                 return false;
         if (d->url)
                 return false;
@@ -542,6 +543,7 @@ static int table_dedup_cell(Table *t, TableCell *cell) {
                 return -ENOMEM;
 
         nd->color = od->color;
+        nd->rgap_color = od->rgap_color;
         nd->url = TAKE_PTR(curl);
         nd->uppercase = od->uppercase;
 
@@ -671,6 +673,20 @@ int table_set_color(Table *t, TableCell *cell, const char *color) {
         return 0;
 }
 
+int table_set_rgap_color(Table *t, TableCell *cell, const char *color) {
+        int r;
+
+        assert(t);
+        assert(cell);
+
+        r = table_dedup_cell(t, cell);
+        if (r < 0)
+                return r;
+
+        table_get_data(t, cell)->rgap_color = empty_to_null(color);
+        return 0;
+}
+
 int table_set_url(Table *t, TableCell *cell, const char *url) {
         _cleanup_free_ char *copy = NULL;
         int r;
@@ -744,6 +760,7 @@ int table_update(Table *t, TableCell *cell, TableDataType type, const void *data
                 return -ENOMEM;
 
         nd->color = od->color;
+        nd->rgap_color = od->rgap_color;
         nd->url = TAKE_PTR(curl);
         nd->uppercase = od->uppercase;
 
@@ -952,6 +969,25 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         break;
                 }
 
+                case TABLE_SET_RGAP_COLOR: {
+                        const char *c = va_arg(ap, const char*);
+                        r = table_set_rgap_color(t, last_cell, c);
+                        break;
+                }
+
+                case TABLE_SET_BOTH_COLORS: {
+                        const char *c = va_arg(ap, const char*);
+
+                        r = table_set_color(t, last_cell, c);
+                        if (r < 0) {
+                                va_end(ap);
+                                return r;
+                        }
+
+                        r = table_set_rgap_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);
@@ -1724,6 +1760,15 @@ static const char* table_data_color(TableData *d) {
         return NULL;
 }
 
+static const char* table_data_rgap_color(TableData *d) {
+        assert(d);
+
+        if (d->rgap_color)
+                return d->rgap_color;
+
+        return NULL;
+}
+
 int table_print(Table *t, FILE *f) {
         size_t n_rows, *minimum_width, *maximum_width, display_columns, *requested_width,
                 i, j, table_minimum_width, table_maximum_width, table_requested_width, table_effective_width,
@@ -1975,12 +2020,13 @@ int table_print(Table *t, FILE *f) {
                         row = t->data + i * t->n_columns;
 
                 do {
+                        const char *gap_color = NULL;
                         more_sublines = false;
 
                         for (j = 0; j < display_columns; j++) {
                                 _cleanup_free_ char *buffer = NULL, *extracted = NULL;
                                 bool lines_truncated = false;
-                                const char *field;
+                                const char *field, *color = NULL;
                                 TableData *d;
                                 size_t l;
 
@@ -2059,23 +2105,35 @@ int table_print(Table *t, FILE *f) {
                                         field = buffer;
                                 }
 
-                                if (row == t->data) /* underline header line fully, including the column separator */
-                                        fputs(ansi_underline(), f);
+                                if (colors_enabled()) {
+                                        if (gap_color)
+                                                fputs(gap_color, f);
+                                        else if (row == t->data) /* underline header line fully, including the column separator */
+                                                fputs(ansi_underline(), f);
+                                }
 
                                 if (j > 0)
-                                        fputc(' ', f); /* column separator */
+                                        fputc(' ', f); /* column separator left of cell */
 
-                                if (table_data_color(d) && colors_enabled()) {
-                                        if (row == t->data) /* first undo header underliner */
+                                if (colors_enabled()) {
+                                        color = table_data_color(d);
+
+                                        /* Undo gap color */
+                                        if (gap_color || (color && row == t->data))
                                                 fputs(ANSI_NORMAL, f);
 
-                                        fputs(table_data_color(d), f);
+                                        if (color)
+                                                fputs(color, f);
+                                        else if (gap_color && row == t->data) /* underline header line cell */
+                                                fputs(ansi_underline(), f);
                                 }
 
                                 fputs(field, f);
 
-                                if (colors_enabled() && (table_data_color(d) || row == t->data))
+                                if (colors_enabled() && (color || row == t->data))
                                         fputs(ANSI_NORMAL, f);
+
+                                gap_color = table_data_rgap_color(d);
                         }
 
                         fputc('\n', f);
index 62f1ed740d90296afe0c2f5aa3423632232aae52..20b4ebd39b584400e129799722e095e0524a3c07 100644 (file)
@@ -47,6 +47,8 @@ typedef enum TableDataType {
         TABLE_SET_ALIGN_PERCENT,
         TABLE_SET_ELLIPSIZE_PERCENT,
         TABLE_SET_COLOR,
+        TABLE_SET_RGAP_COLOR,
+        TABLE_SET_BOTH_COLORS,
         TABLE_SET_URL,
         TABLE_SET_UPPERCASE,
 
@@ -89,6 +91,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_rgap_color(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);