mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-03-08 20:58:20 +03:00
format-table: add field type TABLE_PATH_BASENAME
This is just like TABLE_PATH, but only shows the basename in regular tabular output. This is useful in systemd-repart for example
This commit is contained in:
parent
a67bacff34
commit
d0242ac946
@ -2461,7 +2461,7 @@ static int context_dump_partitions(Context *context) {
|
||||
TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer),
|
||||
TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
|
||||
TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
|
||||
TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
|
||||
TABLE_PATH_BASENAME, p->definition_path, TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
|
||||
TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
|
||||
TABLE_UINT64, p->offset,
|
||||
TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
|
||||
|
@ -281,6 +281,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
|
||||
|
||||
case TABLE_STRING:
|
||||
case TABLE_PATH:
|
||||
case TABLE_PATH_BASENAME:
|
||||
case TABLE_FIELD:
|
||||
case TABLE_HEADER:
|
||||
return strlen(data) + 1;
|
||||
@ -513,7 +514,7 @@ int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType dt
|
||||
int r;
|
||||
|
||||
assert(t);
|
||||
assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_FIELD, TABLE_HEADER));
|
||||
assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER));
|
||||
|
||||
va_start(ap, format);
|
||||
r = vasprintf(&buffer, format, ap);
|
||||
@ -873,6 +874,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
|
||||
|
||||
case TABLE_STRING:
|
||||
case TABLE_PATH:
|
||||
case TABLE_PATH_BASENAME:
|
||||
case TABLE_FIELD:
|
||||
case TABLE_HEADER:
|
||||
data = va_arg(ap, const char *);
|
||||
@ -1281,6 +1283,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
|
||||
return strcmp(a->string, b->string);
|
||||
|
||||
case TABLE_PATH:
|
||||
case TABLE_PATH_BASENAME:
|
||||
return path_compare(a->string, b->string);
|
||||
|
||||
case TABLE_STRV:
|
||||
@ -1453,15 +1456,24 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
|
||||
|
||||
case TABLE_STRING:
|
||||
case TABLE_PATH:
|
||||
case TABLE_PATH_BASENAME:
|
||||
case TABLE_FIELD:
|
||||
case TABLE_HEADER:
|
||||
case TABLE_HEADER: {
|
||||
_cleanup_free_ char *bn = NULL;
|
||||
const char *s;
|
||||
|
||||
if (d->type == TABLE_PATH_BASENAME)
|
||||
s = path_extract_filename(d->string, &bn) < 0 ? d->string : bn;
|
||||
else
|
||||
s = d->string;
|
||||
|
||||
if (d->uppercase && !avoid_uppercasing) {
|
||||
d->formatted = new(char, strlen(d->string) + (d->type == TABLE_FIELD) + 1);
|
||||
d->formatted = new(char, strlen(s) + (d->type == TABLE_FIELD) + 1);
|
||||
if (!d->formatted)
|
||||
return NULL;
|
||||
|
||||
char *q = d->formatted;
|
||||
for (char *p = d->string; *p; p++)
|
||||
for (const char *p = s; *p; p++)
|
||||
*(q++) = (char) toupper((unsigned char) *p);
|
||||
|
||||
if (d->type == TABLE_FIELD)
|
||||
@ -1470,14 +1482,20 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
|
||||
*q = 0;
|
||||
return d->formatted;
|
||||
} else if (d->type == TABLE_FIELD) {
|
||||
d->formatted = strjoin(d->string, ":");
|
||||
d->formatted = strjoin(s, ":");
|
||||
if (!d->formatted)
|
||||
return NULL;
|
||||
|
||||
return d->formatted;
|
||||
}
|
||||
|
||||
if (bn) {
|
||||
d->formatted = TAKE_PTR(bn);
|
||||
return d->formatted;
|
||||
}
|
||||
|
||||
return d->string;
|
||||
}
|
||||
|
||||
case TABLE_STRV:
|
||||
if (strv_isempty(d->strv))
|
||||
@ -2544,6 +2562,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
|
||||
|
||||
case TABLE_STRING:
|
||||
case TABLE_PATH:
|
||||
case TABLE_PATH_BASENAME:
|
||||
case TABLE_FIELD:
|
||||
case TABLE_HEADER:
|
||||
return json_variant_new_string(ret, d->string);
|
||||
|
@ -17,6 +17,7 @@ typedef enum TableDataType {
|
||||
TABLE_STRV,
|
||||
TABLE_STRV_WRAPPED,
|
||||
TABLE_PATH,
|
||||
TABLE_PATH_BASENAME, /* like TABLE_PATH, but display only last path element (i.e. the "basename") in regular output */
|
||||
TABLE_BOOLEAN,
|
||||
TABLE_BOOLEAN_CHECKMARK,
|
||||
TABLE_TIMESTAMP,
|
||||
|
@ -565,6 +565,24 @@ TEST(vertical) {
|
||||
assert_se(json_variant_equal(a, b));
|
||||
}
|
||||
|
||||
TEST(path_basename) {
|
||||
_cleanup_(table_unrefp) Table *t = NULL;
|
||||
_cleanup_free_ char *formatted = NULL;
|
||||
|
||||
assert_se(t = table_new("x"));
|
||||
|
||||
table_set_header(t, false);
|
||||
|
||||
assert_se(table_add_many(t,
|
||||
TABLE_PATH_BASENAME, "/foo/bar",
|
||||
TABLE_PATH_BASENAME, "/quux/bar",
|
||||
TABLE_PATH_BASENAME, "/foo/baz") >= 0);
|
||||
|
||||
assert_se(table_format(t, &formatted) >= 0);
|
||||
|
||||
assert_se(streq(formatted, "bar\nbar\nbaz\n"));
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
|
||||
assert_se(setenv("COLUMNS", "40", 1) >= 0);
|
||||
|
@ -381,7 +381,7 @@ EOF
|
||||
"type" : "swap",
|
||||
"label" : "label2",
|
||||
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
|
||||
"file" : "root.conf",
|
||||
"file" : "$defs/root.conf",
|
||||
"node" : "$imgs/zzz1",
|
||||
"offset" : 1048576,
|
||||
"old_size" : 0,
|
||||
@ -441,7 +441,7 @@ EOF
|
||||
"type" : "swap",
|
||||
"label" : "label1",
|
||||
"uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
|
||||
"file" : "root1.conf",
|
||||
"file" : "$defs/1/root1.conf",
|
||||
"node" : "$imgs/zzz1",
|
||||
"offset" : 1048576,
|
||||
"old_size" : 0,
|
||||
@ -456,7 +456,7 @@ EOF
|
||||
"type" : "swap",
|
||||
"label" : "label2",
|
||||
"uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
|
||||
"file" : "root2.conf",
|
||||
"file" : "$defs/2/root2.conf",
|
||||
"node" : "$imgs/zzz2",
|
||||
"offset" : 34603008,
|
||||
"old_size" : 0,
|
||||
|
Loading…
x
Reference in New Issue
Block a user