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