1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 typedef enum TableDataType
{
20 TABLE_TIMESTAMP_RELATIVE
,
37 TABLE_IN_ADDR
, /* Takes a union in_addr_union (or a struct in_addr) */
38 TABLE_IN6_ADDR
, /* Takes a union in_addr_union (or a struct in6_addr) */
43 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
44 * a cell just added. */
45 TABLE_SET_MINIMUM_WIDTH
,
46 TABLE_SET_MAXIMUM_WIDTH
,
48 TABLE_SET_ALIGN_PERCENT
,
49 TABLE_SET_ELLIPSIZE_PERCENT
,
52 TABLE_SET_BOTH_COLORS
,
56 _TABLE_DATA_TYPE_INVALID
= -1,
59 /* PIDs are just 32bit signed integers on Linux */
60 #define TABLE_PID TABLE_INT32
61 assert_cc(sizeof(pid_t
) == sizeof(int32_t));
63 /* UIDs/GIDs are just 32bit unsigned integers on Linux */
64 #define TABLE_UID TABLE_UINT32
65 #define TABLE_GID TABLE_UINT32
66 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
67 assert_cc(sizeof(gid_t
) == sizeof(uint32_t));
69 typedef struct Table Table
;
70 typedef struct TableCell TableCell
;
72 Table
*table_new_internal(const char *first_header
, ...) _sentinel_
;
73 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
74 Table
*table_new_raw(size_t n_columns
);
75 Table
*table_unref(Table
*t
);
77 DEFINE_TRIVIAL_CLEANUP_FUNC(Table
*, table_unref
);
79 int table_add_cell_full(Table
*t
, TableCell
**ret_cell
, TableDataType type
, const void *data
, size_t minimum_width
, size_t maximum_width
, unsigned weight
, unsigned align_percent
, unsigned ellipsize_percent
);
80 static inline int table_add_cell(Table
*t
, TableCell
**ret_cell
, TableDataType type
, const void *data
) {
81 return table_add_cell_full(t
, ret_cell
, type
, data
, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
83 int table_add_cell_stringf(Table
*t
, TableCell
**ret_cell
, const char *format
, ...) _printf_(3, 4);
85 int table_fill_empty(Table
*t
, size_t until_column
);
87 int table_dup_cell(Table
*t
, TableCell
*cell
);
89 int table_set_minimum_width(Table
*t
, TableCell
*cell
, size_t minimum_width
);
90 int table_set_maximum_width(Table
*t
, TableCell
*cell
, size_t maximum_width
);
91 int table_set_weight(Table
*t
, TableCell
*cell
, unsigned weight
);
92 int table_set_align_percent(Table
*t
, TableCell
*cell
, unsigned percent
);
93 int table_set_ellipsize_percent(Table
*t
, TableCell
*cell
, unsigned percent
);
94 int table_set_color(Table
*t
, TableCell
*cell
, const char *color
);
95 int table_set_rgap_color(Table
*t
, TableCell
*cell
, const char *color
);
96 int table_set_url(Table
*t
, TableCell
*cell
, const char *url
);
97 int table_set_uppercase(Table
*t
, TableCell
*cell
, bool b
);
99 int table_update(Table
*t
, TableCell
*cell
, TableDataType type
, const void *data
);
101 int table_add_many_internal(Table
*t
, TableDataType first_type
, ...);
102 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
104 void table_set_header(Table
*table
, bool b
);
105 void table_set_width(Table
*t
, size_t width
);
106 void table_set_cell_height_max(Table
*t
, size_t height
);
107 int table_set_empty_string(Table
*t
, const char *empty
);
108 int table_set_display_all(Table
*t
);
109 int table_set_display(Table
*t
, size_t first_column
, ...);
110 int table_set_sort(Table
*t
, size_t first_column
, ...);
111 int table_set_reverse(Table
*t
, size_t column
, bool b
);
112 int table_hide_column_from_display(Table
*t
, size_t column
);
114 int table_print(Table
*t
, FILE *f
);
115 int table_format(Table
*t
, char **ret
);
117 static inline TableCell
* TABLE_HEADER_CELL(size_t i
) {
118 return SIZE_TO_PTR(i
+ 1);
121 size_t table_get_rows(Table
*t
);
122 size_t table_get_columns(Table
*t
);
124 TableCell
*table_get_cell(Table
*t
, size_t row
, size_t column
);
126 const void *table_get(Table
*t
, TableCell
*cell
);
127 const void *table_get_at(Table
*t
, size_t row
, size_t column
);
129 int table_to_json(Table
*t
, JsonVariant
**ret
);
130 int table_print_json(Table
*t
, FILE *f
, JsonFormatFlags json_flags
);
132 #define table_log_add_error(r) \
133 log_error_errno(r, "Failed to add cell(s) to table: %m")
135 #define table_log_print_error(r) \
136 log_error_errno(r, "Failed to print table: %m")
138 #define table_log_sort_error(r) \
139 log_error_errno(r, "Failed to sort table: %m")