table

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

CodeQL Run Gosec Unit Test Release GitHub go.mod Go version license codecov release Go Reference

go-table is a simple Table implementation in golang.

This is focused on usability and simplicity.

Overview

Taking advantage of the text/tabwriter package, this library provides a simple way to create and manage tables.

Documentation

Official documentation is available on pkg.go.dev -> slashdevops/go-table

Installing

Latest release:

go get -u github.com/slashdevops/go-table@latest

Specific release:

go get -u github.com/slashdevops/go-table@vx.y.z

Adding it to your project:

import "github.com/slashdevops/go-table"

Examples

EXAMPLE 1: This example use the Builder to create a table with a header and rows before building it. Here you can see how to mix the Builder with the Table native methods. Also is used the Debug flag to show the table with the Debug format (| symbol) at the end of each row.

package main

import (
  "fmt"

  "github.com/slashdevops/go-table"
)

func main() {
  out := new(bytes.Buffer)
  t := NewBuilder(out).
   WithFlags(Debug)

   // add header and rows before building
  t.WithHeader([]string{"Name", "Age", "City"})
  t.WithRow([]string{"John", "30", "New York"}...)
  t.WithRow([]string{"Jane", "20", "London"}...)
  t.WithRow([]string{"Jack", "40", "Paris"}...)
  t.WithRow([]string{"Christian", "47", "Barcelona"}...)

  // build the table
  table := t.Build()

  // add a new row after building
  table.AddRow([]string{"Ely", "50", "Barcelona"}...)

  // render the table
  table.Render()

  fmt.Println(out.String())
}

Output:

Name       |Age  |City
John       |30   |New York
Jane       |20   |London
Jack       |40   |Paris
Christian  |47   |Barcelona
Ely        |50   |Barcelona

EXAMPLE 2: Show the same table with a different separator (,) CSV style

package main

import (
  "fmt"

  "github.com/slashdevops/go-table"
)

func main() {
  buf := new(bytes.Buffer)
  t := New(buf, WithSep(","))
  t.SetHeader([]string{"Name", "Age", "City"})
  t.AddRow([]string{"John", "30", "New York"}...)
  t.AddRow([]string{"Jane", "20", "London"}...)
  t.AddRow([]string{"Jack", "40", "Paris"}...)

  // render the table into the buffer
  t.Render()

 fmt.Println(buf.String())
}

Output:

Name,Age,City
John,30,New York
Jane,20,London
Jack,40,Paris

License

go-table is released under the BSD 3-Clause License. See the bundled LICENSE file for details.

Documentation

Overview

Package table provides a simple table builder with a fluent API.

Index

Examples

Constants

View Source
const (
	// Ignore html tags and treat entities (starting with '&'
	// and ending in ';') as single characters (width = 1).
	FilterHTML uint = 1 << iota

	// Strip Escape characters bracketing escaped text segments
	// instead of passing them through unchanged with the text.
	StripEscape

	// Force right-alignment of cell content.
	// Default is left-alignment.
	AlignRight

	// Handle empty columns as if they were not present in
	// the input in the first place.
	DiscardEmptyColumns

	// Always use tabs for indentation columns (i.e., padding of
	// leading empty cells on the left) independent of padchar.
	TabIndent

	// Print a vertical bar ('|') between columns (after formatting).
	// Discarded columns appear as zero-width columns ("||").
	Debug
)
View Source
const (

	// this is the default header name used when a simple go value is passed to the table
	UnknownHeaderName = "Unknown"
)

Variables

This section is empty.

Functions

func NewBuilder

func NewBuilder(output io.Writer) *tableBuilderChoice

NewBuilder creates a new table builder choice to create a table

Types

type Table

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

Table is a simple abstraction for tex/tabwriter golang package

Example (Builder)

ExampleTable_builder shows how tou use the builder pattern and regular methods

buf := new(bytes.Buffer)
t := NewBuilder(buf).
	WithMinWidth(10).
	WithTabWidth(10).
	WithPadding(10).
	WithPadChar('.').
	WithFlags(AlignRight | Debug).
	WithSep("\t").
	WithHeader([]string{"Name", "Age", "City"}).
	WithRow([]string{"John", "30", "New York"}...).
	WithRow([]string{"Jane", "20", "London"}...).
	Build()

t.AddRow([]string{"Jack", "40", "Paris"}...)

t.Render()

fmt.Println(buf.String())
Output:

..........Name|..........Age|City
..........John|...........30|New York
..........Jane|...........20|London
..........Jack|...........40|Paris
Example (Builder2)

