termtable

package module
v0.0.0-...-09d29f3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2015 License: MIT Imports: 4 Imported by: 18

README

termtable

A Go library to easily generate table in you CLI

Install

$ go get github.com/stevedomin/termtable

Usage

Print a simple table without separators:

package main

import(
    "fmt"
    "github.com/stevedomin/termtable"
)

func main() {
    t := termtable.NewTable(nil, nil)
    t.SetHeader([]string{"LOWERCASE", "UPPERCASE", "NUMBERS"})
    t.AddRow([]string{"abc", "ABCD", "12345"})
    t.AddRow([]string{"defg", "EFGHI", "678"})
    t.AddRow([]string{"hijkl", "JKL", "9000"})
    fmt.Println(t.Render())

    // Output :
    // LOWERCASE UPPERCASE NUMBERS
    // abc       ABCD      12345
    // defg      EFGHI     678
    // hijkl     JKL       9000
}

Print a simple table with separators and custom padding (alt syntax):

package main

import(
    "github.com/stevedomin/termtable"
)

func main() {
    rows := [][]string{
      []string{"abc", "ABCD", "12345"},
      []string{"defg", "EFGHI", "678"},
      []string{"hijkl", "JKL", "9000"},
    }
    t := termtable.NewTable(rows, &termtable.TableOptions{
     Padding: 3,
     UseSeparator: true,
    })
    fmt.Println(t.Render())

    // Output (vertical bars look better in terminal):
    // +---------------+---------------+-------------+
    // |   LOWERCASE   |   UPPERCASE   |   NUMBERS   |
    // +---------------+---------------+-------------+
    // |   abc         |   ABCD        |   12345     |
    // |   defg        |   EFGHI       |   678       |
    // |   hijkl       |   JKL         |   9000      |
    // +---------------+---------------+-------------+
}

Development

To run test:

$ go test

To run benchmarks:

$ go test -bench . -benchmem

Todo

  • Column align
  • Cell align
  • Support more types: int, float, bool
  • Rows separator
  • Multiline cell
  • Custom column width

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Table

type Table struct {
	Rows    [][]string
	Columns [][]string
	Options *TableOptions

	HasHeader bool
	// contains filtered or unexported fields
}

func NewTable

func NewTable(rows [][]string, options *TableOptions) *Table

func (*Table) AddRow

func (t *Table) AddRow(row []string)

func (*Table) Render

func (t *Table) Render() string

func (*Table) SetHeader

func (t *Table) SetHeader(header []string)

type TableOptions

type TableOptions struct {
	Padding      int
	UseSeparator bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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