Compare commits

..

3 Commits

Author SHA1 Message Date
Anita Zhang 782a7eb719
Merge pull request #15081 from mrc0mmand/systemctl-hide-glyph-column
systemctl: hide the 'glyph' column when --no-legend is requested
2020-03-11 11:12:21 -07:00
Frantisek Sumsal d171e679e7 systemctl: hide the 'glyph' column when --no-legend is requested
Fixes: #15077
2020-03-11 14:43:55 +01:00
Frantisek Sumsal 0080964cc8 format-table: allow hiding a specific column
without having to specify the whole display map
2020-03-11 14:43:52 +01:00
3 changed files with 62 additions and 5 deletions

View File

@ -1010,6 +1010,24 @@ int table_set_empty_string(Table *t, const char *empty) {
return free_and_strdup(&t->empty_string, 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, ...) { int table_set_display(Table *t, size_t first_column, ...) {
size_t allocated, column; size_t allocated, column;
va_list ap; va_list ap;
@ -1069,6 +1087,34 @@ int table_set_sort(Table *t, size_t first_column, ...) {
return 0; 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) { static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t index_b) {
assert(a); assert(a);
assert(b); assert(b);

View File

@ -101,9 +101,11 @@ void table_set_header(Table *table, bool b);
void table_set_width(Table *t, size_t width); void table_set_width(Table *t, size_t width);
void table_set_cell_height_max(Table *t, size_t height); void table_set_cell_height_max(Table *t, size_t height);
int table_set_empty_string(Table *t, const char *empty); 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_display(Table *t, size_t first_column, ...);
int table_set_sort(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_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_print(Table *t, FILE *f);
int table_format(Table *t, char **ret); int table_format(Table *t, char **ret);

View File

@ -400,6 +400,12 @@ static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
return log_oom(); return log_oom();
table_set_header(table, !arg_no_legend); table_set_header(table, !arg_no_legend);
if (arg_no_legend) {
/* Hide the 'glyph' column when --no-legend is requested */
r = table_hide_column_from_display(table, 0);
if (r < 0)
return log_error_errno(r, "Failed to hide column: %m");
}
if (arg_full) if (arg_full)
table_set_width(table, 0); table_set_width(table, 0);
@ -461,12 +467,9 @@ static int output_units_list(const UnitInfo *unit_infos, unsigned c) {
if (job_count == 0) { if (job_count == 0) {
/* There's no data in the JOB column, so let's hide it */ /* There's no data in the JOB column, so let's hide it */
/* Also, convert all number constants to size_t so va_arg() r = table_hide_column_from_display(table, 5);
* in table_set_display() fetches a correct number of bytes from
* the stack */
r = table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, (size_t) 6, (size_t) -1);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m"); return log_error_errno(r, "Failed to hide column: %m");
} }
r = table_print(table, NULL); r = table_print(table, NULL);
@ -1980,6 +1983,12 @@ static int output_machines_list(struct machine_info *machine_infos, unsigned n)
return log_oom(); return log_oom();
table_set_header(table, !arg_no_legend); table_set_header(table, !arg_no_legend);
if (arg_no_legend) {
/* Hide the 'glyph' column when --no-legend is requested */
r = table_hide_column_from_display(table, 0);
if (r < 0)
return log_error_errno(r, "Failed to hide column: %m");
}
if (arg_full) if (arg_full)
table_set_width(table, 0); table_set_width(table, 0);