format-table: introduce table_isempty and use it where appropriate

This commit is contained in:
Mike Yuan 2023-12-25 17:34:57 +08:00
parent c01a5c0527
commit 2413a0fab4
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3
15 changed files with 60 additions and 44 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

@ -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

@ -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;