simpletable

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 4 Imported by: 318 modules, 332 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Simple tables in terminal with Go

simpletable banner

Quality gates Coverage Status Go Reference License

Build readable terminal tables in Go with seven built-in styles, multiline cells, alignment, and column spanning.

Comparison

✅ documented · ⚠️ partial · ❌ not found in sources checked. The last status means the sources checked did not document the feature; it does not establish that the library lacks it. Each symbol links to the public source checked for that comparison.

Feature simpletable termtables uitable tablewriter go-pretty
Multiline cell content ✅ ❌ ⚠️ ⚠️ ❌
Explicit column spans ✅ ✅ ❌ ⚠️ ⚠️
Markdown output ✅ ✅ ❌ ✅ ✅
Alignment controls ✅ ✅ ⚠️ ✅ ✅
Custom table styles ✅ ✅ ❌ ✅ ✅

Sources checked on 2026-11-09. Add a library as a column or a feature as a row, and link each status to the public source checked.

Quick start

package main

import (
	"fmt"
	"strings"

	"github.com/alexeyco/simpletable"
)

func main() {
	table := simpletable.New()
	table.Header = &simpletable.Header{
		Cells: []*simpletable.Cell{
			{Align: simpletable.AlignCenter, Text: "#"},
			{Align: simpletable.AlignCenter, Text: "NAME"},
			{Align: simpletable.AlignCenter, Text: "TAX"},
		},
	}
	rows := []struct {
		name string
		tax  float64
	}{
		{"Newton G. Goetz", 532.70},
		{"Rebecca R. Edney", 1423.25},
		{"John R. Jackson", 7526.12},
		{"Ron J. Gomes", 123.84},
		{"Penny R. Lewis", 3221.11},
	}

	total := 0.0
	for i, row := range rows {
		table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
			{Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", i+1)},
			{Text: row.name},
			{Align: simpletable.AlignRight, Text: fmt.Sprintf("$ %.2f", row.tax)},
		})

		total += row.tax
	}
	table.Footer = &simpletable.Footer{
		Cells: []*simpletable.Cell{
			{Align: simpletable.AlignRight, Span: 2, Text: "Subtotal"},
			{Align: simpletable.AlignRight, Text: fmt.Sprintf("$ %.2f", total)},
		},
	}
	table.SetStyle(simpletable.StyleCompactLite)

	for _, line := range strings.Split(table.String(), "\n") {
		fmt.Println(strings.TrimPrefix(strings.TrimRight(line, " "), " "))
	}
}
#         NAME            TAX
--- ------------------ ------------
1   Newton G. Goetz      $ 532.70
2   Rebecca R. Edney    $ 1423.25
3   John R. Jackson     $ 7526.12
4   Ron J. Gomes         $ 123.84
5   Penny R. Lewis      $ 3221.11
--- ------------------ ------------
            Subtotal   $ 12827.02

Install with go get github.com/alexeyco/simpletable.

See the styles demo, multiline example, and column-span example for more.

Benchmarks

(MacBook)

Run these benchmarks using the benchmark instructions.

BenchmarkSimpletable-12             31845             37586 ns/op           34903 B/op        760 allocs/op
BenchmarkTermtables-12              34851             34191 ns/op           24192 B/op        755 allocs/op
BenchmarkUITable-12                  6452            176884 ns/op           55480 B/op       3070 allocs/op
PASS
ok      github.com/alexeyco/simpletable/_example/03-benchmarks-with-others/bench        5.172s

License

MIT

Documentation

Overview

Package simpletable allows to generate and display ascii tables in the terminal, f.e.:

+----+------------------+--------------+-----------------------------+------+
| #  |       NAME       |    PHONE     |            EMAIL            | QTTY |
+----+------------------+--------------+-----------------------------+------+
|  1 | Newton G. Goetz  | 252-585-5166 | NewtonGGoetz@dayrep.com     |   10 |
|  2 | Rebecca R. Edney | 865-475-4171 | RebeccaREdney@armyspy.com   |   12 |
|  3 | John R. Jackson  | 810-325-1417 | JohnRJackson@armyspy.com    |   15 |
|  4 | Ron J. Gomes     | 217-450-8568 | RonJGomes@rhyta.com         |   25 |
|  5 | Penny R. Lewis   | 870-794-1666 | PennyRLewis@rhyta.com       |    5 |
|  6 | Sofia J. Smith   | 770-333-7379 | SofiaJSmith@armyspy.com     |    3 |
|  7 | Karlene D. Owen  | 231-242-4157 | KarleneDOwen@jourrapide.com |   12 |
|  8 | Daniel L. Love   | 978-210-4178 | DanielLLove@rhyta.com       |   44 |
|  9 | Julie T. Dial    | 719-966-5354 | JulieTDial@jourrapide.com   |    8 |
| 10 | Juan J. Kennedy  | 908-910-8893 | JuanJKennedy@dayrep.com     |   16 |
+----+------------------+--------------+-----------------------------+------+
|                                                           Subtotal |  150 |
+----+------------------+--------------+-----------------------------+------+

