table

package module
v0.0.0-...-a92f01c Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 7 Imported by: 1

README

table

Table is a Go module providing simple table formatting functions for command line applications.

GoDoc Go Report

installation

go get -u github.com/jayloop/table

usage

t := table.New("key", "value")
t.FormatHeader(table.Format(table.HiYellow, table.Bold))
t.Precision(2, 1)
t.Row("a", 1)
t.Row("b", 2.0)
t.Row("c", 0.001)
t.Sort(1)
t.Print(os.Stdout)

Documentation

Overview

Package table provides simple table formatting functions for command line applications

Example
package main

import (
	"os"

	"github.com/jayloop/table"
)

func main() {
	t := table.New("key", "value")
	t.Precision(2, 1)
	t.Row("a", 1)
	t.Row("b", 2.0)
	t.Row("c", 0.001)
	t.Sort(1)
	t.Print(os.Stdout)
}
Output:

key  value
c    0.00
a    1
b    2.00

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHeaderFormat

func DefaultHeaderFormat(f FormatFunc)

DefaultHeaderFormat sets the default format to use for all new tables.

Types

type CodeANSI

type CodeANSI int

CodeANSI is an ANSI escape code attribute

const (
	Black CodeANSI = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

ANSI colors

const (
	HiBlack CodeANSI = iota + 90
	HiRed
	HiGreen
	HiYellow
	HiBlue
	HiMagenta
	HiCyan
	HiWhite
)

Bright ANSI colors

const (
	Reset CodeANSI = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	Reverse
	Conceal
	CrossedOut
)

ANSI decorations, most not widely supported

func Background

func Background(c CodeANSI) CodeANSI

Background converts a foreground color value to it's corresponding background value

type FormatFunc

type FormatFunc func(string) string

FormatFunc is a user defined function applying formatting to a header or row value. The typical usecase is setting colors by adding escape characters. A format function should not change the printed length of the value.

func Format

func Format(attr ...CodeANSI) FormatFunc

Format returns a formating function applying ANSI escape codes for the given attributes. Please note that different terminals may support some or none of the colors and decorations.

type Table

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

A Table record stores all table data and formatting options. It is not safe for concurrent use.

func New

func New(headers ...string) *Table

New creates a new table with the given headers. The number of headers decides the number of columns of the table.

func (*Table) AddStruct

func (t *Table) AddStruct(m interface{})

AddStruct adds 2-column rows to a table by iterating over struct fields. The table is created by a previous call to New:

table.New("key", "value")

func (*Table) FormatCols

func (t *Table) FormatCols(fn FormatFunc, cols ...int)

FormatCols adds a format function for the listed column indexes.

func (*Table) FormatHeader

func (t *Table) FormatHeader(fn FormatFunc)

FormatHeader sets to format applied to column headers when printing

func (*Table) FormatNotZero

func (t *Table) FormatNotZero(fn FormatFunc, cols ...int)

FormatNotZero adds a format function applied on values != "0"

func (*Table) FormatRows

func (t *Table) FormatRows(fn FormatFunc, rows ...int)

FormatRows adds a format function for the listed rows indexes. Use row index -1 to denote the last row.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of rows

func (*Table) Less

func (t *Table) Less(i, j int) bool

Less compares row i against row j

func (*Table) MaxWidth

func (t *Table) MaxWidth(chars int, cols ...int)

MaxWidth sets the max width in characters for the listed column indexes.

func (*Table) Padding

func (t *Table) Padding(p int)

Padding sets the number of whitespaces added as padding between columns.

func (*Table) Precision

func (t *Table) Precision(digits int, cols ...int)

Precision sets the number of digits to include when printing float values. It must be set before adding the rows.

func (*Table) Print

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

Print prints the table headers and rows to a io.Writer. Any error returned is from the underlying io.Writer.

func (*Table) Row

func (t *Table) Row(values ...interface{})

Row adds row data.

func (*Table) Sort

func (t *Table) Sort(cols ...int)

Sort sort the table rows by the listed columns

func (*Table) Swap

func (t *Table) Swap(i, j int)

Swap swaps row i and j

Jump to

Keyboard shortcuts

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