table

package
v4.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2017 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package table formats an ASCII/ANSI table with dynamic column widths.

Index

Constants

View Source
const (
	// The cell has been padded
	Padded = 1 << iota

	// The cell has been truncated
	Truncated
)

Variables

This section is empty.

Functions

func Fmt

func Fmt(fmt string, rows [][]string) string

Fmt formats the given rows into a pretty table and returns the string ready to be printed. The fmt parameter is a string of "l" and "r" characters indicating the requested alignment of each column. Example:

rows := [][]string{{"NAME", "VALUE"}, {"Test", "4.5"}, {"Other", "13.2"}}
tab := table.Fmt("lr", rows)
fmt.Println(tab)

func FmtFunc

func FmtFunc(fmt string, rows [][]string, fn Formatter) string

FmtFunc works as Fmt but calls the formatting function fn on each cell before printing.

Types

type Formatter

type Formatter func(cell string, row int, col int, flags int) string

A Formatter is called to apply string formatting such as ANSI escape sequences just before printing a cell. The parameters are the cell text (after padding or truncation), the row and column index (zero based) and a set of flags. possible flags are Padded (the cell has been space padded on the right or left) and Truncated (the cell has been truncated on the right). The returned string must have unchanged (visible) length.

Notes

Bugs

  • When there are multibyte UTF-8 characters we cut the string shorter than necessary.

Jump to

Keyboard shortcuts

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