gotable

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 4 Imported by: 0

README

GoTable

Create terminal tables easily with different padding, built-in styles, and more!

Go Reference License

Information.

The table calculates true value, so you can include UTF-8 characters inside of the table without worrying about it being deformed. The same applies to ANSI escape codes.

There are three different padding options;

  • gotable.PaddingLeft - All padding will be applied after the text.
  • gotable.PaddingRight - There is padding before the text, then the text is rendered.
  • gotable.PaddingCenter - The text is centered inside of the column in the table.

There are also four built in styles;

  • gotable.StyleMarkdown - A MySQL-style table.
  • gotable.StyleRounded - A regular style that has rounded corners.
  • gotable.StyleUnicode - A unicode table style.`
  • gotable.StyleMinimal - A minimal table style with no borders.`
    • Remember, you can create your own customised styles using gotable.Style.

Creating a table.

package main

import (
	"fmt"
	"strconv"

	"github.com/mattyco/gotable"
)

type value struct {
	name        string
	description string
}

var values []*value = []*value{
	{
		name:        "Red",
		description: "This is a colour.",
	},

	{
		name:        "Dog",
		description: "A mans best friend.",
	},

	{
		name:        "SOAD",
		description: "System Of A down.",
	},
}

func main() {
	table := gotable.New()

	table.Header = append(table.Header, &gotable.Cell{Value: "#", Padding: gotable.PaddingCenter})

	table.Header = append(table.Header, &gotable.Cell{Value: "Name", Padding: gotable.PaddingLeft})

	table.Header = append(table.Header, &gotable.Cell{Value: "Description", Padding: gotable.PaddingCenter})

	table.SetStyle(gotable.StyleMinimal)

	table.Body = make([][]*gotable.Cell, 3)

	for i, value := range values {
		table.Body[0] = append(table.Body[0], &gotable.Cell{Value: strconv.Itoa(i+1), Padding: gotable.PaddingLeft})
		table.Body[1] = append(table.Body[1], &gotable.Cell{Value: value.name, Padding: gotable.PaddingLeft})
		table.Body[2] = append(table.Body[2], &gotable.Cell{Value: value.description, Padding: gotable.PaddingLeft})
	}

	fmt.Println(table.String())
}

}

Output.

 #   Name       Description     
--- ------ ---------------------
 1   Red    This is a colour.   
 2   Dog    A mans best friend. 
 3   SOAD   System Of A down.   

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StyleMarkdown uses ASCII characters.
	StyleMarkdown = &Style{
		Border: &Border{
			Top:                "-",
			Left:               "|",
			Right:              "|",
			Bottom:             "-",
			TopLeft:            "+",
			TopRight:           "+",
			BottomLeft:         "+",
			BottomRight:        "+",
			TopIntersection:    "+",
			BottomIntersection: "+",
		},

		Divider: &Divider{
			Left:         "+",
			Right:        "+",
			Intersection: "+",
			Center:       "-",
		},

		Left:         "|",
		Right:        "|",
		CellSplitter: "|",
	}

	// StyleRounded uses rounded outer corners.
	StyleRounded = &Style{
		Border: &Border{
			Top:                "─",
			Left:               "│",
			Right:              "│",
			Bottom:             "─",
			TopLeft:            "╭",
			TopRight:           "╮",
			BottomLeft:         "╰",
			BottomRight:        "╯",
			TopIntersection:    "┬",
			BottomIntersection: "┴",
		},

		Divider: &Divider{
			Left:         "├",
			Right:        "┤",
			Intersection: "┼",
			Center:       "─",
		},

		Left:         "│",
		Right:        "│",
		CellSplitter: "│",
	}

	// StyleUnicode uses unicode characters for the table style.
	StyleUnicode = &Style{
		Border: &Border{
			Top:                "═",
			Left:               "║",
			Right:              "║",
			Bottom:             "═",
			TopLeft:            "╔",
			TopRight:           "╗",
			BottomLeft:         "╚",
			BottomRight:        "╝",
			TopIntersection:    "╤",
			BottomIntersection: "╧",
		},

		Divider: &Divider{
			Left:         "╟",
			Right:        "╢",
			Intersection: "┼",
			Center:       "━",
		},

		Left:         "║",
		Right:        "║",
		CellSplitter: "│",
	}

	// StyleMinimal has no outer borders.
	StyleMinimal = &Style{
		Border: nil,
		Divider: &Divider{
			Left:         "",
			Right:        "",
			Intersection: " ",
			Center:       "-",
		},

		Left:         "",
		Right:        "",
		CellSplitter: " ",
	}
)

Functions

This section is empty.

Types

type Border

type Border struct {
	Top                string
	Left               string
	Right              string
	Bottom             string
	TopLeft            string
	TopRight           string
	BottomLeft         string
	BottomRight        string
	TopIntersection    string
	BottomIntersection string
}

Border defines the characters used for the borders of the table.

type Cell

type Cell struct {
	Value   string
	Padding Padding
}

Cell represents a single cell in a table, containing a value and padding information.

type Divider

type Divider struct {
	Left         string
	Right        string
	Intersection string
	Center       string
}

Divider defines the characters used for dividers in the table.

type Padding

type Padding int

Padding represents the different padding methods.

const (
	PaddingLeft Padding = iota
	PaddingRight
	PaddingCenter
)

type Style

type Style struct {
	Border       *Border
	Divider      *Divider
	Left         string
	Right        string
	CellSplitter string
}

Style defines the style of the table, including borders, dividers, and cell splitters.

type Table

type Table struct {
	Header []*Cell
	Body   [][]*Cell
	// contains filtered or unexported fields
}

func New

func New() *Table

New creates a new table object.

func (*Table) SetStyle

func (t *Table) SetStyle(style *Style)

SetStyle sets the style of the table.

func (*Table) String

func (t *Table) String() string

String returns the table in a string format.

Jump to

Keyboard shortcuts

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