Add new API to core:text/table

- `header/row_of_values`, same `header/row`, more verbose name
- `aligned_header/row_of_values`, set alignment for an entire row
- `header/row_of_aligned_values`, set alignment per value
This commit is contained in:
Feoramund
2024-06-20 17:53:45 -04:00
parent 585747bbbf
commit b81458073e
+128 -22
View File
@@ -28,6 +28,11 @@ Cell_Alignment :: enum {
Right, Right,
} }
Aligned_Value :: struct {
alignment: Cell_Alignment,
value: any,
}
// Determines the width of a string used in the table for alignment purposes. // Determines the width of a string used in the table for alignment purposes.
Width_Proc :: #type proc(str: string) -> int Width_Proc :: #type proc(str: string) -> int
@@ -96,28 +101,27 @@ get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
return &tbl.cells[row*tbl.nr_cols + col] return &tbl.cells[row*tbl.nr_cols + col]
} }
set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: string, alignment: Cell_Alignment) { @private
cell := get_cell(tbl, row, col) to_string :: #force_inline proc(tbl: ^Table, value: any, loc := #caller_location) -> (result: string) {
cell.text = format(tbl, "%v", value) switch val in value {
cell.alignment = alignment case nil:
tbl.dirty = true result = ""
case string:
result = val
case cstring:
result = cast(string)val
case:
result = format(tbl, "%v", val)
if result == "" {
fmt.eprintf("{} text/table: format() resulted in empty string (arena out of memory?)\n", loc)
}
}
return
} }
set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) { set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {
cell := get_cell(tbl, row, col, loc) cell := get_cell(tbl, row, col, loc)
switch val in value { cell.text = to_string(tbl, value, loc)
case nil:
cell.text = ""
case string:
cell.text = string(val)
case cstring:
cell.text = string(val)
case:
cell.text = format(tbl, "%v", val)
if cell.text == "" {
fmt.eprintf("{} text/table: format() resulted in empty string (arena out of memory?)\n", loc)
}
}
tbl.dirty = true tbl.dirty = true
} }
@@ -127,12 +131,20 @@ set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment
tbl.dirty = true tbl.dirty = true
} }
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
tbl.dirty = true
}
format :: proc(tbl: ^Table, _fmt: string, args: ..any) -> string { format :: proc(tbl: ^Table, _fmt: string, args: ..any) -> string {
context.allocator = tbl.format_allocator context.allocator = tbl.format_allocator
return fmt.aprintf(_fmt, ..args) return fmt.aprintf(_fmt, ..args)
} }
header :: proc(tbl: ^Table, values: ..any, loc := #caller_location) { 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) { 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) panic("Cannot add headers after rows have been added", loc)
} }
@@ -152,7 +164,48 @@ header :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
tbl.dirty = true tbl.dirty = true
} }
row :: proc(tbl: ^Table, values: ..any, loc := #caller_location) { 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
}
tbl.dirty = true
}
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
}
tbl.dirty = true
}
row :: row_of_values
row_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
if tbl.nr_cols == 0 { if tbl.nr_cols == 0 {
if len(values) == 0 { if len(values) == 0 {
panic("Cannot create empty row unless the number of columns is known in advance") panic("Cannot create empty row unless the number of columns is known in advance")
@@ -160,14 +213,67 @@ row :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
tbl.nr_cols = len(values) tbl.nr_cols = len(values)
} }
} }
tbl.nr_rows += 1 tbl.nr_rows += 1
for col in 0..<tbl.nr_cols {
for col in 0 ..< tbl.nr_cols {
val := values[col] if col < len(values) else nil val := values[col] if col < len(values) else nil
set_cell_value(tbl, last_row(tbl), col, val) set_cell_value(tbl, last_row(tbl), col, val, loc)
} }
tbl.dirty = true tbl.dirty = true
} }
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)
}
tbl.dirty = true
}
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)
}
}
tbl.dirty = true
}
// 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 { last_row :: proc(tbl: ^Table) -> int {
return tbl.nr_rows - 1 return tbl.nr_rows - 1
} }