Debian Codegraph @

src/shared/format-table.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-json.h"
5
6 #include "shared-forward.h"
7 #include "log.h"
8 #include "pager.h"
9
10 typedef enum TableDataType {
11 TABLE_EMPTY,
12 TABLE_STRING,
13 TABLE_STRING_WITH_ANSI, /* like the above, but contains ANSI sequences/TABs. They will be stripped when outputting to JSON */
14 TABLE_HEADER, /* in regular mode: the cells in the first row, that carry the column names */
15 TABLE_FIELD, /* in vertical mode: the cells in the first column, that carry the field names */
16 TABLE_STRV,
17 TABLE_STRV_WRAPPED,
18 TABLE_PATH,
19 TABLE_PATH_BASENAME, /* like TABLE_PATH, but display only last path element (i.e. the "basename") in regular output */
20 TABLE_VERSION, /* just like TABLE_STRING, but uses version comparison when sorting */
21 TABLE_BOOLEAN,
22 TABLE_BOOLEAN_CHECKMARK,
23 TABLE_TRISTATE,
24 TABLE_TIMESTAMP,
25 TABLE_TIMESTAMP_UTC,
26 TABLE_TIMESTAMP_RELATIVE,
27 TABLE_TIMESTAMP_RELATIVE_MONOTONIC,
28 TABLE_TIMESTAMP_LEFT,
29 TABLE_TIMESTAMP_DATE,
30 TABLE_TIMESPAN,
31 TABLE_TIMESPAN_MSEC,
32 TABLE_TIMESPAN_DAY,
33 TABLE_SIZE,
34 TABLE_BPS,
35 TABLE_INT,
36 TABLE_INT8,
37 TABLE_INT16,
38 TABLE_INT32,
39 TABLE_INT64,
40 TABLE_UINT,
41 TABLE_UINT8,
42 TABLE_UINT16,
43 TABLE_UINT32,
44 TABLE_UINT32_HEX,
45 TABLE_UINT32_HEX_0x,
46 TABLE_UINT64,
47 TABLE_UINT64_HEX,
48 TABLE_UINT64_HEX_0x,
49 TABLE_PERCENT,
50 TABLE_IFINDEX,
51 TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
52 TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
53 TABLE_ID128,
54 TABLE_UUID,
55 TABLE_UID,
56 TABLE_GID,
57 TABLE_PID,
58 TABLE_SIGNAL,
59 TABLE_MODE, /* as in UNIX file mode (mode_t), in typical octal output */
60 TABLE_MODE_INODE_TYPE, /* also mode_t, but displays only the inode type as string */
61 TABLE_DEVNUM, /* a dev_t, displayed in the usual major:minor way */
62 TABLE_JSON, /* an sd_json_variant, for output formatted into text */
63 _TABLE_DATA_TYPE_MAX,
64
65 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
66 * a cell just added. */
67 TABLE_SET_MINIMUM_WIDTH,
68 TABLE_SET_MAXIMUM_WIDTH,
69 TABLE_SET_WEIGHT,
70 TABLE_SET_ALIGN_PERCENT,
71 TABLE_SET_ELLIPSIZE_PERCENT,
72 TABLE_SET_COLOR,
73 TABLE_SET_RGAP_COLOR,
74 TABLE_SET_BOTH_COLORS,
75 TABLE_SET_UNDERLINE,
76 TABLE_SET_RGAP_UNDERLINE,
77 TABLE_SET_BOTH_UNDERLINES,
78 TABLE_SET_URL,
79 TABLE_SET_UPPERCASE,
80 TABLE_SET_JSON_FIELD_NAME,
81
82 _TABLE_DATA_TYPE_INVALID = -EINVAL,
83 } TableDataType;
84
85 typedef enum TableErsatz {
86 TABLE_ERSATZ_EMPTY,
87 TABLE_ERSATZ_DASH,
88 TABLE_ERSATZ_UNSET,
89 TABLE_ERSATZ_NA,
90 _TABLE_ERSATZ_MAX,
91 _TABLE_ERSATZ_INVALID = -EINVAL,
92 } TableErsatz;
93
94 typedef struct Table Table;
95 typedef struct TableCell TableCell;
96
97 Table* table_new_internal(const char *first_header, ...) _sentinel_;
98 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
99 Table* table_new_raw(size_t n_columns);
100 Table* table_new_vertical(void);
101 Table* table_unref(Table *t);
102
103 DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
104 static inline DEFINE_POINTER_ARRAY_CLEAR_FUNC(Table*, table_unref);
105
106 int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType dt, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent);
107 static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType dt, const void *data) {
108 return table_add_cell_full(t, ret_cell, dt, data, SIZE_MAX, SIZE_MAX, UINT_MAX, UINT_MAX, UINT_MAX);
109 }
110 int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType dt, const char *format, ...) _printf_(4, 5);
111 #define table_add_cell_stringf(t, ret_cell, format, ...) table_add_cell_stringf_full(t, ret_cell, TABLE_STRING, format, __VA_ARGS__)
112
113 int table_fill_empty(Table *t, size_t until_column);
114
115 int table_dup_cell(Table *t, TableCell *cell);
116
117 int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
118 int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
119 int table_set_weight(Table *t, TableCell *cell, unsigned weight);
120 int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
121 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
122 int table_set_color(Table *t, TableCell *cell, const char *color);
123 int table_set_rgap_color(Table *t, TableCell *cell, const char *color);
124 int table_set_underline(Table *t, TableCell *cell, bool b);
125 int table_set_rgap_underline(Table *t, TableCell *cell, bool b);
126 int table_set_url(Table *t, TableCell *cell, const char *url);
127 int table_set_uppercase(Table *t, TableCell *cell, bool b);
128
129 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
130
131 int table_add_many_internal(Table *t, TableDataType first_type, ...);
132 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
133
134 void table_set_header(Table *table, bool b);
135 void table_set_width(Table *t, size_t width);
136 void table_set_cell_height_max(Table *t, size_t height);
137 void table_set_ersatz_string(Table *t, TableErsatz ersatz);
138 int table_set_display_internal(Table *t, size_t first_column, ...);
139 #define table_set_display(...) table_set_display_internal(__VA_ARGS__, SIZE_MAX)
140 int table_set_sort_internal(Table *t, size_t first_column, ...);
141 #define table_set_sort(...) table_set_sort_internal(__VA_ARGS__, SIZE_MAX)
142 int table_set_reverse(Table *t, size_t column, bool b);
143 int table_hide_column_from_display_internal(Table *t, ...);
144 #define table_hide_column_from_display(t, ...) table_hide_column_from_display_internal(t, __VA_ARGS__, SIZE_MAX)
145
146 int table_data_requested_width(Table *table, size_t column, size_t *ret);
147
148 int table_set_column_width(Table *t, size_t column, size_t width);
149 int _table_sync_column_widths(size_t column, Table *a, ...);
150 #define table_sync_column_widths(column, a, ...) _table_sync_column_widths(column, a, __VA_ARGS__, NULL)
151
152 int table_print_full(Table *t, FILE *f, bool flush);
153 static inline int table_print(Table *t) {
154 return table_print_full(t, /* f= */ NULL, /* flush= */ false);
155 }
156 int table_print_or_warn(Table *t);
157
158 int table_format(Table *t, char **ret);
159
160 static inline TableCell* TABLE_HEADER_CELL(size_t i) {
161 return SIZE_TO_PTR(i + 1);
162 }
163
164 size_t table_get_rows(Table *t);
165 static inline bool table_isempty(Table *t) {
166 if (!t)
167 return true;
168
169 return table_get_rows(t) <= 1;
170 }
171 size_t table_get_columns(Table *t);
172
173 size_t table_get_current_column(Table *t);
174
175 TableCell* table_get_cell(Table *t, size_t row, size_t column);
176
177 const void* table_get(Table *t, TableCell *cell);
178 const void* table_get_at(Table *t, size_t row, size_t column);
179
180 int table_to_json(Table *t, sd_json_variant **ret);
181 int table_print_json(Table *t, FILE *f, sd_json_format_flags_t json_flags);
182
183 int table_print_with_pager(Table *t, sd_json_format_flags_t json_format_flags, PagerFlags pager_flags, bool show_header);
184
185 char* table_mangle_to_json_field_name(const char *str);
186 int table_set_json_field_name(Table *t, size_t idx, const char *name);
187
188 #define table_log_add_error(r) \
189 log_error_errno(r, "Failed to add cells to table: %m")
190
191 #define table_log_print_error(r) \
192 log_error_errno(r, "Failed to print table: %m")
193
194 #define table_log_sort_error(r) \
195 log_error_errno(r, "Failed to sort table: %m")