-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
52 lines (42 loc) · 1.34 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include "libtbl.h"
static void
magic_formatter(int is_start, void *data)
{
(void) data;
if (is_start)
printf("\033[6m");
else
printf("\033[25m");
}
int
main(void)
{
libtbl_tbl table; /* Table printer state */
libtbl_tblcoldef cols[] = { /* Table column definitions */
{ .name = "FOO", .type = LIBTBL_TBLCOLTYPE_INT, .flags = LIBTBL_TBLCOL_JUSTIFYR },
{ .name = "IS GREEN", .type = LIBTBL_TBLCOLTYPE_BOOL, .flags = LIBTBL_TBLCOL_BOLD|LIBTBL_TBLCOL_JUSTIFYR|LIBTBL_TBLCOL_COLOUREXPL },
{ .name = "BAR", .type = LIBTBL_TBLCOLTYPE_STRING, .flags = 0 },
{ .name = "MAGIC", .type = LIBTBL_TBLCOLTYPE_LONG, .flags = LIBTBL_TBLCOL_CUSTOM },
};
/* Init the table printer */
table = libtbl_tbl_begin(cols, sizeof(cols) / sizeof(*cols));
if (!table) {
fprintf(stderr, "error: could not init table\n");
return EXIT_FAILURE;
}
/* Add rows */
for (int i = 0; i < 10; ++i) {
char buf[32] = {0};
snprintf(buf, sizeof buf, "Testing 123 -> %d", i + 42);
libtbl_tbl_add_row(table,
i + 1,
(i%2) ? LIBTBL_COLOUR_GREEN : LIBTBL_COLOUR_RED, i % 2,
buf,
magic_formatter, NULL, 42);
}
/* Dump the table and deallocate the internal allocations */
libtbl_tbl_end(table);
return EXIT_SUCCESS;
}