README

Table

Go codecov GoDoc Go Report Card

Print out tabular data on the command line using the ansi color esacape codes. Support for writing the ouput based on the fields in a struct and for defining and creating the table manully using the underlying object.

Support for colors on windows can be don using mattn/go-colorable to make a io.Writer that will work.

Usage

For creating a table yourself using the struct.

	tab := table.Table{
		Headers: []string{"something", "another"},
		Rows: [][]string{
			{"1", "2"},
			{"3", "4"},
			{"3", "a longer piece of text that should stretch"},
			{"but this one is longer", "shorter now"},
		},
	}
	err := tab.WriteTable(w, nil) // w is any io.Writer

With a struct slice

	data := []struct {
		Name     string `table:"THE NAME"`
		Location string `table:"THE LOCATION"`
	}{
		{"name", "l"},
		{"namgfcxe", "asfdad"},
		{"namr3e", "l134151dsa"},
		{"namear", "lasd2135"},
	}

    err := table.MarshalTo(w, data, nil) // writes to any w = io.Writer
    buf, err := table.Marshal(data, nil) // also supports return the bytes

The nil parameter is the configuration for the table, this can be set manually, but if its left as nil the deafult config settings will be used.

type Config struct {
	ShowIndex       bool     // shows the index/row number as the first column
	Color           bool     // use the color codes in the output
	AlternateColors bool     // alternate the colors when writing
	TitleColorCode  string   // the ansi code for the title row
	AltColorCodes   []string // the ansi codes to alternate between
}

Installation

Go makes this part easy.

$ go get github.com/mattn/go-colorable

License

MIT

Author

Tom Lazar

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(arr interface{}, c *Config) ([]byte, error)

    Marshal the slince into a table format using reflection

    func MarshalTo

    func MarshalTo(w io.Writer, arr interface{}, c *Config) error

      MarshalTo writes the reflected table into the passed in io.Writer

      Types

      type Config

      type Config struct {
      	ShowIndex       bool     // shows the index/row number as the first column
      	Color           bool     // use the color codes in the output
      	AlternateColors bool     // alternate the colors when writing
      	TitleColorCode  string   // the ansi code for the title row
      	AltColorCodes   []string // the ansi codes to alternate between
      }

        Config is the

        func DefaultConfig

        func DefaultConfig() *Config

          DefaultConfig returns the default config for table, if its ever left null in a method this will be the one used to display the table

          type Table

          type Table struct {
          	Headers []string
          	Rows    [][]string
          }

            Table is the struct used to define the structure, this can be used from a zero state, or inferred using the reflection based methods

            func (Table) WriteTable

            func (t Table) WriteTable(w io.Writer, c *Config) error

              WriteTable writes the defined table to the writer passed in