Merge pull request #30628 from YHNdnzj/format-table-improvement

Some improvements for format-table
This commit is contained in:
Yu Watanabe 2023-12-25 20:01:32 +09:00 committed by GitHub
commit 287a5f1cff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 63 additions and 47 deletions

View file

@ -132,3 +132,13 @@ expression s;
- prioq_size(s) != 0
+ !prioq_isempty(s)
)
@@
expression s;
@@
(
- table_get_rows(s) <= 1
+ table_isempty(s)
|
- table_get_rows(s) > 1
+ !table_isempty(s)
)

View file

@ -81,7 +81,7 @@ static int dump_fdstore(sd_bus *bus, const char *arg) {
if (r < 0)
return r;
if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_get_rows(table) <= 1)
if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(table))
log_info("No file descriptors in fdstore of '%s'.", unit);
else {
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */true);

View file

@ -402,7 +402,7 @@ static int show_table(Table *table, const char *word) {
assert(table);
assert(word);
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
table_set_header(table, arg_legend);
if (!FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
@ -414,10 +414,10 @@ static int show_table(Table *table, const char *word) {
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
else
if (table_isempty(table))
printf("No %s.\n", word);
else
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
}
return 0;

View file

@ -903,12 +903,12 @@ static void service_dump_fdstore(Service *s, FILE *f, const char *prefix) {
"%s%s '%s' (type=%s; dev=" DEVNUM_FORMAT_STR "; inode=%" PRIu64 "; rdev=" DEVNUM_FORMAT_STR "; path=%s; access=%s)\n",
prefix, i == s->fd_store ? "File Descriptor Store Entry:" : " ",
i->fdname,
inode_type_to_string(st.st_mode),
strna(inode_type_to_string(st.st_mode)),
DEVNUM_FORMAT_VAL(st.st_dev),
(uint64_t) st.st_ino,
DEVNUM_FORMAT_VAL(st.st_rdev),
strna(path),
accmode_to_string(flags));
strna(accmode_to_string(flags)));
}
}

View file

@ -232,7 +232,7 @@ static int verb_list(int argc, char **argv, void *userdata) {
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No credentials passed. (i.e. $CREDENTIALS_DIRECTORY not set.)");
}
if ((arg_json_format_flags & JSON_FORMAT_OFF) && table_get_rows(t) <= 1) {
if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && table_isempty(t)) {
log_info("No credentials");
return 0;
}

View file

