]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/format-table.h
1a486bf5bef429a05ca16c16c9adf7ab4269eddd
[thirdparty/systemd.git] / src / shared / format-table.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7
8 #include "json.h"
9 #include "macro.h"
10 #include "pager.h"
11
12 typedef enum TableDataType {
13 TABLE_EMPTY,
14 TABLE_STRING,
15 TABLE_STRV,
16 TABLE_STRV_WRAPPED,
17 TABLE_PATH,
18 TABLE_BOOLEAN,
19 TABLE_TIMESTAMP,
20 TABLE_TIMESTAMP_UTC,
21 TABLE_TIMESTAMP_RELATIVE,
22 TABLE_TIMESPAN,
23 TABLE_TIMESPAN_MSEC,
24 TABLE_SIZE,
25 TABLE_BPS,
26 TABLE_INT,
27 TABLE_INT8,
28 TABLE_INT16,
29 TABLE_INT32,
30 TABLE_INT64,
31 TABLE_UINT,
32 TABLE_UINT8,
33 TABLE_UINT16,
34 TABLE_UINT32,
35 TABLE_UINT64,
36 TABLE_PERCENT,
37 TABLE_IFINDEX,
38 TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
39 TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
40 TABLE_ID128,
41 TABLE_UUID,
42 TABLE_UID,
43 TABLE_GID,
44 TABLE_PID,
45 TABLE_SIGNAL,
46 _TABLE_DATA_TYPE_MAX,
47
48 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
49 * a cell just added. */
50 TABLE_SET_MINIMUM_WIDTH,
51 TABLE_SET_MAXIMUM_WIDTH,
52 TABLE_SET_WEIGHT,
53 TABLE_SET_ALIGN_PERCENT,
54 TABLE_SET_ELLIPSIZE_PERCENT,
55 TABLE_SET_COLOR,
56 TABLE_SET_RGAP_COLOR,
57 TABLE_SET_BOTH_COLORS,
58 TABLE_SET_URL,
59 TABLE_SET_UPPERCASE,
60
61 _TABLE_DATA_TYPE_INVALID = -EINVAL,
62 } TableDataType;
63
64 typedef struct Table Table;
65 typedef struct TableCell TableCell;
66
67 Table *table_new_internal(const char *first_header, ...) _sentinel_;
68 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
69 Table *table_new_raw(size_t n_columns);
70 Table *table_unref(Table *t);
71
72 DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
73
74 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);
75 static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
76 return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
77 }
78 int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) _printf_(3, 4);
79
80 int table_fill_empty(Table *t, size_t until_column);
81
82 int table_dup_cell(Table *t, TableCell *cell);
83
84 int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
85 int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
86 int table_set_weight(Table *t, TableCell *cell, unsigned weight);
87 int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
88 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
89 int table_set_color(Table *t, TableCell *cell, const char *color);
90 int table_set_rgap_color(Table *t, TableCell *cell, const char *color);
91 int table_set_url(Table *t, TableCell *cell, const char *url);
92 int table_set_uppercase(Table *t, TableCell *cell, bool b);
93
94 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
95
96 int table_add_many_internal(Table *t, TableDataType first_type, ...);
97 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
98
99 void table_set_header(Table *table, bool b);
100 void table_set_width(Table *t, size_t width);
101 void table_set_cell_height_max(Table *t, size_t height);
102 int table_set_empty_string(Table *t, const char *empty);
103 int table_set_display_all(Table *t);
104 int table_set_display_internal(Table *t, size_t first_column, ...);
105 #define table_set_display(...) table_set_display_internal(__VA_ARGS__, (size_t) SIZE_MAX)
106 int table_set_sort_internal(Table *t, size_t first_column, ...);
107 #define table_set_sort(...) table_set_sort_internal(__VA_ARGS__, (size_t) SIZE_MAX)
108 int table_set_reverse(Table *t, size_t column, bool b);
109 int table_hide_column_from_display(Table *t, size_t column);
110
111 int table_print(Table *t, FILE *f);
112 int table_format(Table *t, char **ret);
113
114 static inline TableCell* TABLE_HEADER_CELL(size_t i) {
115 return SIZE_TO_PTR(i + 1);
116 }
117
118 size_t table_get_rows(Table *t);
119 size_t table_get_columns(Table *t);
120
121 TableCell *table_get_cell(Table *t, size_t row, size_t column);
122
123 const void *table_get(Table *t, TableCell *cell);
124 const void *table_get_at(Table *t, size_t row, size_t column);
125
126 int table_to_json(Table *t, JsonVariant **ret);
127 int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags);
128
129 int table_print_with_pager(Table *t, JsonFormatFlags json_format_flags, PagerFlags pager_flags, bool show_header);
130
131 #define table_log_add_error(r) \
132 log_error_errno(r, "Failed to add cell(s) to table: %m")
133
134 #define table_log_print_error(r) \
135 log_error_errno(r, "Failed to print table: %m")
136
137 #define table_log_sort_error(r) \
138 log_error_errno(r, "Failed to sort table: %m")