teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0126-format-table-optionally-make-specific-cells-clickabl.patch

ff6046
From 4ffba0dd993bc461df18fcf59591fc71ab6e6cc8 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Tue, 6 Nov 2018 12:06:14 +0100
ff6046
Subject: [PATCH] format-table: optionally make specific cells clickable links
ff6046
ff6046
(cherry picked from commit 165ca5663e9859083c70d793a6b4aa4f3b2af24c)
ff6046
ff6046
Related: #1689832
ff6046
---
ff6046
 src/basic/format-table.c | 79 +++++++++++++++++++++++++++++++++++-----
ff6046
 src/basic/format-table.h |  1 +
ff6046
 2 files changed, 71 insertions(+), 9 deletions(-)
ff6046
ff6046
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
ff6046
index 3429d9a071..ac5d66eda2 100644
ff6046
--- a/src/basic/format-table.c
ff6046
+++ b/src/basic/format-table.c
ff6046
@@ -9,6 +9,7 @@
ff6046
 #include "gunicode.h"
ff6046
 #include "pager.h"
ff6046
 #include "parse-util.h"
ff6046
+#include "pretty-print.h"
ff6046
 #include "string-util.h"
ff6046
 #include "terminal-util.h"
ff6046
 #include "time-util.h"
ff6046
@@ -58,6 +59,7 @@ typedef struct TableData {
ff6046
         unsigned align_percent;     /* 0 … 100, where to pad with spaces when expanding is needed. 0: left-aligned, 100: right-aligned */
ff6046
 
ff6046
         const char *color;          /* ANSI color string to use for this cell. When written to terminal should not move cursor. Will automatically be reset after the cell */
ff6046
+        char *url;                  /* A URL to use for a clickable hyperlink */
ff6046
         char *formatted;            /* A cached textual representation of the cell data, before ellipsation/alignment */
ff6046
 
ff6046
         union {
ff6046
@@ -182,6 +184,8 @@ static TableData *table_data_unref(TableData *d) {
ff6046
                 return NULL;
ff6046
 
ff6046
         free(d->formatted);
ff6046
+        free(d->url);
ff6046
+
ff6046
         return mfree(d);
ff6046
 }
ff6046
 
ff6046
@@ -392,6 +396,7 @@ int table_dup_cell(Table *t, TableCell *cell) {
ff6046
 }
ff6046
 
ff6046
 static int table_dedup_cell(Table *t, TableCell *cell) {
ff6046
+        _cleanup_free_ char *curl = NULL;
ff6046
         TableData *nd, *od;
ff6046
         size_t i;
ff6046
 
ff6046
@@ -410,11 +415,25 @@ static int table_dedup_cell(Table *t, TableCell *cell) {
ff6046
 
ff6046
         assert(od->n_ref > 1);
ff6046
 
ff6046
-        nd = table_data_new(od->type, od->data, od->minimum_width, od->maximum_width, od->weight, od->align_percent, od->ellipsize_percent);
ff6046
+        if (od->url) {
ff6046
+                curl = strdup(od->url);
ff6046
+                if (!curl)
ff6046
+                        return -ENOMEM;
ff6046
+        }
ff6046
+
ff6046
+        nd = table_data_new(
ff6046
+                        od->type,
ff6046
+                        od->data,
ff6046
+                        od->minimum_width,
ff6046
+                        od->maximum_width,
ff6046
+                        od->weight,
ff6046
+                        od->align_percent,
ff6046
+                        od->ellipsize_percent);
ff6046
         if (!nd)
ff6046
                 return -ENOMEM;
ff6046
 
ff6046
         nd->color = od->color;
ff6046
+        nd->url = TAKE_PTR(curl);
ff6046
 
ff6046
         table_data_unref(od);
ff6046
         t->data[i] = nd;
ff6046
@@ -542,6 +561,26 @@ int table_set_color(Table *t, TableCell *cell, const char *color) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
+int table_set_url(Table *t, TableCell *cell, const char *url) {
ff6046
+        _cleanup_free_ char *copy = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        assert(t);
ff6046
+        assert(cell);
ff6046
+
ff6046
+        if (url) {
ff6046
+                copy = strdup(url);
ff6046
+                if (!copy)
ff6046
+                        return -ENOMEM;
ff6046
+        }
ff6046
+
ff6046
+        r = table_dedup_cell(t, cell);
ff6046
+        if (r < 0)
ff6046
+                return r;
ff6046
+
ff6046
+        return free_and_replace(table_get_data(t, cell)->url, copy);
ff6046
+}
ff6046
+
ff6046
 int table_add_many_internal(Table *t, TableDataType first_type, ...) {
ff6046
         TableDataType type;
ff6046
         va_list ap;
ff6046
@@ -884,11 +923,13 @@ static int table_data_requested_width(TableData *d, size_t *ret) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-static char *align_string_mem(const char *str, size_t new_length, unsigned percent) {
ff6046
-        size_t w = 0, space, lspace, old_length;
ff6046
+static char *align_string_mem(const char *str, const char *url, size_t new_length, unsigned percent) {
ff6046
+        size_t w = 0, space, lspace, old_length, clickable_length;
ff6046
+        _cleanup_free_ char *clickable = NULL;
ff6046
         const char *p;
ff6046
         char *ret;
ff6046
         size_t i;
ff6046
+        int r;
ff6046
 
ff6046
         /* As with ellipsize_mem(), 'old_length' is a byte size while 'new_length' is a width in character cells */
ff6046
 
ff6046
@@ -897,6 +938,15 @@ static char *align_string_mem(const char *str, size_t new_length, unsigned perce
ff6046
 
ff6046
         old_length = strlen(str);
ff6046
 
ff6046
+        if (url) {
ff6046
+                r = terminal_urlify(url, str, &clickable);
ff6046
+                if (r < 0)
ff6046
+                        return NULL;
ff6046
+
ff6046
+                clickable_length = strlen(clickable);
ff6046
+        } else
ff6046
+                clickable_length = old_length;
ff6046
+
ff6046
         /* Determine current width on screen */
ff6046
         p = str;
ff6046
         while (p < str + old_length) {
ff6046
@@ -913,23 +963,23 @@ static char *align_string_mem(const char *str, size_t new_length, unsigned perce
ff6046
 
ff6046
         /* Already wider than the target, if so, don't do anything */
ff6046
         if (w >= new_length)
ff6046
-                return strndup(str, old_length);
ff6046
+                return clickable ? TAKE_PTR(clickable) : strdup(str);
ff6046
 
ff6046
         /* How much spaces shall we add? An how much on the left side? */
ff6046
         space = new_length - w;
ff6046
         lspace = space * percent / 100U;
ff6046
 
ff6046
-        ret = new(char, space + old_length + 1);
ff6046
+        ret = new(char, space + clickable_length + 1);
ff6046
         if (!ret)
ff6046
                 return NULL;
ff6046
 
ff6046
         for (i = 0; i < lspace; i++)
ff6046
                 ret[i] = ' ';
ff6046
-        memcpy(ret + lspace, str, old_length);
ff6046
-        for (i = lspace + old_length; i < space + old_length; i++)
ff6046
+        memcpy(ret + lspace, clickable ?: str, clickable_length);
ff6046
+        for (i = lspace + clickable_length; i < space + clickable_length; i++)
ff6046
                 ret[i] = ' ';
ff6046
 
ff6046
-        ret[space + old_length] = 0;
ff6046
+        ret[space + clickable_length] = 0;
ff6046
         return ret;
ff6046
 }
ff6046
 
ff6046
@@ -1184,13 +1234,24 @@ int table_print(Table *t, FILE *f) {
ff6046
                         } else if (l < width[j]) {
ff6046
                                 /* Field is shorter than allocated space. Let's align with spaces */
ff6046
 
ff6046
-                                buffer = align_string_mem(field, width[j], d->align_percent);
ff6046
+                                buffer = align_string_mem(field, d->url, width[j], d->align_percent);
ff6046
                                 if (!buffer)
ff6046
                                         return -ENOMEM;
ff6046
 
ff6046
                                 field = buffer;
ff6046
                         }
ff6046
 
ff6046
+                        if (l >= width[j] && d->url) {
ff6046
+                                _cleanup_free_ char *clickable = NULL;
ff6046
+
ff6046
+                                r = terminal_urlify(d->url, field, &clickable);
ff6046
+                                if (r < 0)
ff6046
+                                        return r;
ff6046
+
ff6046
+                                free_and_replace(buffer, clickable);
ff6046
+                                field = buffer;
ff6046
+                        }
ff6046
+
ff6046
                         if (j > 0)
ff6046
                                 fputc(' ', f); /* column separator */
ff6046
 
ff6046
diff --git a/src/basic/format-table.h b/src/basic/format-table.h
ff6046
index 6dc2d16052..9978a8baf2 100644
ff6046
--- a/src/basic/format-table.h
ff6046
+++ b/src/basic/format-table.h
ff6046
@@ -42,6 +42,7 @@ int table_set_weight(Table *t, TableCell *cell, unsigned weight);
ff6046
 int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
ff6046
 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
ff6046
 int table_set_color(Table *t, TableCell *cell, const char *color);
ff6046
+int table_set_url(Table *t, TableCell *cell, const char *color);
ff6046
 
ff6046
 int table_add_many_internal(Table *t, TableDataType first_type, ...);
ff6046
 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)