ff6046
From 75130d739d206715a09619c7b7bf62613dab1d2b Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Wed, 7 Nov 2018 17:41:32 +0100
ff6046
Subject: [PATCH] format-table: optionally allow reversing the sort order for a
ff6046
 column
ff6046
ff6046
(cherry picked from commit a2c73e2d3823e878de7a7ee193631108c5fc5be0)
ff6046
ff6046
Related: #1689832
ff6046
---
ff6046
 src/basic/format-table.c | 22 +++++++++++++++++++++-
ff6046
 src/basic/format-table.h |  1 +
ff6046
 2 files changed, 22 insertions(+), 1 deletion(-)
ff6046
ff6046
diff --git a/src/basic/format-table.c b/src/basic/format-table.c
ff6046
index 64b9eb4108..a3ff527e91 100644
ff6046
--- a/src/basic/format-table.c
ff6046
+++ b/src/basic/format-table.c
ff6046
@@ -107,6 +107,8 @@ struct Table {
ff6046
 
ff6046
         size_t *sort_map;     /* The columns to order rows by, in order of preference. */
ff6046
         size_t n_sort_map;
ff6046
+
ff6046
+        bool *reverse_map;
ff6046
 };
ff6046
 
ff6046
 Table *table_new_raw(size_t n_columns) {
ff6046
@@ -215,6 +217,7 @@ Table *table_unref(Table *t) {
ff6046
         free(t->data);
ff6046
         free(t->display_map);
ff6046
         free(t->sort_map);
ff6046
+        free(t->reverse_map);
ff6046
 
ff6046
         return mfree(t);
ff6046
 }
ff6046
@@ -836,7 +839,7 @@ static int table_data_compare(const void *x, const void *y, void *userdata) {
ff6046
 
ff6046
                 r = cell_data_compare(d, *a, dd, *b);
ff6046
                 if (r != 0)
ff6046
-                        return r;
ff6046
+                        return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
ff6046
         }
ff6046
 
ff6046
         /* Order identical lines by the order there were originally added in */
ff6046
@@ -1356,3 +1359,20 @@ size_t table_get_columns(Table *t) {
ff6046
         assert(t->n_columns > 0);
ff6046
         return t->n_columns;
ff6046
 }
ff6046
+
ff6046
+int table_set_reverse(Table *t, size_t column, bool b) {
ff6046
+        assert(t);
ff6046
+        assert(column < t->n_columns);
ff6046
+
ff6046
+        if (!t->reverse_map) {
ff6046
+                if (!b)
ff6046
+                        return 0;
ff6046
+
ff6046
+                t->reverse_map = new0(bool, t->n_columns);
ff6046
+                if (!t->reverse_map)
ff6046
+                        return -ENOMEM;
ff6046
+        }
ff6046
+
ff6046
+        t->reverse_map[column] = b;
ff6046
+        return 0;
ff6046
+}
ff6046
diff --git a/src/basic/format-table.h b/src/basic/format-table.h
ff6046
index 2db2084062..07cb2351cb 100644
ff6046
--- a/src/basic/format-table.h
ff6046
+++ b/src/basic/format-table.h
ff6046
@@ -53,6 +53,7 @@ void table_set_header(Table *table, bool b);
ff6046
 void table_set_width(Table *t, size_t width);
ff6046
 int table_set_display(Table *t, size_t first_column, ...);
ff6046
 int table_set_sort(Table *t, size_t first_column, ...);
ff6046
+int table_set_reverse(Table *t, size_t column, bool b);
ff6046
 
ff6046
 int table_print(Table *t, FILE *f);
ff6046
 int table_format(Table *t, char **ret);