]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/format-table: add helpers to query and set column width
authorZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Mon, 2 Mar 2026 14:35:41 +0000 (15:35 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
Mon, 23 Mar 2026 13:20:10 +0000 (14:20 +0100)
src/shared/format-table.c
src/shared/format-table.h

index f400602b343d5f1394560bb16a9d78cb34a0eda7..279e7fda68e7f8da1e9304ebe7c0bd7f71f432f9 100644 (file)
@@ -2139,6 +2139,75 @@ static const char* table_data_rgap_underline(const TableData *d) {
         return NULL;
 }
 
+int table_data_requested_width(Table *table, size_t column, size_t *ret) {
+        size_t width = 0;
+        int r;
+
+        assert(table);
+        assert(ret);
+
+        for (size_t row = 0; row < table_get_rows(table); row++) {
+                TableCell *cell = table_get_cell(table, row, column);
+                if (!cell)
+                        continue;
+
+                TableData *data = table_get_data(table, cell);
+                if (!data)
+                        continue;
+
+                size_t w;
+
+                r = table_data_requested_width_height(
+                                table, data, SIZE_MAX, &w, /* ret_height= */ NULL, /* have_soft= */ NULL);
+                if (r < 0)
+                        return r;
+
+                width = MAX(width, w);
+        }
+
+        *ret = width;
+        return 0;
+}
+
+int table_set_column_width(Table *t, size_t column, size_t width) {
+        int r = 0;
+
+        assert(t);
+
+        for (size_t row = 0; row < table_get_rows(t); row++) {
+                TableCell *cell = table_get_cell(t, row, column);
+                if (!cell)
+                        continue;
+
+                RET_GATHER(r, table_set_minimum_width(t, cell, width));
+        }
+
+        return r;
+}
+
+int table_sync_column_width(Table *a, size_t column_a, Table *b, size_t column_b) {
+        size_t w1, w2;
+        int r;
+
+        assert(a);
+        assert(b);
+
+        /* Make both tables have specified columns of same width */
+
+        r = table_data_requested_width(a, column_a, &w1);
+        if (r < 0)
+                return log_error_errno(r, "Failed to query table column width: %m");
+
+        r = table_data_requested_width(b, column_b, &w2);
+        if (r < 0)
+                return log_error_errno(r, "Failed to query table column width: %m");
+
+        r = 0;
+        RET_GATHER(r, table_set_column_width(a, column_a, MAX(w1, w2)));
+        RET_GATHER(r, table_set_column_width(b, column_b, MAX(w1, w2)));
+        return r;
+}
+
 int table_print(Table *t, FILE *f) {
         size_t n_rows, *minimum_width, *maximum_width, display_columns, *requested_width,
                 table_minimum_width, table_maximum_width, table_requested_width, table_effective_width,
index 997ac20eb6882dce0656f5b804d7fdbe4c73967f..9a11fb7c30cefd1416daaa2ade1bb0f72a3236ad 100644 (file)
@@ -141,6 +141,11 @@ int table_set_reverse(Table *t, size_t column, bool b);
 int table_hide_column_from_display_internal(Table *t, ...);
 #define table_hide_column_from_display(t, ...) table_hide_column_from_display_internal(t, __VA_ARGS__, SIZE_MAX)
 
+int table_data_requested_width(Table *table, size_t column, size_t *ret);
+
+int table_set_column_width(Table *t, size_t column, size_t width);
+int table_sync_column_width(Table *a, size_t column_a, Table *b, size_t column_b);
+
 int table_print(Table *t, FILE *f);
 int table_format(Table *t, char **ret);