Merge pull request #8709 from poettering/format-table
generic table formatter
This commit is contained in:
@@ -178,6 +178,10 @@ tests += [
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-format-table.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-ratelimit.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
@@ -17,6 +17,30 @@ static void test_one(const char *p) {
|
||||
_cleanup_free_ char *t;
|
||||
t = ellipsize(p, columns(), 70);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, columns(), 0);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, columns(), 100);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 0, 50);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 1, 50);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 2, 50);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 3, 50);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 4, 50);
|
||||
puts(t);
|
||||
free(t);
|
||||
t = ellipsize(p, 5, 50);
|
||||
puts(t);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -26,6 +26,8 @@ static void test_get_locales(void) {
|
||||
}
|
||||
|
||||
static void test_locale_is_valid(void) {
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
assert_se(locale_is_valid("en_EN.utf8"));
|
||||
assert_se(locale_is_valid("fr_FR.utf8"));
|
||||
assert_se(locale_is_valid("fr_FR@euro"));
|
||||
@@ -43,6 +45,8 @@ static void test_keymaps(void) {
|
||||
char **p;
|
||||
int r;
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
assert_se(!keymap_is_valid(""));
|
||||
assert_se(!keymap_is_valid("/usr/bin/foo"));
|
||||
assert_se(!keymap_is_valid("\x01gar\x02 bage\x03"));
|
||||
@@ -65,11 +69,31 @@ static void test_keymaps(void) {
|
||||
assert_se(keymap_is_valid("unicode"));
|
||||
}
|
||||
|
||||
#define dump_glyph(x) log_info(STRINGIFY(x) ": %s", special_glyph(x))
|
||||
static void dump_special_glyphs(void) {
|
||||
assert_cc(ELLIPSIS + 1 == _SPECIAL_GLYPH_MAX);
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
log_info("is_locale_utf8: %s", yes_no(is_locale_utf8()));
|
||||
|
||||
dump_glyph(TREE_VERTICAL);
|
||||
dump_glyph(TREE_BRANCH);
|
||||
dump_glyph(TREE_RIGHT);
|
||||
dump_glyph(TREE_SPACE);
|
||||
dump_glyph(TRIANGULAR_BULLET);
|
||||
dump_glyph(BLACK_CIRCLE);
|
||||
dump_glyph(ARROW);
|
||||
dump_glyph(MDASH);
|
||||
dump_glyph(ELLIPSIS);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_get_locales();
|
||||
test_locale_is_valid();
|
||||
|
||||
test_keymaps();
|
||||
|
||||
dump_special_glyphs();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -102,6 +102,15 @@ static void test_utf8_n_codepoints(void) {
|
||||
assert_se(utf8_n_codepoints("\xF1") == (size_t) -1);
|
||||
}
|
||||
|
||||
static void test_utf8_console_width(void) {
|
||||
assert_se(utf8_console_width("abc") == 3);
|
||||
assert_se(utf8_console_width("zażółcić gęślą jaźń") == 19);
|
||||
assert_se(utf8_console_width("串") == 2);
|
||||
assert_se(utf8_console_width("") == 0);
|
||||
assert_se(utf8_console_width("…👊🔪💐…") == 8);
|
||||
assert_se(utf8_console_width("\xF1") == (size_t) -1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_utf8_is_valid();
|
||||
test_utf8_is_printable();
|
||||
@@ -111,6 +120,7 @@ int main(int argc, char *argv[]) {
|
||||
test_utf8_escaping_printable();
|
||||
test_utf16_to_utf8();
|
||||
test_utf8_n_codepoints();
|
||||
test_utf8_console_width();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user