From a53afb337985f8f1c7fe8f620fe30cec87f554d5 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Thu, 8 Nov 2018 21:17:47 +0100
Subject: [PATCH] format-table: add table_update() to update existing entries

(cherry picked from commit 27e730e6d0a7709c17ccef170f10846e92dca2a0)

Related: #1689832
---
 src/basic/format-table.c | 40 ++++++++++++++++++++++++++++++++++++++++
 src/basic/format-table.h |  2 ++
 2 files changed, 42 insertions(+)

diff --git a/src/basic/format-table.c b/src/basic/format-table.c
index a3ff527e91..302642d748 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 07cb2351cb..4273c8c49b 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)
 
