diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-12-03 21:36:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-12-03 22:42:38 +0100 |
commit | 359abf6dd05aa6bca3438e9c969ed904bd3d447d (patch) | |
tree | fd0d902050341e51e07cb78b34a3bc5839c98c4e /src/shared/format-table.c | |
parent | machinectl: optionally output table in JSON (diff) | |
download | systemd-359abf6dd05aa6bca3438e9c969ed904bd3d447d.tar.xz systemd-359abf6dd05aa6bca3438e9c969ed904bd3d447d.zip |
format-table: add option to uppercase cells on display
This adds a per-cell option for uppercasing displayed strings.
Implicitly turn this on for the header row. The fact that we format the
table header in uppercase is a formatting thing after all, hence should
be applied by the formatter, i.e. the table display code.
Moreover, this provides us with the benefit that we can more nicely
reuse the specified table headers as JSON field names, like we already
do: json field names are usually not uppercase.
Diffstat (limited to 'src/shared/format-table.c')
-rw-r--r-- | src/shared/format-table.c | 62 |
1 files changed, 50 insertions, 12 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 55767580ca..6fdb583a72 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include <ctype.h> #include <stdio_ext.h> #include "alloc-util.h" @@ -58,6 +59,8 @@ typedef struct TableData { unsigned ellipsize_percent; /* 0 … 100, where to place the ellipsis when compression is needed */ unsigned align_percent; /* 0 … 100, where to pad with spaces when expanding is needed. 0: left-aligned, 100: right-aligned */ + 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 */ char *url; /* A URL to use for a clickable hyperlink */ char *formatted; /* A cached textual representation of the cell data, before ellipsation/alignment */ @@ -132,6 +135,7 @@ Table *table_new_raw(size_t n_columns) { Table *table_new_internal(const char *first_header, ...) { _cleanup_(table_unrefp) Table *t = NULL; size_t n_columns = 1; + const char *h; va_list ap; int r; @@ -139,8 +143,6 @@ Table *table_new_internal(const char *first_header, ...) { va_start(ap, first_header); for (;;) { - const char *h; - h = va_arg(ap, const char*); if (!h) break; @@ -153,19 +155,18 @@ Table *table_new_internal(const char *first_header, ...) { if (!t) return NULL; - r = table_add_cell(t, NULL, TABLE_STRING, first_header); - if (r < 0) - return NULL; - va_start(ap, first_header); - for (;;) { - const char *h; + for (h = first_header; h; h = va_arg(ap, const char*)) { + TableCell *cell; - h = va_arg(ap, const char*); - if (!h) - break; + r = table_add_cell(t, &cell, TABLE_STRING, h); + if (r < 0) { + va_end(ap); + return NULL; + } - r = table_add_cell(t, NULL, TABLE_STRING, h); + /* Make the table header uppercase */ + r = table_set_uppercase(t, cell, true); if (r < 0) { va_end(ap); return NULL; @@ -427,6 +428,7 @@ static int table_dedup_cell(Table *t, TableCell *cell) { nd->color = od->color; nd->url = TAKE_PTR(curl); + nd->uppercase = od->uppercase; table_data_unref(od); t->data[i] = nd; @@ -574,6 +576,27 @@ int table_set_url(Table *t, TableCell *cell, const char *url) { return free_and_replace(table_get_data(t, cell)->url, copy); } +int table_set_uppercase(Table *t, TableCell *cell, bool b) { + TableData *d; + int r; + + assert(t); + assert(cell); + + r = table_dedup_cell(t, cell); + if (r < 0) + return r; + + assert_se(d = table_get_data(t, cell)); + + if (d->uppercase == b) + return 0; + + d->formatted = mfree(d->formatted); + d->uppercase = b; + return 1; +} + int table_update(Table *t, TableCell *cell, TableDataType type, const void *data) { _cleanup_free_ char *curl = NULL; TableData *nd, *od; @@ -607,6 +630,7 @@ int table_update(Table *t, TableCell *cell, TableDataType type, const void *data nd->color = od->color; nd->url = TAKE_PTR(curl); + nd->uppercase = od->uppercase; table_data_unref(od); t->data[i] = nd; @@ -858,6 +882,20 @@ static const char *table_data_format(TableData *d) { return ""; case TABLE_STRING: + if (d->uppercase) { + char *p, *q; + + d->formatted = new(char, strlen(d->string) + 1); + if (!d->formatted) + return NULL; + + for (p = d->string, q = d->formatted; *p; p++, q++) + *q = (char) toupper((unsigned char) *p); + *q = 0; + + return d->formatted; + } + return d->string; case TABLE_BOOLEAN: |