ExampleTable_builder2 shows how tou use the builder pattern and regular methods in different order and code style

out := new(bytes.Buffer)
t := NewBuilder(out).
	WithFlags(Debug)

	// add header and rows before building
t.WithHeader([]string{"Name", "Age", "City"})
t.WithRow([]string{"John", "30", "New York"}...)
t.WithRow([]string{"Jane", "20", "London"}...)
t.WithRow([]string{"Jack", "40", "Paris"}...)
t.WithRow([]string{"Christian", "47", "Barcelona"}...)

// build the table
table := t.Build()

// add a new row after building
table.AddRow([]string{"Ely", "50", "Barcelona"}...)

// render the table
table.Render()

fmt.Println(out.String())
Output:

Name       |Age  |City
John       |30   |New York
Jane       |20   |London
Jack       |40   |Paris
Christian  |47   |Barcelona
Ely        |50   |Barcelona

func New

func New(output io.Writer, opts ...TableOption) *Table

New creates a new table

Example

ExampleNew shows how to create a new table

buf := new(bytes.Buffer)
t := New(buf)
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]string{"John", "30", "New York"}...)
t.AddRow([]string{"Jane", "20", "London"}...)
t.AddRow([]string{"Jack", "40", "Paris"}...)
t.Render()

fmt.Println(buf.String())
Output:

Name  Age  City
John  30   New York
Jane  20   London
Jack  40   Paris
Example (WithOptions)

ExampleNew shows how to create a new table with options

buf := new(bytes.Buffer)
t := New(
	buf,
	WithMinWidth(10),
	WithTabWidth(10),
	WithPadding(10),
	WithPadChar('.'),
	WithFlags(AlignRight|Debug),
	WithSep("\t"),
)
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]string{"John", "30", "New York"}...)
t.AddRow([]string{"Jane", "20", "London"}...)
t.AddRow([]string{"Jack", "40", "Paris"}...)
t.Render()

fmt.Println(buf.String())
Output:

..........Name|..........Age|City
..........John|...........30|New York
..........Jane|...........20|London
..........Jack|...........40|Paris
Example (WithOptions_csv)

ExampleNew_withOptions_csv shows how to output a csv

buf := new(bytes.Buffer)
t := New(buf, WithSep(","))
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]string{"John", "30", "New York"}...)
t.AddRow([]string{"Jane", "20", "London"}...)
t.AddRow([]string{"Jack", "40", "Paris"}...)
t.Render()

fmt.Println(buf.String())
Output:

Name,Age,City
John,30,New York
Jane,20,London
Jack,40,Paris

func (*Table) AddRow

func (t *Table) AddRow(row ...string)

AddRow adds a row to the table

func (*Table) AddRowf

func (t *Table) AddRowf(format string, a ...any)

AddRowf adds a row to the table using a format string

func (*Table) AddRows

func (t *Table) AddRows(rows [][]string)

AddRows adds multiple rows to the table, matrix style

func (*Table) AddRowsf

func (t *Table) AddRowsf(format string, a ...any)

AddRowsf adds multiple rows to the table, matrix style, using a format string

func (*Table) Render

func (t *Table) Render() error

Render renders the table into the output

func (*Table) SetHeader

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

SetHeader sets the header of the table

Example (NoCall)

ExampleTable_SetHeader_noCall show the default behavior when no header is set

buf := new(bytes.Buffer)
t := New(buf)
// t.SetHeader([]string{"Name", "Age", "City"}) // with no headers, show Unknown[1,2,3...]
t.AddRow([]string{"John", "30", "New York"}...)
t.AddRow([]string{"Jane", "20", "London"}...)
t.AddRow([]string{"Jack", "40", "Paris"}...)
t.Render()

fmt.Println(buf.String())
Output:

Unknown1  Unknown2  Unknown3
John      30        New York
Jane      20        London
Jack      40        Paris

type TableOption

type TableOption func(*tableOptions)

TableOption is a function that configures a table

func WithFlags

func WithFlags(flags uint) TableOption

WithFlags sets the flags of the table

func WithMinWidth

func WithMinWidth(minWidth int) TableOption

WithMinWidth sets the minimum width of the table

func WithPadChar

func WithPadChar(padChar byte) TableOption

WithPadChar sets the pad char of the table

func WithPadding

func WithPadding(padding int) TableOption

WithPadding sets the padding of the table

func WithSep

func WithSep(sep string) TableOption

WithSep sets the separator of the table

func WithTabWidth

func WithTabWidth(tabWidth int) TableOption

WithTabWidth sets the tab width of the table

Jump to

Keyboard shortcuts

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