Blame SOURCES/0136-format-table-add-option-to-uppercase-cells-on-displa.patch

a3e2b5
From 1d3c6e3c0937ac56a51594a3b6908a801fa9ac5c Mon Sep 17 00:00:00 2001
a3e2b5
From: Lennart Poettering <lennart@poettering.net>
a3e2b5
Date: Mon, 3 Dec 2018 21:36:26 +0100
a3e2b5
Subject: [PATCH] format-table: add option to uppercase cells on display
a3e2b5
a3e2b5
This adds a per-cell option for uppercasing displayed strings.
a3e2b5
Implicitly turn this on for the header row. The fact that we format the
a3e2b5
table header in uppercase is a formatting thing after all, hence should
a3e2b5
be applied by the formatter, i.e. the table display code.
a3e2b5
a3e2b5
Moreover, this provides us with the benefit that we can more nicely
a3e2b5
reuse the specified table headers as JSON field names, like we already
a3e2b5
do: json field names are usually not uppercase.
a3e2b5
a3e2b5
(cherry picked from commit 359abf6dd05aa6bca3438e9c969ed904bd3d447d)
a3e2b5
a3e2b5
Related: #1689832
a3e2b5
---
a3e2b5
 src/basic/format-table.c | 62 ++++++++++++++++++++++++++++++++--------
a3e2b5
 src/basic/format-table.h |  1 +
a3e2b5
 2 files changed, 51 insertions(+), 12 deletions(-)
a3e2b5
a3e2b5
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
a3e2b5
index cfb8aadbda..fe2201ee5f 100644
a3e2b5
--- a/src/basic/format-table.c
a3e2b5
+++ b/src/basic/format-table.c
a3e2b5
@@ -1,5 +1,6 @@
a3e2b5
 /* SPDX-License-Identifier: LGPL-2.1+ */
a3e2b5
 
a3e2b5
+#include <ctype.h>
a3e2b5
 #include <stdio_ext.h>
a3e2b5
 
a3e2b5
 #include "alloc-util.h"
