shared/format-table: add helpers to query and set column width

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-03-23 14:20:10 +01:00
parent 57a1f1ab96
commit bcaa5acf81
2 changed files with 74 additions and 0 deletions
+69
View 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,
+5
View 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);