1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-28 20:25:38 +03:00

basic: add minimalistic table formatter

We have plenty of code in our codebase that outputs tables to the
console, and all is homegrown and awful. Let's replace it with a generic
implementation that can do automatically what the old implementations
did manually.

Features:

1. Ellipsation (for fields overly long) and alignment (for
   fields overly short)

2. Sorting of rows

3. automatically copies formatting from the same cell in the row above

4. Heavy use of varargs to make putting together tables easy

5. can expand and compress tables, with weights

6. Has a minimal understanding of unicode wide characters in order to
   match unicode strings to character cell terminals.

7. Columns can be reordered and individually turned off.

8. pretty printing for various data types

And more.
This commit is contained in:
Lennart Poettering 2018-04-11 20:03:39 +02:00
parent a89e30ecb4
commit 1960e73611
5 changed files with 1454 additions and 0 deletions

1247
src/basic/format-table.c Normal file

File diff suppressed because it is too large Load Diff

62
src/basic/format-table.h Normal file
View File

@ -0,0 +1,62 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include "macro.h"
typedef enum TableDataType {
TABLE_EMPTY,
TABLE_STRING,
TABLE_BOOLEAN,
TABLE_TIMESTAMP,
TABLE_TIMESPAN,
TABLE_SIZE,
TABLE_UINT32,
_TABLE_DATA_TYPE_MAX,
_TABLE_DATA_TYPE_INVALID = -1,
} TableDataType;
typedef struct Table Table;
typedef struct TableCell TableCell;
Table *table_new_internal(const char *first_header, ...) _sentinel_;
#define table_new(...) table_new_internal(__VA_ARGS__, NULL)
Table *table_new_raw(size_t n_columns);
Table *table_unref(Table *t);
DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType type, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent);
static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
}
int table_dup_cell(Table *t, TableCell *cell);
int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
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_add_many_internal(Table *t, TableDataType first_type, ...);
#define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
void table_set_header(Table *table, bool b);
void table_set_width(Table *t, size_t width);
int table_set_display(Table *t, size_t first_column, ...);
int table_set_sort(Table *t, size_t first_column, ...);
int table_print(Table *t, FILE *f);
int table_format(Table *t, char **ret);
static inline TableCell* TABLE_HEADER_CELL(size_t i) {
return SIZE_TO_PTR(i + 1);
}
size_t table_get_rows(Table *t);
size_t table_get_columns(Table *t);

View File

@ -77,6 +77,8 @@ basic_sources = files('''
fileio-label.h
fileio.c
fileio.h
format-table.c
format-table.h
format-util.h
fs-util.c
fs-util.h

View File

@ -178,6 +178,10 @@ tests += [
[],
[]],
[['src/test/test-format-table.c'],
[],
[]],
[['src/test/test-ratelimit.c'],
[],
[]],

View File

@ -0,0 +1,139 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "format-table.h"
#include "string-util.h"
#include "time-util.h"
int main(int argc, char *argv[]) {
_cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *formatted = NULL;
assert_se(setenv("COLUMNS", "40", 1) >= 0);
assert_se(t = table_new("ONE", "TWO", "THREE"));
assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
assert_se(table_add_many(t,
TABLE_STRING, "xxx",
TABLE_STRING, "yyy",
TABLE_BOOLEAN, true) >= 0);
assert_se(table_add_many(t,
TABLE_STRING, "a long field",
TABLE_STRING, "yyy",
TABLE_BOOLEAN, false) >= 0);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"ONE TWO THREE\n"
"xxx yyy yes\n"
"a long field yyy no\n"));
formatted = mfree(formatted);
table_set_width(t, 40);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"ONE TWO THREE\n"
"xxx yyy yes\n"
"a long field yyy no\n"));
formatted = mfree(formatted);
table_set_width(t, 12);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"ONE TWO THR…\n"
"xxx yyy yes\n"
"a … yyy no\n"));
formatted = mfree(formatted);
table_set_width(t, 5);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"… … …\n"
"… … …\n"
"… … …\n"));
formatted = mfree(formatted);
table_set_width(t, 3);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"… … …\n"
"… … …\n"
"… … …\n"));
formatted = mfree(formatted);
table_set_width(t, (size_t) -1);
assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"ONE TWO THREE\n"
"a long field yyy no\n"
"xxx yyy yes\n"));
formatted = mfree(formatted);
table_set_header(t, false);
assert_se(table_add_many(t,
TABLE_STRING, "fäää",
TABLE_STRING, "uuu",
TABLE_BOOLEAN, true) >= 0);
assert_se(table_add_many(t,
TABLE_STRING, "fäää",
TABLE_STRING, "zzz",
TABLE_BOOLEAN, false) >= 0);
assert_se(table_add_many(t,
TABLE_EMPTY,
TABLE_SIZE, (uint64_t) 4711,
TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
"a long field yyy no\n"
"fäää zzz no\n"
"fäää uuu yes\n"
"xxx yyy yes\n"
" 4.6K 5min\n"));
formatted = mfree(formatted);
assert_se(table_set_display(t, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, (size_t) -1) >= 0);
assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted);
assert_se(streq(formatted,
" no a long f… no a long f… a long fi…\n"
" no fäää no fäää fäää \n"
" yes fäää yes fäää fäää \n"
" yes xxx yes xxx xxx \n"
"5min 5min \n"));
return 0;
}