table

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package table implements a container API for tables of data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Params

type Params struct {
	Rows        int  // max. number of rows to write; 0 = unlimited (default)
	NoHeader    bool // whether to print the header, default - yes
	MaxColWidth int  // for WriteText only; 0 = unlimited, otherwise must be >= 4
}

Params are parameters for pretty-printing or CSV export of Table data.

type Row

type Row interface {
	CSV() []string // an encoding/csv compatible row representation
}

Row interface that a table row representation must implement.

type Table

type Table struct {
	Header []string // optional, may be nil
	Rows   []Row
}

Table container.

A typical use:

type MyRow struct {
  Name string
  Age int
}

func (r MyRow) CSV() []string {
  return []string{r.Name, fmt.Sprintf("%d", r.Age)}
}
t := NewTable([]string{"Name", "Age"})
t.AddRow(MyRow{"John", 25}, MyRow{"Jane", 24})

func NewTable

func NewTable(header ...string) *Table

NewTable creates a new Table instance with optional column headers. It is expected that, when present, the number of column headers is the same as the number of elements in each Row.

func (*Table) AddRow

func (t *Table) AddRow(rows ...Row)

AddRow adds one or more rows to the table.

func (*Table) WriteCSV

func (t *Table) WriteCSV(w io.Writer, p Params) error

WriteCSV writes the entire table to w in CSV format.

func (*Table) WriteText

func (t *Table) WriteText(w io.Writer, p Params) error

WriteText writes the table as a text formatted for ease of reading.

Jump to

Keyboard shortcuts

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