package core:text/table

⌘K
Ctrl+K
or
/

    Overview

    The package table implements ASCII/markdown/HTML/custom rendering of tables.

    Custom rendering example:

    tbl := init(&Table{})
    padding(tbl, 0, 1)
    row(tbl, "A_LONG_ENUM", "= 54,", "// A comment about A_LONG_ENUM")
    row(tbl, "AN_EVEN_LONGER_ENUM", "= 1,", "// A comment about AN_EVEN_LONGER_ENUM")
    build(tbl)
    for row in 0..<tbl.nr_rows {
    	for col in 0..<tbl.nr_cols {
    		write_table_cell(stdio_writer(), tbl, row, col)
    	}
    	io.write_byte(stdio_writer(), '\n')
    }
    
    

    This outputs:

    A_LONG_ENUM         = 54, // A comment about A_LONG_ENUM
    AN_EVEN_LONGER_ENUM = 1,  // A comment about AN_EVEN_LONGER_ENUM
    
    

    ASCII rendering example:

    tbl := init(&Table{})
    defer destroy(tbl)
    
    caption(tbl, "This is a table caption and it is very long")
    
    padding(tbl, 1, 1) // Left/right padding of cells
    
    header(tbl, "AAAAAAAAA", "B")
    header(tbl, "C") // Appends to previous header row. Same as if done header("AAAAAAAAA", "B", "C") from start.
    
    // Create a row with two values. Since there are three columns the third
    // value will become the empty string.
    //
    // NOTE: header() is not allowed anymore after this.
    row(tbl, 123, "foo")
    
    // Use `format()` if you need custom formatting. This will allocate into
    // the arena specified at init.
    row(tbl,
        format(tbl, "%09d", 5),
        format(tbl, "%.6f", 6.28318530717958647692528676655900576))
    
    // A row with zero values is allowed as long as a previous row or header
    // exist. The value and alignment of each cell can then be set
    // individually.
    row(tbl)
    	set_cell_value_and_alignment(tbl, last_row(tbl), 0, "a", .Center)
    	set_cell_value(tbl, last_row(tbl), 1, "bbb")
    	set_cell_value(tbl, last_row(tbl), 2, "c")
    
    // Headers are regular cells, too. Use header_row() as row index to modify
    // header cells.
    set_cell_alignment(tbl, header_row(tbl), 1, .Center) // Sets alignment of 'B' column to Center.
    set_cell_alignment(tbl, header_row(tbl), 2, .Right) // Sets alignment of 'C' column to Right.
    
    build(tbl)
    
    write_ascii_table(stdio_writer(), tbl)
    write_markdown_table(stdio_writer(), tbl)
    
    

    This outputs:

    +-----------------------------------------------+
    |  This is a table caption and it is very long  |
    +------------------+-----------------+----------+
    | AAAAAAAAA        |        B        |        C |
    +------------------+-----------------+----------+
    | 123              | foo             |          |
    | 000000005        | 6.283185        |          |
    |        a         | bbb             | c        |
    +------------------+-----------------+----------+
    
    

    and

    |    AAAAAAAAA     |        B        |    C     |
    |:-----------------|:---------------:|---------:|
    | 123              | foo             |          |
    | 000000005        | 6.283185        |          |
    | a                | bbb             | c        |
    
    

    respectively.

    Types

    Cell ¶

    Cell :: struct {
    	text:      string,
    	alignment: Cell_Alignment,
    }
    Related Procedures With Returns

    Cell_Alignment ¶

    Cell_Alignment :: enum int {
    	Left, 
    	Center, 
    	Right, 
    }
    Related Procedures With Parameters

    Table ¶

    Table :: struct {
    	lpad:             int,
    	rpad:             int,
    	// Cell padding (left/right)
    	cells:            [dynamic]Cell,
    	caption:          string,
    	nr_rows:          int,
    	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
    	dirty:            bool,
    	// The following are computed on build()
    	colw:             [dynamic]int,
    	// Width of each column (including padding, excluding borders)
    	tblw:             int,
    }
    Related Procedures With Parameters

    Constants

    This section is empty.

    Variables

    This section is empty.

    Procedures

    build ¶

    build :: proc(tbl: ^Table) {…}

    caption ¶

    caption :: proc(tbl: ^Table, value: string) {…}

    destroy ¶

    destroy :: proc(tbl: ^Table) {…}

    first_row ¶

    first_row :: proc(tbl: ^Table) -> int {…}

    format ¶

    format :: proc(tbl: ^Table, _fmt: string, .. args: ..any, loc := #caller_location) -> string {…}

    get_cell ¶

    get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {…}
    header :: proc(tbl: ^Table, .. values: ..any, loc := #caller_location) {…}

    header_row ¶

    header_row :: proc(tbl: ^Table) -> int {…}

    init_with_allocator ¶

    init_with_allocator :: proc(tbl: ^Table, format_allocator := context.temp_allocator, table_allocator := context.allocator) -> ^Table {…}

    init_with_mem_arena ¶

    init_with_mem_arena :: proc(tbl: ^Table, format_arena: ^mem.Arena, table_allocator := context.allocator) -> ^Table {…}

    init_with_virtual_arena ¶

    init_with_virtual_arena :: proc(tbl: ^Table, format_arena: ^mem_virtual.Arena, table_allocator := context.allocator) -> ^Table {…}

    last_row ¶

    last_row :: proc(tbl: ^Table) -> int {…}

    padding ¶

    padding :: proc(tbl: ^Table, lpad, rpad: int) {…}

    row ¶

    row :: proc(tbl: ^Table, .. values: ..any, loc := #caller_location) {…}

    set_cell_alignment ¶

    set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment, loc := #caller_location) {…}

    set_cell_value ¶

    set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {…}

    set_cell_value_and_alignment ¶

    set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: string, alignment: Cell_Alignment) {…}

    stdio_writer ¶

    stdio_writer :: proc() -> io.Stream {…}

    strings_builder_writer ¶

    strings_builder_writer :: proc(b: ^strings.Builder) -> io.Stream {…}

    write_ascii_table ¶

    write_ascii_table :: proc(w: io.Stream, tbl: ^Table) {…}

    write_byte_repeat ¶

    write_byte_repeat :: proc(w: io.Stream, n: int, b: u8) {…}

    write_html_table ¶

    write_html_table :: proc(w: io.Stream, tbl: ^Table) {…}

    write_markdown_table ¶

    write_markdown_table :: proc(w: io.Stream, tbl: ^Table) {…}
     

    Renders table according to GitHub Flavored Markdown (GFM) specification

    write_table_cell ¶

    write_table_cell :: proc(w: io.Stream, tbl: ^Table, row, col: int) {…}

    write_text_align ¶

    write_text_align :: proc(
    	w:                io.Stream, 
    	colw, lpad, rpad: int, 
    	text:             string, 
    	alignment:        Cell_Alignment, 
    ) {…}

    Procedure Groups

    Source Files

    Generation Information

    Generated with odin version dev-2024-04 (vendor "odin") Windows_amd64 @ 2024-04-26 21:08:59.120310700 +0000 UTC