prettytable

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

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

Go to latest
Published: Oct 13, 2014 License: MIT Imports: 9 Imported by: 33

README

go-prettytable

go-prettytable is a library for Golang to build a simple text table with a multibyte, doublewidth character support.

GoDoc

Installation

Install and update this go package with go get -u github.com/tatsushid/go-prettytable

Examples

Import this package and use

tbl, err := prettytable.NewTable([]prettytable.Column{
	{Header: "COL1"},
	{Header: "COL2", MinWidth: 6},
	{Header: "COL3", AlignRight: true},
}...)
if err != nil {
	panic(err)
}
tbl.Separator = " | "
tbl.AddRow("foo", "bar", "baz")
tbl.AddRow(1, 2.3, 4)
tbl.Print()

It outputs

COL1 | COL2   | COL3
foo  | bar    |  baz
1    | 2.3    |    4

Also it can be used with multibyte, doublewidth characters

tbl, err := prettytable.NewTable([]prettytable.Column{
	{Header: "名前"},
	{Header: "個数", AlignRight: true},
}...)
if err != nil {
	panic(err)
}
tbl.Separator = " | "
tbl.AddRow("りんご", 5)
tbl.AddRow("みかん", 3)
tbl.AddRow("柿", 2)
tbl.Print()

It outputs (may not be displayed correctly with proportional fonts but it is displeyed good on terminal)

名前   | 個数
りんご |    5
みかん |    3
柿     |    2

For more detail, please see godoc.

See Also

License

go-prettytable is under MIT License. See the LICENSE file for details.

Documentation

Overview

Package prettytable is a library for Golang to build a simple text table with a multibyte, doublewidth character support.

Example
package main

import (
	"github.com/tatsushid/go-prettytable"
)

func main() {
	tbl, err := prettytable.NewTable([]prettytable.Column{
		{Header: "COL1"},
		{Header: "COL2", MinWidth: 6},
		{Header: "COL3", AlignRight: true},
	}...)
	if err != nil {
		panic(err)
	}
	tbl.Separator = " | "
	tbl.AddRow("foo", "bar", "baz")
	tbl.AddRow(1, 2.3, 4)
	tbl.Print()
}
Output:
COL1 | COL2   | COL3
foo  | bar    |  baz
1    | 2.3    |    4
Example (DoublewidthChars)
package main

import (
	"github.com/tatsushid/go-prettytable"
)

func main() {
	tbl, err := prettytable.NewTable([]prettytable.Column{
		{Header: "名前"},
		{Header: "個数", AlignRight: true},
	}...)
	if err != nil {
		panic(err)
	}
	tbl.Separator = " | "
	tbl.AddRow("りんご", 5)
	tbl.AddRow("みかん", 3)
	tbl.AddRow("柿", 2)
	tbl.Print()
}
Output:
名前   | 個数
りんご |    5
みかん |    3
柿     |    2

Index

Examples

Constants

This section is empty.

Variables

View Source
var Separator = " "

Package wide column separator, used as default when NewTable is called.

Functions

This section is empty.

Types

type Column

type Column struct {
	// Column name used as its header
	Header string
	// If this value is true, the column is aligned to the right.
	AlignRight bool
	// Minimal width of column. If Header or column value's length is
	// larger than it, the column width is extended to the length.
	MinWidth int
	// Maximal width of column. If Header or column value's length is
	// larger than it, the header or value is truncated
	MaxWidth int
	// contains filtered or unexported fields
}

Column is used for column definitions of a table

type Table

type Table struct {
	// If this value is true, a header line isn't generated
	NoHeader bool
	// Column separator characters. Separator package value is default
	Separator string
	// contains filtered or unexported fields
}

Table represents a table itself and has its option values

func NewTable

func NewTable(cols ...Column) (*Table, error)

NewTable defines a table with columns and returns *Table. It returns an error if no columns are passed or passed invalid columns, for example, MinWidth is larger than MaxWidth

func (*Table) AddRow

func (t *Table) AddRow(vars ...interface{}) error

AddRow adds a row with given values. It returns an error if no values are passed or a number of values is larger than a number of columns.

It converts values into strings by following rules

  • If a value fulfills fmt.Stringer interface, it is converted into its String() function result
  • If a value is an integer or a float, it is converted into a decimal number string.
  • If a value is a bool, it is converted into "true" or "false" string
  • If a value is a string, it is used as is.
  • If a value is a []byte or []rune, it is converted int string
  • Otherwise, an error is returned

func (*Table) Bytes

func (t *Table) Bytes() []byte

Bytes returns a generated text table []byte

func (*Table) Print

func (t *Table) Print() (n int, err error)

Print prints a generated text table to os.Stdout. It returns the number of bytes written. Any errors encountered during the write is also returned.

func (*Table) String

func (t *Table) String() string

String returns a generated text table string

func (*Table) WriteTo

func (t *Table) WriteTo(w io.Writer) (int64, error)

WriteTo writes a generated text table to a writer. It returns the number of bytes written. Any errors encountered during the write is also returned.

Jump to

Keyboard shortcuts

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