From 3536a2757a3ec75a3e1c01db8efed88ee4a7b216 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Dec 15 2020 09:45:32 +0000 Subject: format-table: add table_update() to update existing entries (cherry picked from commit 27e730e6d0a7709c17ccef170f10846e92dca2a0) Related: #1689832 patch_name: 0130-format-table-add-table_update-to-update-existing-ent.patch present_in_specfile: true location_in_specfile: 130 squash_commits: true --- diff --git a/src/basic/format-table.c b/src/basic/format-table.c index a3ff527..302642d 100644 --- a/src/basic/format-table.c +++ b/src/basic/format-table.c @@ -590,6 +590,46 @@ int table_set_url(Table *t, TableCell *cell, const char *url) { return free_and_replace(table_get_data(t, cell)->url, copy); } +int table_update(Table *t, TableCell *cell, TableDataType type, const void *data) { + _cleanup_free_ char *curl = NULL; + TableData *nd, *od; + size_t i; + + assert(t); + assert(cell); + + i = TABLE_CELL_TO_INDEX(cell); + if (i >= t->n_cells) + return -ENXIO; + + assert_se(od = t->data[i]); + + if (od->url) { + curl = strdup(od->url); + if (!curl) + return -ENOMEM; + } + + nd = table_data_new( + type, + data, + od->minimum_width, + od->maximum_width, + od->weight, + od->align_percent, + od->ellipsize_percent); + if (!nd) + return -ENOMEM; + + nd->color = od->color; + nd->url = TAKE_PTR(curl); + + table_data_unref(od); + t->data[i] = nd; + + return 0; +} + int table_add_many_internal(Table *t, TableDataType first_type, ...) { TableDataType type; va_list ap; diff --git a/src/basic/format-table.h b/src/basic/format-table.h index 07cb235..4273c8c 100644 --- a/src/basic/format-table.h +++ b/src/basic/format-table.h @@ -46,6 +46,8 @@ int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent); int table_set_color(Table *t, TableCell *cell, const char *color); int table_set_url(Table *t, TableCell *cell, const char *color); +int table_update(Table *t, TableCell *cell, TableDataType type, const void *data); + int table_add_many_internal(Table *t, TableDataType first_type, ...); #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)