summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrantisek Sumsal <frantisek@sumsal.cz>2020-03-11 13:27:43 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-03-18 20:29:15 +0100
commit1c7de81f89ef01c4a23a7fb8c4c06aebbcd354d6 (patch)
tree5d70d8a25973d04e3a9c97ad17e0f7ce0b8ee8e5
parentb7f2308bda4942d1b8e10250db6836fe4fc0d8b8 (diff)
downloadsystemd-1c7de81f89ef01c4a23a7fb8c4c06aebbcd354d6.tar.gz
format-table: allow hiding a specific column
without having to specify the whole display map (cherry picked from commit 0080964cc87e8e2c9904816bf67198b9119ed473)
-rw-r--r--src/shared/format-table.c46
-rw-r--r--src/shared/format-table.h2
2 files changed, 48 insertions, 0 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index 4250130464..a4b412ecb9 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -1010,6 +1010,24 @@ int table_set_empty_string(Table *t, const char *empty) {
return free_and_strdup(&t->empty_string, empty);
}
+int table_set_display_all(Table *t) {
+ size_t allocated;
+
+ assert(t);
+
+ allocated = t->n_display_map;
+
+ if (!GREEDY_REALLOC(t->display_map, allocated, MAX(t->n_columns, allocated)))
+ return -ENOMEM;
+
+ for (size_t i = 0; i < t->n_columns; i++)
+ t->display_map[i] = i;
+
+ t->n_display_map = t->n_columns;
+
+ return 0;
+}
+
int table_set_display(Table *t, size_t first_column, ...) {
size_t allocated, column;
va_list ap;
@@ -1069,6 +1087,34 @@ int table_set_sort(Table *t, size_t first_column, ...) {
return 0;
}
+int table_hide_column_from_display(Table *t, size_t column) {
+ size_t allocated, cur = 0;
+ int r;
+
+ assert(t);
+ assert(column < t->n_columns);
+
+ /* If the display map is empty, initialize it with all available columns */
+ if (!t->display_map) {
+ r = table_set_display_all(t);
+ if (r < 0)
+ return r;
+ }
+
+ allocated = t->n_display_map;
+
+ for (size_t i = 0; i < allocated; i++) {
+ if (t->display_map[i] == column)
+ continue;
+
+ t->display_map[cur++] = t->display_map[i];
+ }
+
+ t->n_display_map = cur;
+
+ return 0;
+}
+
static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t index_b) {
assert(a);
assert(b);
diff --git a/src/shared/format-table.h b/src/shared/format-table.h
index 870a29d385..62f1ed740d 100644
--- a/src/shared/format-table.h
+++ b/src/shared/format-table.h
@@ -101,9 +101,11 @@ void table_set_header(Table *table, bool b);
void table_set_width(Table *t, size_t width);
void table_set_cell_height_max(Table *t, size_t height);
int table_set_empty_string(Table *t, const char *empty);
+int table_set_display_all(Table *t);
int table_set_display(Table *t, size_t first_column, ...);
int table_set_sort(Table *t, size_t first_column, ...);
int table_set_reverse(Table *t, size_t column, bool b);
+int table_hide_column_from_display(Table *t, size_t column);
int table_print(Table *t, FILE *f);
int table_format(Table *t, char **ret);