1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 typedef enum TableDataType
{
17 TABLE_TIMESTAMP_RELATIVE
,
32 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
33 * a cell just added. */
34 TABLE_SET_MINIMUM_WIDTH
,
35 TABLE_SET_MAXIMUM_WIDTH
,
37 TABLE_SET_ALIGN_PERCENT
,
38 TABLE_SET_ELLIPSIZE_PERCENT
,
43 _TABLE_DATA_TYPE_INVALID
= -1,
46 typedef struct Table Table
;
47 typedef struct TableCell TableCell
;
49 Table
*table_new_internal(const char *first_header
, ...) _sentinel_
;
50 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
51 Table
*table_new_raw(size_t n_columns
);
52 Table
*table_unref(Table
*t
);
54 DEFINE_TRIVIAL_CLEANUP_FUNC(Table
*, table_unref
);
56 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
);
57 static inline int table_add_cell(Table
*t
, TableCell
**ret_cell
, TableDataType type
, const void *data
) {
58 return table_add_cell_full(t
, ret_cell
, type
, data
, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
60 int table_add_cell_stringf(Table
*t
, TableCell
**ret_cell
, const char *format
, ...) _printf_(3, 4);
62 int table_dup_cell(Table
*t
, TableCell
*cell
);
64 int table_set_minimum_width(Table
*t
, TableCell
*cell
, size_t minimum_width
);
65 int table_set_maximum_width(Table
*t
, TableCell
*cell
, size_t maximum_width
);
66 int table_set_weight(Table
*t
, TableCell
*cell
, unsigned weight
);
67 int table_set_align_percent(Table
*t
, TableCell
*cell
, unsigned percent
);
68 int table_set_ellipsize_percent(Table
*t
, TableCell
*cell
, unsigned percent
);
69 int table_set_color(Table
*t
, TableCell
*cell
, const char *color
);
70 int table_set_url(Table
*t
, TableCell
*cell
, const char *url
);
71 int table_set_uppercase(Table
*t
, TableCell
*cell
, bool b
);
73 int table_update(Table
*t
, TableCell
*cell
, TableDataType type
, const void *data
);
75 int table_add_many_internal(Table
*t
, TableDataType first_type
, ...);
76 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
78 void table_set_header(Table
*table
, bool b
);
79 void table_set_width(Table
*t
, size_t width
);
80 int table_set_display(Table
*t
, size_t first_column
, ...);
81 int table_set_sort(Table
*t
, size_t first_column
, ...);
82 int table_set_reverse(Table
*t
, size_t column
, bool b
);
84 int table_print(Table
*t
, FILE *f
);
85 int table_format(Table
*t
, char **ret
);
87 static inline TableCell
* TABLE_HEADER_CELL(size_t i
) {
88 return SIZE_TO_PTR(i
+ 1);
91 size_t table_get_rows(Table
*t
);
92 size_t table_get_columns(Table
*t
);
94 TableCell
*table_get_cell(Table
*t
, size_t row
, size_t column
);
96 const void *table_get(Table
*t
, TableCell
*cell
);
97 const void *table_get_at(Table
*t
, size_t row
, size_t column
);
99 int table_to_json(Table
*t
, JsonVariant
**ret
);
100 int table_print_json(Table
*t
, FILE *f
, JsonFormatFlags json_flags
);