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