if (r < 0)
return log_error_errno(r, "Failed to set alignment: %m");
- r = table_set_empty_string(table, "-");
- if (r < 0)
- return log_error_errno(r, "Failed to set empty string: %m");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
r = table_set_sort(table, (size_t) COLUMN_NAME);
if (r < 0)
(void) table_set_align_percent(t, TABLE_HEADER_CELL(3), 100);
(void) table_set_align_percent(t, TABLE_HEADER_CELL(7), 100);
- (void) table_set_empty_string(t, "-");
+ table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
} else
pager_open(arg_pager_flags);
if (!t)
return log_oom();
- (void) table_set_empty_string(t, "-");
+ table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
table_set_header(table, false);
- r = table_set_empty_string(table, "(unset)");
- if (r < 0)
- return log_oom();
+ table_set_ersatz_string(table, TABLE_ERSATZ_UNSET);
r = table_add_many(table,
TABLE_STRING, "Static hostname:",
table_set_header(table, false);
- r = table_set_empty_string(table, "(unset)");
- if (r < 0)
- return log_oom();
+ table_set_ersatz_string(table, TABLE_ERSATZ_UNSET);
if (!strv_isempty(kernel_locale)) {
log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.");
if (!table)
return log_oom();
- table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
if (!arg_full && arg_max_addresses != ALL_ADDRESSES)
table_set_cell_height_max(table, arg_max_addresses);
table_set_width(table, 0);
table_set_header(table, arg_legend);
- if (table_set_empty_string(table, "-") < 0)
- return log_oom();
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
assert_se(cell = table_get_cell(table, 0, 0));
(void) table_set_minimum_width(table, cell, 3);
size_t n_cells;
bool header; /* Whether to show the header row? */
+ TableErsatz ersatz; /* What to show when we have an empty cell or an invalid value that cannot be rendered. */
+
size_t width; /* If == 0 format this as wide as necessary. If SIZE_MAX format this to console
* width or less wide, but not wider. Otherwise the width to format this table in. */
size_t cell_height_max; /* Maximum number of lines per cell. (If there are more, ellipsis is shown. If SIZE_MAX then no limit is set, the default. == 0 is not allowed.) */
size_t n_json_fields;
bool *reverse_map;
-
- char *empty_string;
};
Table *table_new_raw(size_t n_columns) {
.header = true,
.width = SIZE_MAX,
.cell_height_max = SIZE_MAX,
+ .ersatz = TABLE_ERSATZ_EMPTY,
};
return TAKE_PTR(t);
free(t->display_map);
free(t->sort_map);
free(t->reverse_map);
- free(t->empty_string);
for (size_t i = 0; i < t->n_json_fields; i++)
free(t->json_fields[i]);
t->cell_height_max = height;
}
-int table_set_empty_string(Table *t, const char *empty) {
+void table_set_ersatz_string(Table *t, TableErsatz ersatz) {
assert(t);
+ assert(ersatz >= 0 && ersatz < _TABLE_ERSATZ_MAX);
+
+ t->ersatz = ersatz;
+}
- return free_and_strdup(&t->empty_string, empty);
+static const char* table_ersatz_string(const Table *t) {
+ switch (t->ersatz) {
+ case TABLE_ERSATZ_EMPTY:
+ return "";
+ case TABLE_ERSATZ_DASH:
+ return "-";
+ case TABLE_ERSATZ_UNSET:
+ return "(unset)";
+ case TABLE_ERSATZ_NA:
+ return "n/a";
+ default:
+ assert_not_reached();
+ }
}
static int table_set_display_all(Table *t) {
switch (d->type) {
case TABLE_EMPTY:
- return strempty(t->empty_string);
+ return table_ersatz_string(t);
case TABLE_STRING:
case TABLE_PATH:
case TABLE_STRV:
if (strv_isempty(d->strv))
- return strempty(t->empty_string);
+ return table_ersatz_string(t);
d->formatted = strv_join(d->strv, "\n");
if (!d->formatted)
case TABLE_STRV_WRAPPED: {
if (strv_isempty(d->strv))
- return strempty(t->empty_string);
+ return table_ersatz_string(t);
char *buf = format_strv_width(d->strv, column_width);
if (!buf)
_TABLE_DATA_TYPE_INVALID = -EINVAL,
} TableDataType;
+typedef enum TableErsatz {
+ TABLE_ERSATZ_EMPTY,
+ TABLE_ERSATZ_DASH,
+ TABLE_ERSATZ_UNSET,
+ TABLE_ERSATZ_NA,
+ _TABLE_ERSATZ_MAX,
+} TableErsatz;
+
typedef struct Table Table;
typedef struct TableCell TableCell;
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);
+void table_set_ersatz_string(Table *t, TableErsatz ersatz);
int table_set_display_internal(Table *t, size_t first_column, ...);
#define table_set_display(...) table_set_display_internal(__VA_ARGS__, SIZE_MAX)
int table_set_sort_internal(Table *t, size_t first_column, ...);
if (!t)
return log_oom();
- (void) table_set_empty_string(t, "-");
+ table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
STRV_FOREACH(p, arg_hierarchies) {
_cleanup_free_ char *resolved = NULL, *f = NULL, *buf = NULL;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (const struct job_info *j = jobs; j < jobs + n; j++) {
if (streq(j->state, "running"))
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (struct machine_info *m = machine_infos; m < machine_infos + n; m++) {
_cleanup_free_ char *mname = NULL;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (const UnitFileList *u = units; u < units + c; u++) {
const char *on_underline = NULL, *on_unit_color = NULL, *id;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (const UnitInfo *u = unit_infos; unit_infos && (size_t) (u - unit_infos) < c; u++) {
_cleanup_free_ char *j = NULL;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (struct socket_info *s = socket_infos; s < socket_infos + cs; s++) {
_cleanup_free_ char *j = NULL;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (struct timer_info *t = timer_infos; t < timer_infos + n; t++) {
_cleanup_free_ char *j = NULL, *activates = NULL;
if (arg_full)
table_set_width(table, 0);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
for (struct automount_info *info = infos; info < infos + n_infos; info++) {
_cleanup_free_ char *j = NULL;
(void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
(void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
- (void) table_set_empty_string(t, "-");
+ table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
/* Determine if the target will make use of partition/fs attributes for any of the transfers */
for (size_t n = 0; n < c->n_transfers; n++) {
(void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
(void) table_set_align_percent(table, table_get_cell(table, 0, 4), 100);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
(void) table_set_sort(table, (size_t) 3, (size_t) 8);
(void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4, (size_t) 5, (size_t) 6, (size_t) 7);
}
return log_oom();
(void) table_set_align_percent(table, table_get_cell(table, 0, 3), 100);
- (void) table_set_empty_string(table, "-");
+ table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
(void) table_set_sort(table, (size_t) 3, (size_t) 5);
(void) table_set_display(table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4);
}