-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimpletables.h
More file actions
328 lines (299 loc) · 9.01 KB
/
simpletables.h
File metadata and controls
328 lines (299 loc) · 9.01 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2022 h3xcode
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SIMPLETABLES_H
#define SIMPLETABLES_H
#include <errno.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#ifndef ST_STYLE
#define ST_STYLE 1
#endif
#if ST_STYLE == 1
#define _ST_SULC "*"
#define _ST_SURC "*"
#define _ST_SLLC "*"
#define _ST_SLRC "*"
#define _ST_SHL "*"
#define _ST_SVL "*"
#define _ST_SUT "*"
#define _ST_SLT "*"
#define _ST_SLL "*"
#define _ST_SRL "*"
#define _ST_SC "*"
#elif ST_STYLE == 2
#define _ST_SULC "┌"
#define _ST_SURC "┐"
#define _ST_SLLC "└"
#define _ST_SLRC "┘"
#define _ST_SHL "─"
#define _ST_SVL "│"
#define _ST_SUT "┬"
#define _ST_SLT "┴"
#define _ST_SLL "├"
#define _ST_SRL "┤"
#define _ST_SC "┼"
#elif ST_STYLE == 0
// Custom style
#ifndef ST_SULC
#error "ST_STYLE is set to 0, but ST_SULC is not defined"
//
#endif
#ifndef ST_SURC
#error "ST_STYLE is set to 0, but ST_SURC is not defined"
#endif
#ifndef ST_SLLC
#error "ST_STYLE is set to 0, but ST_SLLC is not defined"
#endif
#ifndef ST_SLRC
#error "ST_STYLE is set to 0, but ST_SLRC is not defined"
#endif
#ifndef ST_SHL
#error "ST_STYLE is set to 0, but ST_SHL is not defined"
#endif
#ifndef ST_SVL
#error "ST_STYLE is set to 0, but ST_SVL is not defined"
#endif
#ifndef ST_SUT
#error "ST_STYLE is set to 0, but ST_SUT is not defined"
#endif
#ifndef ST_SLT
#error "ST_STYLE is set to 0, but ST_SLT is not defined"
#endif
#ifndef ST_SLL
#error "ST_STYLE is set to 0, but ST_SLL is not defined"
#endif
#ifndef ST_SRL
#error "ST_STYLE is set to 0, but ST_SRL is not defined"
#endif
#ifndef ST_SC
#error "ST_STYLE is set to 0, but ST_SC is not defined"
#endif
#define _ST_SULC ST_SULC
#define _ST_SURC ST_SURC
#define _ST_SLLC ST_SLLC
#define _ST_SLRC ST_SLRC
#define _ST_SHL ST_SHL
#define _ST_SVL ST_SVL
#define _ST_SUT ST_SUT
#define _ST_SLT ST_SLT
#define _ST_SLL ST_SLL
#define _ST_SRL ST_SRL
#define _ST_SC ST_SC
#else
#error "Invalid ST_STYLE"
#endif
struct st_row {
int columns_count;
char **columns;
};
typedef struct {
char *sulc;
char *surc;
char *sllc;
char *slrc;
char *shl;
char *svl;
char *sut;
char *slt;
char *sll;
char *srl;
char *sc;
bool bold_header;
} st_style;
st_style st_default_style = {_ST_SULC, _ST_SURC, _ST_SLLC, _ST_SLRC,
_ST_SHL, _ST_SVL, _ST_SUT, _ST_SLT,
_ST_SLL, _ST_SRL, _ST_SC, true};
typedef struct {
char *title;
int width;
int usable_width;
int columns_count;
float *columns_width_modifiers;
char **columns;
int rows_count;
struct st_row *rows;
} st_table;
st_table *st_create_table(char *title, int width, int columns_count) {
st_table *table = malloc(sizeof(st_table));
table->title = title;
table->width = width;
table->usable_width = width - (columns_count);
table->columns_count = columns_count;
table->columns_width_modifiers = malloc(sizeof(float) * columns_count);
table->columns = malloc(sizeof(char *) * columns_count);
table->rows_count = 0;
table->rows = malloc(sizeof(struct st_row));
table->rows[0] = (struct st_row){0, NULL};
float default_width_modifier = 1.0 / columns_count;
for (int i = 0; i < columns_count; i++) {
table->columns_width_modifiers[i] = default_width_modifier;
}
return table;
}
void st_set_column(st_table *table, int column, char *name,
float width_modifier) {
table->columns[column] = name;
table->columns_width_modifiers[column] = width_modifier;
}
void st_add_row(st_table *table, char **row) {
table->rows_count++;
table->rows = realloc(table->rows, sizeof(struct st_row) * table->rows_count);
table->rows[table->rows_count - 1] =
(struct st_row){table->columns_count, row};
}
char *_st_center_text(char *text, int width) {
int text_length = strlen(text);
int right_padding = (width - text_length) / 2;
int left_padding = width - text_length - right_padding;
char *result = malloc(sizeof(char) * (width + 1));
for (int i = 0; i < left_padding; i++) {
result[i] = ' ';
}
for (int i = 0; i < text_length; i++) {
result[i + left_padding] = text[i];
}
for (int i = 0; i < right_padding; i++) {
result[i + left_padding + text_length] = ' ';
}
result[width] = '\0';
return result;
}
st_style *st_create_style(char *sulc, char *surc, char *sllc, char *slrc,
char *shl, char *svl, char *sut, char *slt, char *sll,
char *srl, char *sc, bool bold_header) {
st_style *style = malloc(sizeof(st_style));
style->sulc = malloc(sizeof(char) * (strlen(sulc) + 1));
strcpy(style->sulc, sulc);
style->surc = malloc(sizeof(char) * (strlen(surc) + 1));
strcpy(style->surc, surc);
style->sllc = malloc(sizeof(char) * (strlen(sllc) + 1));
strcpy(style->sllc, sllc);
style->slrc = malloc(sizeof(char) * (strlen(slrc) + 1));
strcpy(style->slrc, slrc);
style->shl = malloc(sizeof(char) * (strlen(shl) + 1));
strcpy(style->shl, shl);
style->svl = malloc(sizeof(char) * (strlen(svl) + 1));
strcpy(style->svl, svl);
style->sut = malloc(sizeof(char) * (strlen(sut) + 1));
strcpy(style->sut, sut);
style->slt = malloc(sizeof(char) * (strlen(slt) + 1));
strcpy(style->slt, slt);
style->sll = malloc(sizeof(char) * (strlen(sll) + 1));
strcpy(style->sll, sll);
style->srl = malloc(sizeof(char) * (strlen(srl) + 1));
strcpy(style->srl, srl);
style->sc = malloc(sizeof(char) * (strlen(sc) + 1));
strcpy(style->sc, sc);
style->bold_header = bold_header;
return style;
}
void st_print_header_styled(st_table *table, st_style *style) {
char *title = _st_center_text(table->title, table->width);
if (style->bold_header) attron(A_BOLD);
printw("%s\n", title);
if (style->bold_header) attroff(A_BOLD);
free(title);
printw("%s", style->sulc);
for (int i = 0; i < table->columns_count; i++) {
int column_width = table->columns_width_modifiers[i] * table->usable_width;
for (int j = 0; j < column_width; j++) {
printw("%s", style->shl);
}
if (i < table->columns_count - 1) {
printw("%s", style->sut);
}
}
printw("%s\n", style->surc);
printw("%s", style->svl);
for (int i = 0; i < table->columns_count; i++) {
int column_width = table->columns_width_modifiers[i] * table->usable_width;
char *column = _st_center_text(table->columns[i], column_width);
if (style->bold_header) attron(A_BOLD);
printw("%s", column);
if (style->bold_header) attroff(A_BOLD);
free(column);
printw(style->svl);
}
printw("\n");
printw("%s", style->sll);
for (int i = 0; i < table->columns_count; i++) {
int column_width = table->columns_width_modifiers[i] * table->usable_width;
for (int j = 0; j < column_width; j++) {
printw("%s", style->shl);
}
if (i < table->columns_count - 1) {
printw("%s", style->sc);
}
}
printw("%s\n", style->srl);
}
void st_print_row_styled(st_table *table, int row, st_style *style) {
printw("%s", style->svl);
for (int i = 0; i < table->columns_count; i++) {
int column_width = table->columns_width_modifiers[i] * table->usable_width;
char *column = _st_center_text(table->rows[row].columns[i], column_width);
printw("%s", column);
free(column);
printw(style->svl);
}
printw("\n");
}
void st_print_rows_styled(st_table *table, st_style *style) {
for (int i = 0; i < table->rows_count; i++) {
st_print_row_styled(table, i, style);
}
}
void st_print_styled(st_table *table, st_style *style) {
st_print_header_styled(table, style);
st_print_rows_styled(table, style);
printw("%s", style->sllc);
for (int i = 0; i < table->columns_count; i++) {
int column_width = table->columns_width_modifiers[i] * table->usable_width;
for (int j = 0; j < column_width; j++) {
printw("%s", style->shl);
}
if (i < table->columns_count - 1) {
printw("%s", style->slt);
}
}
printw("%s\n", style->slrc);
}
void st_free_style(st_style *style) {
free(style->sulc);
free(style->surc);
free(style->sllc);
free(style->slrc);
free(style->shl);
free(style->svl);
free(style->sut);
free(style->slt);
free(style->sll);
free(style->srl);
free(style->sc);
free(style);
}
void st_free(st_table *table) {
free(table->columns_width_modifiers);
free(table->columns);
free(table->rows);
free(table);
}
#define st_print_header(table) st_print_header_styled(table, &st_default_style)
#define st_print_row(table, row) \
st_print_row_styled(table, row, &st_default_style)
#define st_print_rows(table) st_print_rows_styled(table, &st_default_style)
#define st_print(table) st_print_styled(table, &st_default_style)
#endif