uitable

package module
Version: v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: MIT Imports: 6 Imported by: 482

README

uitable GoDoc Build Status

uitable is a go library for representing data as tables for terminal applications. It provides primitives for sizing and wrapping columns to improve readability.

Example Usage

Full source code for the example is available at example/main.go

table := uitable.New()
table.MaxColWidth = 50

table.AddRow("NAME", "BIRTHDAY", "BIO")
for _, hacker := range hackers {
  table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio)
}
fmt.Println(table)

Will render the data as:

NAME          BIRTHDAY          BIO
Ada Lovelace  December 10, 1815 Ada was a British mathematician and writer, chi...
Alan Turing   June 23, 1912     Alan was a British pioneering computer scientis...

For wrapping in two columns:

table = uitable.New()
table.MaxColWidth = 80
table.Wrap = true // wrap columns

for _, hacker := range hackers {
  table.AddRow("Name:", hacker.Name)
  table.AddRow("Birthday:", hacker.Birthday)
  table.AddRow("Bio:", hacker.Bio)
  table.AddRow("") // blank
}
fmt.Println(table)

Will render the data as:

Name:     Ada Lovelace
Birthday: December 10, 1815
Bio:      Ada was a British mathematician and writer, chiefly known for her work on
          Charles Babbage's early mechanical general-purpose computer, the Analytical
          Engine

Name:     Alan Turing
Birthday: June 23, 1912
Bio:      Alan was a British pioneering computer scientist, mathematician, logician,
          cryptanalyst and theoretical biologist

Installation

$ go get -v github.com/gosuri/uitable

Bitdeli Badge

Documentation

Overview

Package uitable provides a decorator for formating data as a table

Example
package main

import (
	"fmt"

	"github.com/gosuri/uitable"
)

type hacker struct {
	Name, Birthday, Bio string
}

var hackers = []hacker{
	{"Ada Lovelace", "December 10, 1815", "Ada was a British mathematician and writer, chiefly known for her work on Charles Babbage's early mechanical general-purpose computer, the Analytical Engine"},
	{"Alan Turing", "June 23, 1912", "Alan was a British pioneering computer scientist, mathematician, logician, cryptanalyst and theoretical biologist"},
}

func main() {
	table := uitable.New()
	table.MaxColWidth = 50

	fmt.Println("==> List")
	table.AddRow("NAME", "BIRTHDAY", "BIO")
	for _, hacker := range hackers {
		table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio)
	}
	fmt.Println(table)

	fmt.Print("\n==> Details\n")
	table = uitable.New()
	table.MaxColWidth = 80
	table.Wrap = true
	for _, hacker := range hackers {
		table.AddRow("Name:", hacker.Name)
		table.AddRow("Birthday:", hacker.Birthday)
		table.AddRow("Bio:", hacker.Bio)
		table.AddRow("") // blank
	}
	fmt.Println(table)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Separator = "\t"

Separator is the default column seperator

Functions

This section is empty.

Types

type Cell

type Cell struct {
	// Width is the width of the cell
	Width uint

	// Wrap when true wraps the contents of the cell when the lenght exceeds the width
	Wrap bool

	// RightAlign when true aligns contents to the right
	RightAlign bool

	// Data is the cell data
	Data interface{}
}

Cell represents a column in a row

func (*Cell) LineWidth

func (c *Cell) LineWidth() uint

LineWidth returns the max width of all the lines in a cell

func (*Cell) String

func (c *Cell) String() string

String returns the string formated representation of the cell

type Row

type Row struct {
	// Cells is the group of cell for the row
	Cells []*Cell

	// Separator for tabular columns
	Separator string
}

Row represents a row in a table

func NewRow

func NewRow(data ...interface{}) *Row

NewRow returns a new Row and adds the data to the row

func (*Row) String

func (r *Row) String() string

String returns the string representation of the row

type Table

type Table struct {
	// Rows is the collection of rows in the table
	Rows []*Row

	// MaxColWidth is the maximum allowed width for cells in the table
	MaxColWidth uint

	// Wrap when set to true wraps the contents of the columns when the length exceeds the MaxColWidth
	Wrap bool

	// Separator is the seperator for columns in the table. Default is "\t"
	Separator string
	// contains filtered or unexported fields
}

Table represents a decorator that renders the data in formatted in a table

func New

func New() *Table

New returns a new Table with default values

func (*Table) AddRow

func (t *Table) AddRow(data ...interface{}) *Table

AddRow adds a new row to the table

func (*Table) Bytes

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

Bytes returns the []byte value of table

func (*Table) RightAlign

func (t *Table) RightAlign(col int)

func (*Table) String

func (t *Table) String() string

String returns the string value of table

Source Files

Directories

Path Synopsis
util
strutil
Package strutil provides various utilities for manipulating strings
Package strutil provides various utilities for manipulating strings
wordwrap
Package wordwrap provides methods for wrapping the contents of a string
Package wordwrap provides methods for wrapping the contents of a string

Jump to

Keyboard shortcuts

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