]>
Commit | Line | Data |
---|---|---|
1960e736 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | #pragma once | |
3 | ||
4 | #include <stdbool.h> | |
5 | #include <stdio.h> | |
6 | #include <sys/types.h> | |
7 | ||
8 | #include "macro.h" | |
9 | ||
10 | typedef enum TableDataType { | |
11 | TABLE_EMPTY, | |
12 | TABLE_STRING, | |
13 | TABLE_BOOLEAN, | |
14 | TABLE_TIMESTAMP, | |
15 | TABLE_TIMESPAN, | |
16 | TABLE_SIZE, | |
17 | TABLE_UINT32, | |
18 | _TABLE_DATA_TYPE_MAX, | |
19 | _TABLE_DATA_TYPE_INVALID = -1, | |
20 | } TableDataType; | |
21 | ||
22 | typedef struct Table Table; | |
23 | typedef struct TableCell TableCell; | |
24 | ||
25 | Table *table_new_internal(const char *first_header, ...) _sentinel_; | |
26 | #define table_new(...) table_new_internal(__VA_ARGS__, NULL) | |
27 | Table *table_new_raw(size_t n_columns); | |
28 | Table *table_unref(Table *t); | |
29 | ||
30 | DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref); | |
31 | ||
32 | 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); | |
33 | static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) { | |
34 | return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1); | |
35 | } | |
36 | ||
37 | int table_dup_cell(Table *t, TableCell *cell); | |
38 | ||
39 | int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width); | |
40 | int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width); | |
41 | int table_set_weight(Table *t, TableCell *cell, unsigned weight); | |
42 | int table_set_align_percent(Table *t, TableCell *cell, unsigned percent); | |
43 | int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent); | |
44 | int table_set_color(Table *t, TableCell *cell, const char *color); | |
45 | ||
46 | int table_add_many_internal(Table *t, TableDataType first_type, ...); | |
47 | #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX) | |
48 | ||
49 | void table_set_header(Table *table, bool b); | |
50 | void table_set_width(Table *t, size_t width); | |
51 | int table_set_display(Table *t, size_t first_column, ...); | |
52 | int table_set_sort(Table *t, size_t first_column, ...); | |
53 | ||
54 | int table_print(Table *t, FILE *f); | |
55 | int table_format(Table *t, char **ret); | |
56 | ||
57 | static inline TableCell* TABLE_HEADER_CELL(size_t i) { | |
58 | return SIZE_TO_PTR(i + 1); | |
59 | } | |
60 | ||
61 | size_t table_get_rows(Table *t); | |
62 | size_t table_get_columns(Table *t); |