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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include "tabular.h"
#include "allocator.h"
#include <sys/ioctl.h>
#include <err.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#define ARRLEN(x) (sizeof(x)/sizeof(*(x)))
bool print_style(FILE *file, const struct tabular_cfg *cfg,
const struct tabular_row *row, const struct tabular_col *col);
char *col_pos_str(const struct tabular_user *row, const struct tabular_user *col);
bool col_pos_hidden(const struct tabular_user *user);
char *col_name_str(const struct tabular_user *row, const struct tabular_user *col);
bool col_name_hidden(const struct tabular_user *user);
static const char **argv = NULL;
static struct tabular_col columns[] = {
{
.name = "Pos",
.to_str = col_pos_str,
.is_hidden = NULL,
.minwidth = 3,
.maxwidth = 3,
.lpad = 0,
.rpad = 0,
.align = TABULAR_ALIGN_CENTER,
.strategy = TABULAR_TRUNC,
.essential = true,
},
{
.name = "Value",
.to_str = col_name_str,
.is_hidden = NULL,
.minwidth = 5,
.maxwidth = 80,
.lpad = 0,
.rpad = 0,
.align = TABULAR_ALIGN_LEFT,
.strategy = TABULAR_WRAP_WORDAWARE,
.essential = true,
},
};
static struct tabular_cfg cfg = {
.colors = 256,
.columns = columns,
.column_count = ARRLEN(columns),
.fit_rows = true,
.hsep = "│ ",
.vsep = "─",
.xsep = "┼─",
.outw = 0,
.outh = 0,
.lpad = 1,
.rpad = 1,
.print_style = print_style,
.skip_lines = 3,
.allocator = &stdlib_heap_allocator
};
bool
print_style(FILE *file, const struct tabular_cfg *cfg,
const struct tabular_row *row, const struct tabular_col *col)
{
if (cfg->colors == 256) {
if (!col) { /* separators */
fprintf(file, "\x1b[90m");
return true;
} else if (!row) { /* header */
fprintf(file, "\x1b[1m");
return true;
} else if (!strcmp(col->name, "Name")) {
fprintf(file, "\x1b[35m");
return true;
}
}
return false;
}
char *
col_pos_str(const struct tabular_user *row, const struct tabular_user *col)
{
char buf[16];
snprintf(buf, sizeof(buf), "%li", row->id);
return strdup(buf);
}
char *
col_name_str(const struct tabular_user *row, const struct tabular_user *col)
{
return strdup(argv[row->id]);
}
int
main(int argc, const char **_argv)
{
struct tabular_row *rows, **end;
struct tabular_stats stats;
struct winsize ws;
int i, rc;
argv = _argv;
rc = ioctl(1, TIOCGWINSZ, &ws);
if (!rc) {
cfg.outw = ws.ws_col;
cfg.outh = ws.ws_row;
} else {
cfg.outw = 80;
cfg.outh = 26;
}
rows = NULL;
end = &rows;
for (i = 0; i < argc; i++) {
*end = tabular_alloc_row(&cfg, &rc,
(struct tabular_user) { .id = (size_t) i });
if (!*end) errx(1, "tabular_append_row %i", rc);
end = &(*end)->next;
}
rc = tabular_format(stdout, &cfg, &stats, &rows);
if (rc) errx(1, "tabular_format %i", rc);
printf("\n%lu lines, %lu rows\n",
stats.lines_used, stats.rows_displayed);
tabular_free_rows(&cfg, rows);
}
|