]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/format-table.h
tree-wide: use UINT64_MAX or friends
[thirdparty/systemd.git] / src / shared / format-table.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1960e736
LP
2#pragma once
3
4#include <stdbool.h>
5#include <stdio.h>
6#include <sys/types.h>
7
31ac2357 8#include "json.h"
1960e736 9#include "macro.h"
e676b4fc 10#include "pager.h"
1960e736
LP
11
12typedef enum TableDataType {
13 TABLE_EMPTY,
14 TABLE_STRING,
4618660d 15 TABLE_STRV,
b0e3d799 16 TABLE_STRV_WRAPPED,
f93d876c 17 TABLE_PATH,
1960e736
LP
18 TABLE_BOOLEAN,
19 TABLE_TIMESTAMP,
c5bbb2b5
YW
20 TABLE_TIMESTAMP_UTC,
21 TABLE_TIMESTAMP_RELATIVE,
1960e736 22 TABLE_TIMESPAN,
ba99f19c 23 TABLE_TIMESPAN_MSEC,
1960e736 24 TABLE_SIZE,
9ff27e64 25 TABLE_BPS,
d5538326 26 TABLE_INT,
e74294c3
YW
27 TABLE_INT8,
28 TABLE_INT16,
d5538326
YW
29 TABLE_INT32,
30 TABLE_INT64,
31 TABLE_UINT,
e74294c3
YW
32 TABLE_UINT8,
33 TABLE_UINT16,
1960e736 34 TABLE_UINT32,
a4661181
LP
35 TABLE_UINT64,
36 TABLE_PERCENT,
8390d391 37 TABLE_IFINDEX,
a7a257cd
YW
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) */
137688df
LP
40 TABLE_ID128,
41 TABLE_UUID,
a7273eaf
LP
42 TABLE_UID,
43 TABLE_GID,
44 TABLE_PID,
45 TABLE_SIGNAL,
1960e736 46 _TABLE_DATA_TYPE_MAX,
6268974f
LP
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,
b0395c11
LP
56 TABLE_SET_RGAP_COLOR,
57 TABLE_SET_BOTH_COLORS,
6268974f
LP
58 TABLE_SET_URL,
59 TABLE_SET_UPPERCASE,
60
2d93c20e 61 _TABLE_DATA_TYPE_INVALID = -EINVAL,
1960e736
LP
62} TableDataType;
63
64typedef struct Table Table;
65typedef struct TableCell TableCell;
66
67Table *table_new_internal(const char *first_header, ...) _sentinel_;
68#define table_new(...) table_new_internal(__VA_ARGS__, NULL)
69Table *table_new_raw(size_t n_columns);
70Table *table_unref(Table *t);
71
72DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
73
74int 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);
75static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
f5fbe71d 76 return table_add_cell_full(t, ret_cell, type, data, SIZE_MAX, SIZE_MAX, UINT_MAX, UINT_MAX, UINT_MAX);
1960e736 77}
5896e03c 78int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) _printf_(3, 4);
1960e736 79
728a22d3
LP
80int table_fill_empty(Table *t, size_t until_column);
81
1960e736
LP
82int table_dup_cell(Table *t, TableCell *cell);
83
84int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
85int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
86int table_set_weight(Table *t, TableCell *cell, unsigned weight);
87int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
88int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
89int table_set_color(Table *t, TableCell *cell, const char *color);
b0395c11 90int table_set_rgap_color(Table *t, TableCell *cell, const char *color);
dc0e9c9b 91int table_set_url(Table *t, TableCell *cell, const char *url);
359abf6d 92int table_set_uppercase(Table *t, TableCell *cell, bool b);
1960e736 93
27e730e6
LP
94int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
95
1960e736
LP
96int 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
99void table_set_header(Table *table, bool b);
100void table_set_width(Table *t, size_t width);
d91614e7 101void table_set_cell_height_max(Table *t, size_t height);
7a9b4bb0 102int table_set_empty_string(Table *t, const char *empty);
0080964c 103int table_set_display_all(Table *t);
ef1e0b9a
YW
104int 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)
106int 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)
a2c73e2d 108int table_set_reverse(Table *t, size_t column, bool b);
0080964c 109int table_hide_column_from_display(Table *t, size_t column);
1960e736
LP
110
111int table_print(Table *t, FILE *f);
112int table_format(Table *t, char **ret);
113
114static inline TableCell* TABLE_HEADER_CELL(size_t i) {
115 return SIZE_TO_PTR(i + 1);
116}
117
118size_t table_get_rows(Table *t);
119size_t table_get_columns(Table *t);
9314ead7
LP
120
121TableCell *table_get_cell(Table *t, size_t row, size_t column);
62d99b39
LP
122
123const void *table_get(Table *t, TableCell *cell);
124const void *table_get_at(Table *t, size_t row, size_t column);
31ac2357
LP
125
126int table_to_json(Table *t, JsonVariant **ret);
a1c7a1f0 127int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags);
d8aedafb 128
e676b4fc
LP
129int table_print_with_pager(Table *t, JsonFormatFlags json_format_flags, PagerFlags pager_flags, bool show_header);
130
d8aedafb
YW
131#define table_log_add_error(r) \
132 log_error_errno(r, "Failed to add cell(s) to table: %m")
d836018a 133
4b6607d9 134#define table_log_print_error(r) \
135 log_error_errno(r, "Failed to print table: %m")
df83eb54 136
137#define table_log_sort_error(r) \
138 log_error_errno(r, "Failed to sort table: %m")