@ -114,7 +114,7 @@ int list_enrolled(struct crypt_device *cd) {
return table_log_add_error(r);
}
if (table_get_rows(t) <= 1) {
if (table_isempty(t)) {
log_info("No slots found.");
return 0;
}

View file

@ -189,7 +189,7 @@ static int list_homes(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
if (table_get_rows(table) > 1 || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
r = table_set_sort(table, (size_t) 0);
if (r < 0)
return table_log_sort_error(r);
@ -199,11 +199,11 @@ static int list_homes(int argc, char *argv[], void *userdata) {
return r;
}
if (arg_legend && (arg_json_format_flags & JSON_FORMAT_OFF)) {
if (table_get_rows(table) > 1)
printf("\n%zu home areas listed.\n", table_get_rows(table) - 1);
else
if (arg_legend && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
if (table_isempty(table))
printf("No home areas.\n");
else
printf("\n%zu home areas listed.\n", table_get_rows(table) - 1);
}
return 0;

View file

@ -111,7 +111,7 @@ static int print_inhibitors(sd_bus *bus) {
if (r < 0)
return bus_log_parse_error(r);
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
r = table_set_sort(table, (size_t) 1, (size_t) 0, (size_t) 5, (size_t) 6);
if (r < 0)
return table_log_sort_error(r);
@ -124,10 +124,10 @@ static int print_inhibitors(sd_bus *bus) {
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
else
if (table_isempty(table))
printf("No inhibitors.\n");
else
printf("\n%zu inhibitors listed.\n", table_get_rows(table) - 1);
}
return 0;

View file

@ -119,7 +119,7 @@ static int show_table(Table *table, const char *word) {
assert(table);
assert(word);
if (table_get_rows(table) > 1 || OUTPUT_MODE_IS_JSON(arg_output)) {
if (!table_isempty(table) || OUTPUT_MODE_IS_JSON(arg_output)) {
r = table_set_sort(table, (size_t) 0);
if (r < 0)
return table_log_sort_error(r);
@ -135,10 +135,10 @@ static int show_table(Table *table, const char *word) {
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
else
if (table_isempty(table))
printf("No %s.\n", word);
else
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
}
return 0;

View file

@ -235,7 +235,7 @@ static int show_table(Table *table, const char *word) {
assert(table);
assert(word);
if (table_get_rows(table) > 1 || OUTPUT_MODE_IS_JSON(arg_output)) {
if (!table_isempty(table) || OUTPUT_MODE_IS_JSON(arg_output)) {
r = table_set_sort(table, (size_t) 0);
if (r < 0)
return table_log_sort_error(r);
@ -251,10 +251,10 @@ static int show_table(Table *table, const char *word) {
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
else
if (table_isempty(table))
printf("No %s.\n", word);
else
printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
}
return 0;

View file

@ -2570,17 +2570,17 @@ static int verb_list_components(int argc, char *argv[], void *userdata) {
}
}
if (table_get_rows(table) > 1 || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
if (!table_isempty(table) || !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true);
if (r < 0)
return log_error_errno(r, "Failed to output table: %m");
}
if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
if (table_get_rows(table) > 1)
printf("\n%zu components listed.\n", table_get_rows(table) - 1);
else
if (table_isempty(table))
printf("No components defined.\n");
else
printf("\n%zu components listed.\n", table_get_rows(table) - 1);
}
return 0;

View file

@ -1030,7 +1030,7 @@ static int list_images(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
r = table_set_sort(table, (size_t) 0);
if (r < 0)
return table_log_sort_error(r);
@ -1043,10 +1043,10 @@ static int list_images(int argc, char *argv[], void *userdata) {
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu images listed.\n", table_get_rows(table) - 1);
else
if (table_isempty(table))
printf("No images.\n");
else
printf("\n%zu images listed.\n", table_get_rows(table) - 1);
}
return 0;

View file

@ -2003,7 +2003,7 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
if (d->mode == MODE_INVALID)
return table_ersatz_string(t);
return inode_type_to_string(d->mode);
return inode_type_to_string(d->mode) ?: table_ersatz_string(t);
case TABLE_DEVNUM:
if (devnum_is_zero(d->devnum))

View file

@ -144,6 +144,12 @@ static inline TableCell* TABLE_HEADER_CELL(size_t i) {
}
size_t table_get_rows(Table *t);
static inline bool table_isempty(Table *t) {
if (!t)
return true;
return table_get_rows(t) <= 1;
}
size_t table_get_columns(Table *t);
size_t table_get_current_column(Table *t);

View file

@ -1530,7 +1530,7 @@ int pkcs11_list_tokens(void) {
if (r < 0 && r != -EAGAIN)
return r;
if (table_get_rows(t) <= 1) {
if (table_isempty(t)) {
log_info("No suitable PKCS#11 tokens found.");
return 0;
}

View file

@ -5965,7 +5965,7 @@ int tpm2_list_devices(void) {
}
}
if (table_get_rows(t) <= 1) {
if (table_isempty(t)) {
log_info("No suitable TPM2 devices found.");
return 0;
}

View file

@ -440,7 +440,7 @@ static int display_user(int argc, char *argv[], void *userdata) {
if (uid_map_lines < 0)
return uid_map_lines;
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return table_log_print_error(r);
@ -743,7 +743,7 @@ static int display_group(int argc, char *argv[], void *userdata) {
if (gid_map_lines < 0)
return gid_map_lines;
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return table_log_print_error(r);
@ -891,17 +891,17 @@ static int display_memberships(int argc, char *argv[], void *userdata) {
}
if (table) {
if (table_get_rows(table) > 1) {
if (!table_isempty(table)) {
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return table_log_print_error(r);
}
if (arg_legend) {
if (table_get_rows(table) > 1)
printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
else
if (table_isempty(table))
printf("No memberships.\n");
else
printf("\n%zu memberships listed.\n", table_get_rows(table) - 1);
}
}
@ -956,17 +956,17 @@ static int display_services(int argc, char *argv[], void *userdata) {
return table_log_add_error(r);
}
if (table_get_rows(t) > 1) {
if (!table_isempty(t)) {
r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return table_log_print_error(r);
}
if (arg_legend && arg_output != OUTPUT_JSON) {
if (table_get_rows(t) > 1)
printf("\n%zu services listed.\n", table_get_rows(t) - 1);
else
if (table_isempty(t))
printf("No services.\n");
else
printf("\n%zu services listed.\n", table_get_rows(t) - 1);
}
return 0;