]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add TABLE_VERSION cell type
authorLennart Poettering <lennart@poettering.net>
Thu, 18 Sep 2025 07:29:27 +0000 (09:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Sep 2025 07:31:35 +0000 (09:31 +0200)
This is exactly like TABLE_STRING, with one exception: it sorts via
strverscmp_improved().

src/shared/format-table.c
src/shared/format-table.h

index c672be326d2a6954259259821a8679b8d24df7bb..c129f260e8c03d67be24ce5cb5c15e01bfcdc3e6 100644 (file)
@@ -287,6 +287,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
         case TABLE_HEADER:
+        case TABLE_VERSION:
                 return strlen(data) + 1;
 
         case TABLE_STRV:
@@ -526,7 +527,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_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER));
+        assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER, TABLE_VERSION));
 
         va_start(ap, format);
         r = vasprintf(&buffer, format, ap);
@@ -934,6 +935,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                 case TABLE_PATH_BASENAME:
                 case TABLE_FIELD:
                 case TABLE_HEADER:
+                case TABLE_VERSION:
                         data = va_arg(ap, const char *);
                         break;
 
@@ -1395,6 +1397,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_PATH_BASENAME:
                         return path_compare(a->string, b->string);
 
+                case TABLE_VERSION:
+                        return strverscmp_improved(a->string, b->string);
+
                 case TABLE_STRV:
                 case TABLE_STRV_WRAPPED:
                         return strv_compare(a->strv, b->strv);
@@ -1579,7 +1584,8 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
         case TABLE_PATH:
         case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
-        case TABLE_HEADER: {
+        case TABLE_HEADER:
+        case TABLE_VERSION: {
                 _cleanup_free_ char *bn = NULL;
                 const char *s;
 
@@ -2753,6 +2759,7 @@ static int table_data_to_json(TableData *d, sd_json_variant **ret) {
         case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
         case TABLE_HEADER:
+        case TABLE_VERSION:
                 return sd_json_variant_new_string(ret, d->string);
 
         case TABLE_STRV:
index beea0cbe1b458cd803edfe1e3b0d8694a90275c2..3365a8feb7bd2707ba2da9c21ea563afb56fb9a5 100644 (file)
@@ -16,6 +16,7 @@ typedef enum TableDataType {
         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_VERSION,             /* just like TABLE_STRING, but uses version comparison when sorting */
         TABLE_BOOLEAN,
         TABLE_BOOLEAN_CHECKMARK,
         TABLE_TIMESTAMP,