Categorygithub.com/landoop/tableprinter
modulepackage
0.0.0-20201125135848-89e81fc956e7
Repository: https://github.com/landoop/tableprinter.git
Documentation: pkg.go.dev

# README

TablePrinter

tableprinter is an intelligent value-to-table formatter and writer. It uses a customized version of olekukonko/tablewriter to render a result table.

It checks every in data and transforms those data(structure values, slices, maps, single lists that may contain different type of values such as go standard values like int, string even a value that implements the fmt.Stringer interface) to a table formatted text and outputs it to an io.Writer. Like encoding/json#Encode but for tables.

build status report card godoc examples

Installation

The only requirement is the Go Programming Language, at least version 1.10.

$ go get -u github.com/lensesio/tableprinter
package main

import (
    "os"
    "sort"

    "github.com/kataras/tablewriter"
    "github.com/lensesio/tableprinter"
)

type person struct {
    Firstname string `header:"first name"`
    Lastname  string `header:"last name"`
}

func main() {
    printer := tableprinter.New(os.Stdout)
    persons := []person{
        {"Chris", "Doukas"},
        {"Georgios", "Callas"},
        {"Ioannis", "Christou"},
        {"Nikolaos", "Doukas"},
        {"Dimitrios", "Dellis"},
    }

    sort.Slice(persons, func(i, j int) bool {
        return persons[j].Firstname > persons[i].Firstname
    })

    // Optionally, customize the table, import of the underline 'tablewriter' package is required for that.
    printer.BorderTop, printer.BorderBottom, printer.BorderLeft, printer.BorderRight = true, true, true, true
    printer.CenterSeparator = "│"
    printer.ColumnSeparator = "│"
    printer.RowSeparator = "─"
    printer.HeaderBgColor = tablewriter.BgBlackColor
    printer.HeaderFgColor = tablewriter.FgGreenColor

    // Print the slice of structs as table, as shown above.
    printer.Print(persons)
}

Examples

Versioning

Current: v0.0.3

Read more about Semantic Versioning 2.0.0

License

Distributed under Apache Version 2.0, see LICENSE for more information.

# Functions

CanAcceptRow accepts a value of row and a set of filter and returns true if it can be printed, otherwise false.
MakeFilters accept a value of row and generic filters and returns a set of typed `RowFilter`.
New creates and initializes a Printer with the default values based on the "w" target writer.
Print outputs whatever "in" value passed as a table to the "w", filters cna be used to control what rows can be visible or hidden.
PrintHeadList prints whatever "list" as a table to the "w" with a single header.
PrintJSON prints the json-bytes as a table to the "w", filters cna be used to control what rows can be visible or hidden.
RegisterParser sets a parser based on its kind of type.
RemoveStructHeader will dynamically remove a specific header tag from a struct's field based on the "fieldName" which must be a valid exported field of the struct.
Render prints a table based on the rules of this "p" Printer.
SetStructHeader dynamically sets the "newHeaderValue" to a specific struct's field's header tag's value based on the "fieldName" which must be a valid exported field of the struct.
WhichParser returns the available `Parser` for the "typ" type; Slice, Map, Struct...

# Constants

AlignCenter is the center aligment (1).
AlignDefault is the default alignment (0).
AlignLeft is the left aligment (3).
AlignRight is the right aligment (2).
CountHeaderTag usage: List []any `header:"MyList,count"`.
DateHeaderTag usage: Start string `header:"Start,date"`, the field's value should be formatted as time.RFC3339.
DurationHeaderTag usage: Uptime int64 `header:"Uptime,unixduration"`.
ForceTextHeaderTag usage: ID int `header:"ID,text"`.
HeaderTag usage: Field string `header:"Name"`.
InlineHeaderTag usage: Embedded Struct `header:"inline"`.
NumberHeaderTag usage: NumberButString string `header:"Age,number"`.
TimestampAsLocalHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|local)"`.
TimestampAsUTCHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc)"`.
TimestampFormatANSICHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|ANSIC)"`.
TimestampFormatARFC3339NanoHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC3339Nano)"`.
TimestampFormatHumanHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|human)"`.
TimestampFormatRFC1123HeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC1123)"`.
default one.
TimestampFormatRFC3339HeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC3339)"`.
TimestampFormatRFC822HeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC822)"`.
TimestampFormatRFC822ZHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC822Z)"`.
TimestampFormatRFC850HeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RFC850)"`.
TimestampFormatRubyDateHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|RubyDate)"`.
TimestampFormatUnixDateCHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms|utc|UnixDate)"`.
TimestampFromMillisecondsHeaderTag usage: Timestamp int64 `header:"Start,timestamp(ms)"`.
TimestampHeaderTag usage: Timestamp int64 `json:"timestamp" yaml:"Timestamp" header:"At,timestamp(ms|utc|02 Jan 2006 15:04)"`.

# Variables

Default is the default Table Printer.
The built'n type parsers, all except `JSONParser` are directly linked to the `Print/PrintHeadList` functions.
The built'n type parsers, all except `JSONParser` are directly linked to the `Print/PrintHeadList` functions.
The built'n type parsers, all except `JSONParser` are directly linked to the `Print/PrintHeadList` functions.
type is the root struct.
The built'n type parsers, all except `JSONParser` are directly linked to the `Print/PrintHeadList` functions.

# Structs

Printer contains some information about the final table presentation.
StructHeader contains the name of the header extracted from the struct's `HeaderTag` field tag.
TimestampHeaderTagValue the header's value of a "timestamp" header tag functionality.

# Interfaces

Parser should be implemented by all available reflect-based parsers.

# Type aliases

Alignment is the alignment type (int).
RowFilter is the row's filter, accepts the reflect.Value of the custom type, and returns true if the particular row can be included in the final result.