dyntab

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

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

Go to latest
Published: Dec 2, 2017 License: MIT Imports: 6 Imported by: 0

README

DYNamicTABles

Build Status GoDoc Go Report Card

Dynamic table generation for golang using https://github.com/olekukonko/tablewriter

The package uses an Table struct to hold main data.

To create a new table table := dyntab.NewTable() To add recursion into types table.Recurse([]reflect.Type{{reflect.TypeOf(time.Now())}}) To add data table.SetData(data) To set specialized to String methods table.Specialize([]dyntab.ToSpecialize{time.Location{}, func(i interface{}) (string, error) {return "hey"}}) To print the table table.PrintTo(os.Stdout)

The most simple usage is err := dyntab.NewTable().SetData(data).PrintTo(os.Stdout)

The data can be a struct or slice of structs, as well as a slice of reflect.types that it should recurse into. The output will be a cli table.

It uses struct tags tab:"name" to have personalized headers for the fields of the table (tab:"-" to ignore a field).

If you want to override any of the Header, Body or Footer implementation you need to implement the functions Header() ([]string, error), Body() ([][]string, error) or Footer() ([]string, error).

Example in the godoc references.

Documentation

Overview

Package dyntab creates dynamic tables for structs or slices of structs

Example
package main

import (
	"errors"
	"github.com/SimonSchneider/dyntab"
	"os"
	"reflect"
	"strconv"
	"time"
)

type (
	MyTime struct{ time.Time }
	info   struct {
		name        string
		secret      string `tab:"-"`
		description string `tab:"desc"`
	}
	container struct {
		info
		number int64 `tab:"number of smth"`
		T      MyTime
		Loc    time.Location `tab:"location"`
	}
	containers []container
)

func (t MyTime) MarshalText() (text []byte, err error) {
	return []byte(t.Format("2006-01-02")), nil
}

func Loc2String(i interface{}) (string, error) {
	l, ok := i.(time.Location)
	if ok {
		return "in: " + l.String(), nil
	}
	return "", errors.New("not time.location")
}

func (c containers) Footer() ([]string, error) {
	sum := int64(0)
	for _, n := range c {
		sum += n.number
	}
	return []string{"", "Total", strconv.Itoa(int(sum)), "", " "}, nil
}

func main() {
	cont := containers{
		container{
			info: info{
				name:        "hello",
				secret:      "pretty",
				description: "world",
			},
			number: int64(2),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
		container{
			info: info{
				name:        "good",
				secret:      "sweet",
				description: "bye",
			},
			number: int64(4),
			T:      MyTime{time.Unix(1355270400, 0)},
			Loc:    *time.UTC,
		},
	}

	dyntab.NewTable().
		SetData(cont).
		Recurse([]reflect.Type{
			reflect.TypeOf(info{}),
			reflect.TypeOf(container{})}).
		Specialize([]dyntab.ToSpecialize{{
			Type:     reflect.TypeOf(time.Location{}),
			ToString: Loc2String}}).
		PrintTo(os.Stdout)
}
Output:

+-------+-------+----------------+------------+----------+
| NAME  | DESC  | NUMBER OF SMTH |     T      | LOCATION |
+-------+-------+----------------+------------+----------+
| hello | world |              2 | 2012-12-12 | in: UTC  |
| good  | bye   |              4 | 2012-12-12 | in: UTC  |
+-------+-------+----------------+------------+----------+
|         TOTAL |       6        |                       |
+-------+-------+----------------+------------+----------+

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TabBody

type TabBody interface {
	Body() ([][]string, error)
}

TabBody interface can be implemented to override the default body creation

type TabFooter

type TabFooter interface {
	Footer() ([]string, error)
}

TabFooter interface can be implemented to override the default footer creation

type TabHeader

type TabHeader interface {
	Header() ([]string, error)
}

TabHeader interface can be implemented to override the default header creation

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table holds the main table data

func NewTable

func NewTable() *Table

NewTable returns a new table ready for printing

func (Table) PrintTo

func (t Table) PrintTo(w io.Writer) (err error)

PrintTo prints the table to the io.Writer

func (*Table) Recurse

func (t *Table) Recurse(r []reflect.Type) *Table

Recurse sets the reflect.Types, Recurse slice is required so it's possible to determine what structs to print as a column and which to recurse into,

func (*Table) SetData

func (t *Table) SetData(i interface{}) *Table

SetData sets the data of the table

func (*Table) Specialize

func (t *Table) Specialize(s []ToSpecialize) *Table

Specialize sets the toSpecialize types to the specialize

type ToSpecialize

type ToSpecialize struct {
	Type     reflect.Type
	ToString func(interface{}) (string, error)
}

ToSpecialize is struct for holding to string specializations

Jump to

Keyboard shortcuts

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