There are the following key features:

* Declarative style. Have to write more code, and hell with it.

* Styling. With 6 predefined styles: MySql-like (default), compact, compact lite, markdown, rounded and unicode. And you can change it.

* Header and footer. Separeted from table body.

* Multiline cells support. See https://github.com/alexeyco/simpletable/blob/master/_example/04-multiline/main.go for example.

* cellInterface content alignment. Left, right or center.

* Row spanning. By analogy with the way it is done in HTML. See Cell.Span attribute description.

* Fast! Really fast, see https://github.com/alexeyco/simpletable/blob/master/_example/03-benchmarks-with-others

Examples: https://github.com/alexeyco/simpletable/tree/master/_example

Index

Constants

View Source
const (
	// AlignLeft sets cell left alignment (default).
	AlignLeft = 0

	// AlignCenter sets cell center alignment.
	AlignCenter = 1

	// AlignRight sets cell right alignment.
	AlignRight = 2
)

Variables

View Source
var (
	// StyleDefault - MySql-like table style:
	//
	// +---+------------------+------------+
	// | # |       NAME       |    TAX     |
	// +---+------------------+------------+
	// | 1 | Newton G. Goetz  |   $ 532.70 |
	// | 2 | Rebecca R. Edney |  $ 1423.25 |
	// | 3 | John R. Jackson  |  $ 7526.12 |
	// | 4 | Ron J. Gomes     |   $ 123.84 |
	// | 5 | Penny R. Lewis   |  $ 3221.11 |
	// +---+------------------+------------+
	// |   |         Subtotal | $ 12827.02 |
	// +---+------------------+------------+
	StyleDefault = &Style{
		Border: &BorderStyle{
			TopLeft:            "+",
			Top:                "-",
			TopRight:           "+",
			Right:              "|",
			BottomRight:        "+",
			Bottom:             "-",
			BottomLeft:         "+",
			Left:               "|",
			TopIntersection:    "+",
			BottomIntersection: "+",
		},
		Divider: &DividerStyle{
			Left:         "+",
			Center:       "-",
			Right:        "+",
			Intersection: "+",
		},
		Cell: "|",
	}

	// StyleCompact - compact table style:
	//
	//  #         NAME            TAX
	// === ================== ============
	//  1   Newton G. Goetz      $ 532.70
	//  2   Rebecca R. Edney    $ 1423.25
	//  3   John R. Jackson     $ 7526.12
	//  4   Ron J. Gomes         $ 123.84
	//  5   Penny R. Lewis      $ 3221.11
	// === ================== ============
	//              Subtotal   $ 12827.02
	StyleCompact = &Style{
		Border: &BorderStyle{
			TopLeft:            "",
			Top:                "",
			TopRight:           "",
			Right:              "",
			BottomRight:        "",
			Bottom:             "",
			BottomLeft:         "",
			Left:               "",
			TopIntersection:    "",
			BottomIntersection: "",
		},
		Divider: &DividerStyle{
			Left:         "",
			Center:       "=",
			Right:        "",
			Intersection: " ",
		},
		Cell: " ",
	}

	// StyleCompactLite - compact lite table style:
	//
	//  #         NAME            TAX
	// --- ------------------ ------------
	//  1   Newton G. Goetz      $ 532.70
	//  2   Rebecca R. Edney    $ 1423.25
	//  3   John R. Jackson     $ 7526.12
	//  4   Ron J. Gomes         $ 123.84
	//  5   Penny R. Lewis      $ 3221.11
	// --- ------------------ ------------
	//              Subtotal   $ 12827.02
	StyleCompactLite = &Style{
		Border: &BorderStyle{
			TopLeft:            "",
			Top:                "",
			TopRight:           "",
			Right:              "",
			BottomRight:        "",
			Bottom:             "",
			BottomLeft:         "",
			Left:               "",
			TopIntersection:    "",
			BottomIntersection: "",
		},
		Divider: &DividerStyle{
			Left:         "",
			Center:       "-",
			Right:        "",
			Intersection: " ",
		},
		Cell: " ",
	}

	// StyleCompactClassic - compact classic table style:
	//
	//  #         NAME            TAX
	//  1   Newton G. Goetz      $ 532.70
	//  2   Rebecca R. Edney    $ 1423.25
	//  3   John R. Jackson     $ 7526.12
	//  4   Ron J. Gomes         $ 123.84
	//  5   Penny R. Lewis      $ 3221.11
	//              Subtotal   $ 12827.02
	StyleCompactClassic = &Style{
		Border: &BorderStyle{
			TopLeft:            "",
			Top:                "",
			TopRight:           "",
			Right:              "",
			BottomRight:        "",
			Bottom:             "",
			BottomLeft:         "",
			Left:               "",
			TopIntersection:    "",
			BottomIntersection: "",
		},
		Divider: &DividerStyle{
			Left:         "",
			Center:       "",
			Right:        "",
			Intersection: " ",
		},
		Cell: " ",
	}

	// StyleMarkdown - markdown table style:
	//
	// | # |       NAME       |    TAX     |
	// |---|------------------|------------|
	// | 1 | Newton G. Goetz  |   $ 532.70 |
	// | 2 | Rebecca R. Edney |  $ 1423.25 |
	// | 3 | John R. Jackson  |  $ 7526.12 |
	// | 4 | Ron J. Gomes     |   $ 123.84 |
	// | 5 | Penny R. Lewis   |  $ 3221.11 |
	// |---|------------------|------------|
	// |   |         Subtotal | $ 12827.02 |
	StyleMarkdown = &Style{
		Border: &BorderStyle{
			TopLeft:            "",
			Top:                "",
			TopRight:           "",
			Right:              "|",
			BottomRight:        "",
			Bottom:             "",
			BottomLeft:         "",
			Left:               "|",
			TopIntersection:    "",
			BottomIntersection: "",
		},
		Divider: &DividerStyle{
			Left:         "|",
			Center:       "-",
			Right:        "|",
			Intersection: "|",
		},
		Cell: "|",
	}

	// StyleRounded - rounded table style:
	//
	// .---.------------------.------------.
	// | # |       NAME       |    TAX     |
	// +---+------------------+------------+
	// | 1 | Newton G. Goetz  |   $ 532.70 |
	// | 2 | Rebecca R. Edney |  $ 1423.25 |
	// | 3 | John R. Jackson  |  $ 7526.12 |
	// | 4 | Ron J. Gomes     |   $ 123.84 |
	// | 5 | Penny R. Lewis   |  $ 3221.11 |
	// +---+------------------+------------+
	// |   |         Subtotal | $ 12827.02 |
	// '---'------------------'------------'
	StyleRounded = &Style{
		Border: &BorderStyle{
			TopLeft:            ".",
			Top:                "-",
			TopRight:           ".",
			Right:              "|",
			BottomRight:        "'",
			Bottom:             "-",
			BottomLeft:         "'",
			Left:               "|",
			TopIntersection:    ".",
			BottomIntersection: "'",
		},
		Divider: &DividerStyle{
			Left:         "+",
			Center:       "-",
			Right:        "+",
			Intersection: "+",
		},
		Cell: "|",
	}

	// StyleUnicode is the Unicode table style.
	StyleUnicode = &Style{
		Border: &BorderStyle{
			TopLeft:            "╔",
			Top:                "═",
			TopRight:           "╗",
			Right:              "║",
			BottomRight:        "╝",
			Bottom:             "═",
			BottomLeft:         "╚",
			Left:               "║",
			TopIntersection:    "╤",
			BottomIntersection: "╧",
		},
		Divider: &DividerStyle{
			Left:         "╟",
			Center:       "━",
			Right:        "╢",
			Intersection: "┼",
		},
		Cell: "│",
	}
)

