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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
|
/*
Copyright 2023 oskarnp <oskarnp@proton.me>
Made available under Odin's license.
List of contributors:
oskarnp: Initial implementation.
Feoramund: Unicode support.
*/
package text_table
import "core:io"
import "core:fmt"
import "core:log"
import "core:mem"
import "core:mem/virtual"
import "core:unicode/utf8"
import "base:runtime"
Cell :: struct {
text: string,
width: int,
alignment: Cell_Alignment,
}
Cell_Alignment :: enum {
Left,
Center,
Right,
}
Aligned_Value :: struct {
alignment: Cell_Alignment,
value: any,
}
// Determines the width of a string used in the table for alignment purposes.
Width_Proc :: #type proc(str: string) -> int
Table :: struct {
lpad, rpad: int, // Cell padding (left/right)
cells: [dynamic]Cell,
caption: string,
nr_rows, nr_cols: int,
has_header_row: bool,
table_allocator: runtime.Allocator, // Used for allocating cells/colw
format_allocator: runtime.Allocator, // Used for allocating Cell.text when applicable
// The following are computed on build()
colw: [dynamic]int, // Width of each column (excluding padding and borders)
tblw: int, // Width of entire table (including padding, excluding borders)
}
Decorations :: struct {
// These are strings, because of multi-codepoint Unicode graphemes.
// Connecting decorations:
nw, n, ne,
w, x, e,
sw, s, se: string,
// Straight lines:
vert: string,
horz: string,
}
ascii_width_proc :: proc(str: string) -> int {
return len(str)
}
unicode_width_proc :: proc(str: string) -> (width: int) {
_, _, width = #force_inline utf8.grapheme_count(str)
return
}
init :: proc{init_with_allocator, init_with_virtual_arena, init_with_mem_arena}
init_with_allocator :: proc(tbl: ^Table, format_allocator := context.temp_allocator, table_allocator := context.allocator) -> ^Table {
tbl.table_allocator = table_allocator
tbl.cells = make([dynamic]Cell, tbl.table_allocator)
tbl.colw = make([dynamic]int, tbl.table_allocator)
tbl.format_allocator = format_allocator
return tbl
}
init_with_virtual_arena :: proc(tbl: ^Table, format_arena: ^virtual.Arena, table_allocator := context.allocator) -> ^Table {
return init_with_allocator(tbl, virtual.arena_allocator(format_arena), table_allocator)
}
init_with_mem_arena :: proc(tbl: ^Table, format_arena: ^mem.Arena, table_allocator := context.allocator) -> ^Table {
return init_with_allocator(tbl, mem.arena_allocator(format_arena), table_allocator)
}
destroy :: proc(tbl: ^Table) {
free_all(tbl.format_allocator)
delete(tbl.cells)
delete(tbl.colw)
}
caption :: proc(tbl: ^Table, value: string) {
tbl.caption = value
}
padding :: proc(tbl: ^Table, lpad, rpad: int) {
tbl.lpad = lpad
tbl.rpad = rpad
}
get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
assert(col >= 0 && col < tbl.nr_cols, "cell column out of range", loc)
assert(row >= 0 && row < tbl.nr_rows, "cell row out of range", loc)
resize(&tbl.cells, tbl.nr_cols * tbl.nr_rows)
return &tbl.cells[row*tbl.nr_cols + col]
}
@private
to_string :: #force_inline proc(tbl: ^Table, value: any, loc := #caller_location) -> (result: string) {
switch val in value {
case nil:
result = ""
case string:
result = val
case cstring:
result = cast(string)val
case:
result = format(tbl, "%v", val)
if result == "" {
log.error("text/table.format() resulted in empty string (arena out of memory?)", location = loc)
}
}
return
}
set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {
cell := get_cell(tbl, row, col, loc)
cell.text = to_string(tbl, value, loc)
}
set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment, loc := #caller_location) {
cell := get_cell(tbl, row, col, loc)
cell.alignment = alignment
}
set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: any, alignment: Cell_Alignment, loc := #caller_location) {
cell := get_cell(tbl, row, col, loc)
cell.text = to_string(tbl, value, loc)
cell.alignment = alignment
}
format :: proc(tbl: ^Table, _fmt: string, args: ..any) -> string {
context.allocator = tbl.format_allocator
return fmt.aprintf(_fmt, ..args)
}
header :: header_of_values
header_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
panic("Cannot add headers after rows have been added", loc)
}
if tbl.nr_rows == 0 {
tbl.nr_rows += 1
tbl.has_header_row = true
}
col := tbl.nr_cols
tbl.nr_cols += len(values)
for val in values {
set_cell_value(tbl, header_row(tbl), col, val, loc)
col += 1
}
}
aligned_header_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
panic("Cannot add headers after rows have been added", loc)
}
if tbl.nr_rows == 0 {
tbl.nr_rows += 1
tbl.has_header_row = true
}
col := tbl.nr_cols
tbl.nr_cols += len(values)
for val in values {
set_cell_value_and_alignment(tbl, header_row(tbl), col, val, alignment, loc)
col += 1
}
}
header_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
panic("Cannot add headers after rows have been added", loc)
}
if tbl.nr_rows == 0 {
tbl.nr_rows += 1
tbl.has_header_row = true
}
col := tbl.nr_cols
tbl.nr_cols += len(aligned_values)
for av in aligned_values {
set_cell_value_and_alignment(tbl, header_row(tbl), col, av.value, av.alignment, loc)
col += 1
}
}
row :: row_of_values
row_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
if tbl.nr_cols == 0 {
if len(values) == 0 {
panic("Cannot create empty row unless the number of columns is known in advance")
} else {
tbl.nr_cols = len(values)
}
}
tbl.nr_rows += 1
for col in 0..<tbl.nr_cols {
val := values[col] if col < len(values) else nil
set_cell_value(tbl, last_row(tbl), col, val, loc)
}
}
aligned_row_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
if tbl.nr_cols == 0 {
if len(values) == 0 {
panic("Cannot create empty row unless the number of columns is known in advance")
} else {
tbl.nr_cols = len(values)
}
}
tbl.nr_rows += 1
for col in 0..<tbl.nr_cols {
val := values[col] if col < len(values) else nil
set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
}
}
row_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
if tbl.nr_cols == 0 {
if len(aligned_values) == 0 {
panic("Cannot create empty row unless the number of columns is known in advance")
} else {
tbl.nr_cols = len(aligned_values)
}
}
tbl.nr_rows += 1
for col in 0..<tbl.nr_cols {
if col < len(aligned_values) {
val := aligned_values[col].value
alignment := aligned_values[col].alignment
set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
} else {
set_cell_value_and_alignment(tbl, last_row(tbl), col, "", .Left, loc)
}
}
}
// TODO: This should work correctly when #3262 is fixed.
// row :: proc {
// row_of_values,
// aligned_row_of_values,
// row_of_aligned_values,
// }
last_row :: proc(tbl: ^Table) -> int {
return tbl.nr_rows - 1
}
header_row :: proc(tbl: ^Table) -> int {
return 0 if tbl.has_header_row else -1
}
first_row :: proc(tbl: ^Table) -> int {
return header_row(tbl)+1 if tbl.has_header_row else 0
}
build :: proc(tbl: ^Table, width_proc: Width_Proc) {
resize(&tbl.colw, tbl.nr_cols)
mem.zero_slice(tbl.colw[:])
for row in 0..<tbl.nr_rows {
for col in 0..<tbl.nr_cols {
cell := get_cell(tbl, row, col)
cell.width = width_proc(cell.text)
tbl.colw[col] = max(tbl.colw[col], cell.width)
}
}
colw_sum := 0
for v in tbl.colw {
colw_sum += v + tbl.lpad + tbl.rpad
}
tbl.tblw = max(colw_sum, width_proc(tbl.caption) + tbl.lpad + tbl.rpad)
// Resize columns to match total width of table
remain := tbl.tblw-colw_sum
for col := 0; remain > 0; col = (col + 1) % tbl.nr_cols {
tbl.colw[col] += 1
remain -= 1
}
return
}
write_html_table :: proc(w: io.Writer, tbl: ^Table) {
io.write_string(w, "<table>\n")
if tbl.caption != "" {
io.write_string(w, "\t<caption>")
io.write_string(w, tbl.caption)
io.write_string(w, "</caption>\n")
}
align_attribute :: proc(cell: ^Cell) -> string {
switch cell.alignment {
case .Left: return ` align="left"`
case .Center: return ` align="center"`
case .Right: return ` align="right"`
}
unreachable()
}
if tbl.has_header_row {
io.write_string(w, "\t<thead>\n")
io.write_string(w, "\t\t<tr>\n")
for col in 0..<tbl.nr_cols {
cell := get_cell(tbl, header_row(tbl), col)
io.write_string(w, "\t\t\t<th")
io.write_string(w, align_attribute(cell))
io.write_string(w, ">")
io.write_string(w, cell.text)
io.write_string(w, "</th>\n")
}
io.write_string(w, "\t\t</tr>\n")
io.write_string(w, "\t</thead>\n")
}
io.write_string(w, "\t<tbody>\n")
for row in 0..<tbl.nr_rows {
if tbl.has_header_row && row == header_row(tbl) {
continue
}
io.write_string(w, "\t\t<tr>\n")
for col in 0..<tbl.nr_cols {
cell := get_cell(tbl, row, col)
io.write_string(w, "\t\t\t<td")
io.write_string(w, align_attribute(cell))
io.write_string(w, ">")
io.write_string(w, cell.text)
io.write_string(w, "</td>\n")
}
io.write_string(w, "\t\t</tr>\n")
}
io.write_string(w, "\t</tbody>\n")
io.write_string(w, "</table>\n")
}
write_plain_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
build(tbl, width_proc)
write_caption_separator :: proc(w: io.Writer, tbl: ^Table) {
io.write_byte(w, '+')
write_byte_repeat(w, tbl.tblw + tbl.nr_cols - 1, '-')
io.write_byte(w, '+')
io.write_byte(w, '\n')
}
write_table_separator :: proc(w: io.Writer, tbl: ^Table) {
for col in 0..<tbl.nr_cols {
if col == 0 {
io.write_byte(w, '+')
}
write_byte_repeat(w, tbl.colw[col] + tbl.lpad + tbl.rpad, '-')
io.write_byte(w, '+')
}
io.write_byte(w, '\n')
}
if tbl.caption != "" {
write_caption_separator(w, tbl)
io.write_byte(w, '|')
write_text_align(w, tbl.caption, .Center,
tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
io.write_byte(w, '|')
io.write_byte(w, '\n')
}
write_table_separator(w, tbl)
for row in 0..<tbl.nr_rows {
for col in 0..<tbl.nr_cols {
if col == 0 {
io.write_byte(w, '|')
}
write_table_cell(w, tbl, row, col)
io.write_byte(w, '|')
}
io.write_byte(w, '\n')
if tbl.has_header_row && row == header_row(tbl) {
write_table_separator(w, tbl)
}
}
write_table_separator(w, tbl)
}
write_decorated_table :: proc(w: io.Writer, tbl: ^Table, decorations: Decorations, width_proc: Width_Proc = unicode_width_proc) {
draw_dividing_row :: proc(w: io.Writer, tbl: ^Table, left, horz, divider, right: string) {
io.write_string(w, left)
for col in 0..<tbl.nr_cols {
for _ in 0..<tbl.colw[col] + tbl.lpad + tbl.rpad {
io.write_string(w, horz)
}
if col < tbl.nr_cols-1 {
io.write_string(w, divider)
}
}
io.write_string(w, right)
io.write_byte(w, '\n')
}
build(tbl, width_proc)
// This determines whether or not to divide the top border.
top_divider := decorations.n if len(tbl.caption) == 0 else decorations.horz
// Draw the north border.
draw_dividing_row(w, tbl, decorations.nw, decorations.horz, top_divider, decorations.ne)
if len(tbl.caption) != 0 {
// Draw the caption.
io.write_string(w, decorations.vert)
write_text_align(w, tbl.caption, .Center,
tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
io.write_string(w, decorations.vert)
io.write_byte(w, '\n')
// Draw the divider between the caption and the table rows.
draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.n, decorations.e)
}
// Draw the header.
start := 0
if tbl.has_header_row {
io.write_string(w, decorations.vert)
row := header_row(tbl)
for col in 0..<tbl.nr_cols {
write_table_cell(w, tbl, row, col)
io.write_string(w, decorations.vert)
}
io.write_byte(w, '\n')
start += row + 1
draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.x, decorations.e)
}
// Draw the cells.
for row in start..<tbl.nr_rows {
for col in 0..<tbl.nr_cols {
if col == 0 {
io.write_string(w, decorations.vert)
}
write_table_cell(w, tbl, row, col)
io.write_string(w, decorations.vert)
}
io.write_byte(w, '\n')
}
// Draw the south border.
draw_dividing_row(w, tbl, decorations.sw, decorations.horz, decorations.s, decorations.se)
}
// Renders table according to GitHub Flavored Markdown (GFM) specification
write_markdown_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
// NOTE(oskar): Captions or colspans are not supported by GFM as far as I can tell.
build(tbl, width_proc)
write_row :: proc(w: io.Writer, tbl: ^Table, row: int, alignment: Cell_Alignment = .Left) {
for col in 0..<tbl.nr_cols {
cell := get_cell(tbl, row, col)
if col == 0 {
io.write_byte(w, '|')
}
write_text_align(w, cell.text, alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
io.write_string(w, "|")
}
io.write_byte(w, '\n')
}
start := 0
if tbl.has_header_row {
row := header_row(tbl)
write_row(w, tbl, row, .Center)
for col in 0..<tbl.nr_cols {
cell := get_cell(tbl, row, col)
if col == 0 {
io.write_byte(w, '|')
}
divider_width := tbl.colw[col] + tbl.lpad + tbl.rpad - 1
switch cell.alignment {
case .Left:
io.write_byte(w, ':')
write_byte_repeat(w, max(1, divider_width), '-')
case .Center:
io.write_byte(w, ':')
write_byte_repeat(w, max(1, divider_width - 1), '-')
io.write_byte(w, ':')
case .Right:
write_byte_repeat(w, max(1, divider_width), '-')
io.write_byte(w, ':')
}
io.write_byte(w, '|')
}
io.write_byte(w, '\n')
start += row + 1
}
for row in start..<tbl.nr_rows {
write_row(w, tbl, row)
}
}
write_byte_repeat :: proc(w: io.Writer, n: int, b: byte) {
for _ in 0..<n {
io.write_byte(w, b)
}
}
write_table_cell :: proc(w: io.Writer, tbl: ^Table, row, col: int) {
cell := get_cell(tbl, row, col)
write_text_align(w, cell.text, cell.alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
}
write_text_align :: proc(w: io.Writer, text: string, alignment: Cell_Alignment, lpad, rpad, space: int) {
write_byte_repeat(w, lpad, ' ')
switch alignment {
case .Left:
io.write_string(w, text)
write_byte_repeat(w, space, ' ')
case .Center:
write_byte_repeat(w, space/2, ' ')
io.write_string(w, text)
write_byte_repeat(w, space/2 + space & 1, ' ')
case .Right:
write_byte_repeat(w, space, ' ')
io.write_string(w, text)
}
write_byte_repeat(w, rpad, ' ')
}
|