a3e2b5
@@ -58,6 +59,8 @@ typedef struct TableData {
a3e2b5
         unsigned ellipsize_percent; /* 0 … 100, where to place the ellipsis when compression is needed */
a3e2b5
         unsigned align_percent;     /* 0 … 100, where to pad with spaces when expanding is needed. 0: left-aligned, 100: right-aligned */
a3e2b5
 
a3e2b5
+        bool uppercase;             /* Uppercase string on display */
a3e2b5
+
a3e2b5
         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 */
a3e2b5
         char *url;                  /* A URL to use for a clickable hyperlink */
a3e2b5
         char *formatted;            /* A cached textual representation of the cell data, before ellipsation/alignment */
a3e2b5
@@ -132,6 +135,7 @@ Table *table_new_raw(size_t n_columns) {
a3e2b5
 Table *table_new_internal(const char *first_header, ...) {
a3e2b5
         _cleanup_(table_unrefp) Table *t = NULL;
a3e2b5
         size_t n_columns = 1;
a3e2b5
+        const char *h;
a3e2b5
         va_list ap;
a3e2b5
         int r;
a3e2b5
 
a3e2b5
@@ -139,8 +143,6 @@ Table *table_new_internal(const char *first_header, ...) {
a3e2b5
 
a3e2b5
         va_start(ap, first_header);
a3e2b5
         for (;;) {
a3e2b5
-                const char *h;
a3e2b5
-
a3e2b5
                 h = va_arg(ap, const char*);
a3e2b5
                 if (!h)
a3e2b5
                         break;
a3e2b5
@@ -153,19 +155,18 @@ Table *table_new_internal(const char *first_header, ...) {
a3e2b5
         if (!t)
a3e2b5
                 return NULL;
a3e2b5
 
a3e2b5
-        r = table_add_cell(t, NULL, TABLE_STRING, first_header);
a3e2b5
-        if (r < 0)
a3e2b5
-                return NULL;
a3e2b5
-
a3e2b5
         va_start(ap, first_header);
a3e2b5
-        for (;;) {
a3e2b5
-                const char *h;
a3e2b5
+        for (h = first_header; h; h = va_arg(ap, const char*)) {
a3e2b5
+                TableCell *cell;
a3e2b5
 
a3e2b5
-                h = va_arg(ap, const char*);
a3e2b5
-                if (!h)
a3e2b5
-                        break;
a3e2b5
+                r = table_add_cell(t, &cell, TABLE_STRING, h);
a3e2b5
+                if (r < 0) {
a3e2b5
+                        va_end(ap);
a3e2b5
+                        return NULL;
a3e2b5
+                }
a3e2b5
 
a3e2b5
-                r = table_add_cell(t, NULL, TABLE_STRING, h);
a3e2b5
+                /* Make the table header uppercase */
a3e2b5
+                r = table_set_uppercase(t, cell, true);
a3e2b5
                 if (r < 0) {
a3e2b5
                         va_end(ap);
a3e2b5
                         return NULL;
a3e2b5
@@ -443,6 +444,7 @@ static int table_dedup_cell(Table *t, TableCell *cell) {
a3e2b5
 
a3e2b5
         nd->color = od->color;
a3e2b5
         nd->url = TAKE_PTR(curl);
a3e2b5
+        nd->uppercase = od->uppercase;
a3e2b5
 
a3e2b5
         table_data_unref(od);
a3e2b5
         t->data[i] = nd;
a3e2b5
@@ -590,6 +592,27 @@ int table_set_url(Table *t, TableCell *cell, const char *url) {
a3e2b5
         return free_and_replace(table_get_data(t, cell)->url, copy);
a3e2b5
 }
a3e2b5
 
a3e2b5
+int table_set_uppercase(Table *t, TableCell *cell, bool b) {
a3e2b5
+        TableData *d;
a3e2b5
+        int r;
a3e2b5
+
a3e2b5
+        assert(t);
a3e2b5
+        assert(cell);
a3e2b5
+
a3e2b5
+        r = table_dedup_cell(t, cell);
a3e2b5
+        if (r < 0)
a3e2b5
+                return r;
a3e2b5
+
a3e2b5
+        assert_se(d = table_get_data(t, cell));
a3e2b5
+
a3e2b5
+        if (d->uppercase == b)
a3e2b5
+                return 0;
a3e2b5
+
a3e2b5
+        d->formatted = mfree(d->formatted);
a3e2b5
+        d->uppercase = b;
a3e2b5
+        return 1;
a3e2b5
+}
a3e2b5
+
a3e2b5
 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data) {
a3e2b5
         _cleanup_free_ char *curl = NULL;
a3e2b5
         TableData *nd, *od;
a3e2b5
@@ -623,6 +646,7 @@ int table_update(Table *t, TableCell *cell, TableDataType type, const void *data
a3e2b5
 
a3e2b5
         nd->color = od->color;
a3e2b5
         nd->url = TAKE_PTR(curl);
a3e2b5
+        nd->uppercase = od->uppercase;
a3e2b5
 
a3e2b5
         table_data_unref(od);
a3e2b5
         t->data[i] = nd;
a3e2b5
@@ -902,6 +926,20 @@ static const char *table_data_format(TableData *d) {
a3e2b5
                 return "";
a3e2b5
 
a3e2b5
         case TABLE_STRING:
a3e2b5
+                if (d->uppercase) {
a3e2b5
+                        char *p, *q;
a3e2b5
+
a3e2b5
+                        d->formatted = new(char, strlen(d->string) + 1);
a3e2b5
+                        if (!d->formatted)
a3e2b5
+                                return NULL;
a3e2b5
+
a3e2b5
+                        for (p = d->string, q = d->formatted; *p; p++, q++)
a3e2b5
+                                *q = (char) toupper((unsigned char) *p);
a3e2b5
+                        *q = 0;
a3e2b5
+
a3e2b5
+                        return d->formatted;
a3e2b5
+                }
a3e2b5
+
a3e2b5
                 return d->string;
a3e2b5
 
a3e2b5
         case TABLE_BOOLEAN:
a3e2b5
diff --git a/src/basic/format-table.h b/src/basic/format-table.h
a3e2b5
index a2bb2e0846..5a076b5383 100644
a3e2b5
--- a/src/basic/format-table.h
a3e2b5
+++ b/src/basic/format-table.h
a3e2b5
@@ -45,6 +45,7 @@ int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
a3e2b5
 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
a3e2b5
 int table_set_color(Table *t, TableCell *cell, const char *color);
a3e2b5
 int table_set_url(Table *t, TableCell *cell, const char *color);
a3e2b5
+int table_set_uppercase(Table *t, TableCell *cell, bool b);
a3e2b5
 
a3e2b5
 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
a3e2b5