asciitable

package
v0.0.0-...-5c79d48 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package asciitable implements a simple ASCII table formatter for printing tabular values into a text terminal.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Title         string
	MaxCellLength int
	FootnoteLabel string
	// contains filtered or unexported fields
}

Column represents a column in the table.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table holds tabular values in a rows and columns format.

func MakeHeadlessTable

func MakeHeadlessTable(columnCount int) Table

MakeHeadlessTable creates a new instance of the table without any column names. The number of columns is required.

func MakeTable

func MakeTable(headers []string, rows ...[]string) Table

MakeTable creates a new instance of the table with given column names. Optionally rows to be added to the table may be included.

Example
// Create a table with three column headers.
t := MakeTable([]string{"Token", "Type", "Expiry Time (UTC)"})

// Add in multiple rows.
t.AddRow([]string{"b53bd9d3e04add33ac53edae1a2b3d4f", "auth", "30 Aug 18 23:31 UTC"})
t.AddRow([]string{"5ecde0ca17824454b21937109df2c2b5", "node", "30 Aug 18 23:31 UTC"})
t.AddRow([]string{"9333929146c08928a36466aea12df963", "trusted_cluster", "30 Aug 18 23:33 UTC"})

// Write the table to stdout.
fmt.Println(t.AsBuffer().String())
Output:

func MakeTableWithTruncatedColumn

func MakeTableWithTruncatedColumn(columnOrder []string, rows [][]string, truncatedColumn string) Table

MakeTableWithTruncatedColumn creates a table where the column matching truncatedColumn will be shortened to account for terminal width.

func (*Table) AddColumn

func (t *Table) AddColumn(c Column)

AddColumn adds a column to the table's structure.

func (*Table) AddFootnote

func (t *Table) AddFootnote(label string, note string)

AddFootnote adds a footnote for referencing from truncated cells.

func (*Table) AddRow

func (t *Table) AddRow(row []string)

AddRow adds a row of cells to the table.

func (*Table) AsBuffer

func (t *Table) AsBuffer() *bytes.Buffer

AsBuffer returns a *bytes.Buffer with the printed output of the table.

func (*Table) IsHeadless

func (t *Table) IsHeadless() bool

IsHeadless returns true if none of the table title cells contains any text.

func (*Table) SortRowsBy

func (t *Table) SortRowsBy(colIdxKey []int, stable bool)

SortRowsBy sorts the table rows with the given column indices as the sorting key, optionally performing a stable sort. Column indices out of range are ignored - it is the caller's responsibility to ensure the indices are in range.

Jump to

Keyboard shortcuts

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