table

package module
v0.0.0-...-2c1ff39 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 5 Imported by: 0

README

table

create width-aware tables for the terminal and markdown and things like that

Documentation

Overview

Package table lets you create simple text-based tables that are width-aware, meaning your cells can have escape sequences or emoji in them without everything breaking.

It has only been tested with left-to-right text.

Index

Examples

Constants

View Source
const (
	// ColumnAlignRight means to align things to the right of a
	// cell, like for whole numbers.
	ColumnAlignRight = ColumnAlign(-1)
	// ColumnAlignCenter would align things in the middle of a
	// cell.
	ColumnAlignCenter = ColumnAlign(0)
	// ColumnAlignLeft is the default.
	ColumnAlignLeft = ColumnAlign(1)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnAlign

type ColumnAlign int8

ColumnAlign is used to let columns align in different ways.

type Rule

type Rule struct {
	// the rule Gutter, if set, must have the same width as the table gutter.
	// If not set, falls back to the table gutter.
	Gutter string
	// all these runes should have width 1
	// Rule is the rule itself; if not set, no horizontal rule is drawn
	Rule rune
	// if not set these fall back to Rule
	RightAlignedLeftPad   rune
	RightAlignedRightPad  rune
	LeftAlignedLeftPad    rune
	LeftAlignedRightPad   rune
	CenterAlignedLeftPad  rune
	CenterAlignedRightPad rune
}

A Rule contains information for drawing a horizontal rule between the header and the data in the table

type Table

type Table struct {
	// Gutter is used to separate cells
	Gutter string
	// Indent is added to the beginning of every line
	Indent string
	// Outdent is added to the end of every line
	Outdent string
	// Rule can be set for drawing a line between headers and data
	// (see details in Rule itself)
	Rule Rule
	// Alignment of each column (including headers)
	Align []ColumnAlign
	// TermWidth is the width of the terminal
	TermWidth int
	// BeginRow, is called before printing each row
	BeginRow func(out io.Writer)
	// EndRow is called after printing each row (before printing the newline)
	EndRow func(out io.Writer)
	// Space is the rune used to fill a cell to a uniform width. Must have width 1.
	Space rune
	// Pad is the rune used as padding around cells. Must be width 1.
	Pad rune
	// contains filtered or unexported fields
}

A Table is a way to print tabular data to the terminal, where each column of data has a header, there is a minimum fixed gutter between columns (that can be empty), and all columns except the last are reasonably short such that no truncation is necessary to fit them, and a truncated last column, in the terminal.

func New

func New(headers ...interface{}) *Table

New initializes a Table with reasonable defaults for the given headers.

The headers can be a string, or a StringAndWidther; anything else will panic.

Example
package main

import (
	"os"

	"github.com/chipaca/escapes"
	"github.com/chipaca/table"
)

var countryData = [][]interface{}{
	{"004", "AF", "Afghanistan", "Kabul"},
	{"248", "AX", "Åland Islands", "Mariehamn"},
	{"008", "AL", "Albania", "Tirana"},
	{"012", "DZ", "Algeria", "Algiers"},
	{"016", "AS", "American Samoa", "Pago Pago"},
	{"020", "AD", "Andorra", "Andorra la Vella"},
	{"024", "AO", "Angola", "Luanda"},
}

func main() {
	t := table.New(escapes.Bold("numeric"), escapes.Bold("alpha-2"), escapes.Bold("name"), escapes.Bold("capital"))
	t.Outdent = "\u200b" // this is to trick go's example tests' overly aggressive whitespace trimming
	t.Align[0] = table.ColumnAlignRight
	for _, row := range countryData {
		t.Append(row...)
	}
	t.Print(os.Stdout)
}
Output:

 �[1mnumeric�[0m  �[1malpha-2�[0m  �[1mname�[0m            �[1mcapital�[0m          ​
     004  AF       Afghanistan     Kabul            ​
     248  AX       Åland Islands   Mariehamn        ​
     008  AL       Albania         Tirana           ​
     012  DZ       Algeria         Algiers          ​
     016  AS       American Samoa  Pago Pago        ​
     020  AD       Andorra         Andorra la Vella ​
     024  AO       Angola          Luanda           ​

func NewGHFMD

func NewGHFMD(headers ...string) *Table

NewGHFMD initializes a Table with reasonable defaults for the given headers to produce a table formatted for GitHub-flavoured markdown.

Example
package main

import (
	"os"

	"github.com/chipaca/table"
)

var countryData = [][]interface{}{
	{"004", "AF", "Afghanistan", "Kabul"},
	{"248", "AX", "Åland Islands", "Mariehamn"},
	{"008", "AL", "Albania", "Tirana"},
	{"012", "DZ", "Algeria", "Algiers"},
	{"016", "AS", "American Samoa", "Pago Pago"},
	{"020", "AD", "Andorra", "Andorra la Vella"},
	{"024", "AO", "Angola", "Luanda"},
}

func main() {
	t := table.NewGHFMD("numeric", "alpha-2", "name", "capital")
	t.Outdent = "\u200b" // this is to trick go's example tests' overly aggressive whitespace trimming
	t.Align[0] = table.ColumnAlignRight
	t.Align[2] = table.ColumnAlignCenter
	for _, row := range countryData {
		t.Append(row...)
	}
	t.Print(os.Stdout)
}
Output:

 numeric | alpha-2 |      name      | capital          ​
--------:|---------|:--------------:|------------------​
     004 | AF      |  Afghanistan   | Kabul            ​
     248 | AX      | Åland Islands  | Mariehamn        ​
     008 | AL      |    Albania     | Tirana           ​
     012 | DZ      |    Algeria     | Algiers          ​
     016 | AS      | American Samoa | Pago Pago        ​
     020 | AD      |    Andorra     | Andorra la Vella ​
     024 | AO      |     Angola     | Luanda           ​

func (*Table) Append

func (t *Table) Append(row ...interface{})

Append adds a row to the table's data.

Note it must have the same number of entries as there were headers in the constructor.

Also note the entries can be a string, or a StringAndWidther.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of rows of data in the table.

func (*Table) Print

func (t *Table) Print(out io.Writer)

Print the table.

It attempts to determine the terminal width, falling back to 80 columns if it fails. The last column of data will be truncated so the whole table fits inside this width; note this might not be possible if the non-truncated columns already overflow.

func (*Table) Set

func (t *Table) Set(i, j int, v string)

Set lets you assign a value to a particular cell of the data in the table.

func (*Table) String

func (t *Table) String() string

Jump to

Keyboard shortcuts

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