Functions

This section is empty.

Types

type Body

type Body struct {
	Cells [][]*Cell
}

Body is a table body.

type BorderStyle

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

BorderStyle defines the table border style.

type Cell

type Cell struct {
	Align int    // Cell alignment
	Span  int    // span cell to right (1 - default)
	Text  string // Cell raw text
	// contains filtered or unexported fields
}

Cell is a table cell.

type DividerStyle

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

DividerStyle defines the table divider style.

type Footer struct {
	Cells []*Cell
}

Footer is a table footer.

type Header struct {
	Cells []*Cell
}

Header is a table header.

type Style

type Style struct {
	Border  *BorderStyle
	Divider *DividerStyle
	Cell    string // Symbol between cells
}

Style defines a table style, including borders and dividers.

type Table

type Table struct {
	Header *Header
	Body   *Body
	Footer *Footer
	// contains filtered or unexported fields
}

Table is the main table object.

func New

func New() *Table

New is a Table constructor. It loads struct data, ready to be manipulated.

func (*Table) Print

func (t *Table) Print()

Print writes the rendered table to standard output.

func (*Table) Println

func (t *Table) Println()

Println writes the rendered table to standard output followed by a newline.

func (*Table) SetStyle

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

SetStyle sets the table style.

func (*Table) String

func (t *Table) String() string

String returns the rendered table as a string.

Directories

Path Synopsis
_example
01-styles-demo command
Package main demonstrates the built-in table styles.
Package main demonstrates the built-in table styles.
02-ugly-span command
Package main demonstrates spanning cells.
Package main demonstrates spanning cells.
04-multiline command
Package main demonstrates multiline table cells.
Package main demonstrates multiline table cells.
05-colorize command
Package main demonstrates colored table content.
Package main demonstrates colored table content.

Jump to

Keyboard shortcuts

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