table

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder struct {
	LocaleTag    string                 // e.g. en_US, de_DE, ...
	Prefix       model.Nullable[string] // prefix to impose on table (overrides table prefix)
	ColumnFlags  []model.ColumnFlags    // per-column alignment preferences
	ColumnWidths []model.ColumnWidth    // per-column width details
	// contains filtered or unexported fields
}

Encoder encodes a single table

Encoding typically consists of several stages, which may be used independently (e.g. when coordinating encoding of an entire document) or via the single Encode() method.

A Table Encoder cannot be re-used for multiple tables.

func (*Encoder) CollateWidths

func (enc *Encoder) CollateWidths()

CollateWidths determines how wide each column should be in order to vertically align each column of data.

Column alignment hints are also included in the column width calculations.

[EncodeData] should have been called prior to calling CollateWidths

func (*Encoder) Encode

func (enc *Encoder) Encode() []byte

Encode performs all steps necessary to encode the table as a UTF-8 string.

This includes:

  • filtering columns (TODO)
  • sorting rows (TODO)
  • encoding row data (e.g. escaping `|`s)
  • collating column widths
  • and rendering the resulting table
Example (Simple)
package main

import (
	"fmt"

	"codeberg.org/japh/psv/encoding/table"
	"codeberg.org/japh/psv/model"
)

func main() {

	// create a simple table
	tbl := &model.Table{}
	tbl.AppendRow([]string{"Name", "Points", "Color"})   // 3 column names (no special significance)
	tbl.AppendRow([]string{"Adam", "6"})                 // 2 fields, no color
	tbl.AppendRow([]string{"Alice", "", "red", "pasta"}) // 4 fields, incl. an extra, unamed field
	tbl.AppendRow([]string{"Anon"})                      // 1 field,  an almost empty row
	tbl.AppendRow([]string{"Charlie", "", "green"})      // 3 fields, no points

	enc := &table.Encoder{}
	enc.SetTable(tbl)

	output := enc.Encode()

	fmt.Println("rendered table without rulers")
	fmt.Print(string(output))

}
Output:

rendered table without rulers
| Name    | Points | Color |       |
| Adam    | 6      |       |       |
| Alice   |        | red   | pasta |
| Anon    |        |       |       |
| Charlie |        | green |       |
Example (Table_with_rulers)
package main

import (
	"fmt"

	"codeberg.org/japh/psv/encoding/table"
	"codeberg.org/japh/psv/model"
)

func main() {

	// create a simple table
	tbl := &model.Table{}
	tbl.AppendRuler("")
	tbl.AppendRow([]string{"Name", "Points", "Color"}) // 3 column names (no special significance)
	tbl.AppendRuler("")
	tbl.AppendRow([]string{"Adam", "6"})                 // 2 fields, no color
	tbl.AppendRow([]string{"Alice", "", "red", "pasta"}) // 4 fields, incl. an extra, unamed field
	tbl.AppendRow([]string{"Anon"})                      // 1 field,  an almost empty row
	tbl.AppendRow([]string{"Charlie", "", "green"})      // 3 fields, no points
	tbl.AppendRuler("")

	enc := &table.Encoder{}
	enc.SetTable(tbl)

	output := enc.Encode()

	fmt.Println("rendered table with rulers")
	fmt.Println("  - first row is center aligned")
	fmt.Print(string(output))

}
Output:

rendered table with rulers
  - first row is center aligned
| ------- | ------ | ----- | ----- |
|  Name   | Points | Color |       |
| ------- | ------ | ----- | ----- |
| Adam    | 6      |       |       |
| Alice   |        | red   | pasta |
| Anon    |        |       |       |
| Charlie |        | green |       |
| ------- | ------ | ----- | ----- |

func (*Encoder) EncodeData

func (enc *Encoder) EncodeData()

EncodeData encodes the row data by escaping critical characters within the data.

Note that column widths are calculated using the encoded data!

func (*Encoder) FilterColumns

func (enc *Encoder) FilterColumns()

FilterColumns merges the configured list of column names with the table's actual list of columns

TODO: 2025-02-26 create a simple field mapping

func (*Encoder) Prepare

func (enc *Encoder) Prepare()

Prepare performs all preliminary steps required prior to rendering a table.

This is used by the document encoder, to enable document-wide alignment. i.e. all tables are first prepared, then their column widths are merged and updated thus aligning all tables such that all data can be represented consistently.

See also [Encode]

func (*Encoder) Render

func (enc *Encoder) Render() []byte

Render renders a single table

The following functions should have been called already:

  • FilterColumns()
  • Sort()
  • EncodeData()
  • CollateWidths()

func (*Encoder) SetTable

func (enc *Encoder) SetTable(tbl *model.Table)

SetTable provides a table for encoding.

All of the encoder's metadata is reset.

func (*Encoder) SortRows

func (enc *Encoder) SortRows()

SortRows sorts a table's rows according to the ordering preferences of the encoder, or those of the table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL