or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmain-package.mdrenderer-package.mdtw-package.mdtwcache-package.mdtwwarp-package.mdtwwidth-package.md
tile.json

index.mddocs/

TableWriter for Go

TableWriter is a comprehensive Go library for generating rich text-based tables with support for multiple output formats (ASCII, Unicode, Markdown, HTML, SVG, colorized terminals), advanced styling (cell merging, alignment, padding, borders), flexible input methods (CSV, structs, slices, streaming), and high performance features.

Package Information

  • Package Name: github.com/olekukonko/tablewriter
  • Package Type: go
  • Language: Go
  • Version: v1.1.2
  • Installation: go get github.com/olekukonko/tablewriter@v1.1.2

Core Imports

import (
    "github.com/olekukonko/tablewriter"
    "github.com/olekukonko/tablewriter/renderer"
    "github.com/olekukonko/tablewriter/tw"
    "github.com/olekukonko/tablewriter/pkg/twcache"
    "github.com/olekukonko/tablewriter/pkg/twwarp"
    "github.com/olekukonko/tablewriter/pkg/twwidth"
)

Basic Usage

package main

import (
    "github.com/olekukonko/tablewriter"
    "os"
)

func main() {
    data := [][]string{
        {"Alice", "25", "New York"},
        {"Bob", "30", "Boston"},
    }

    table := tablewriter.NewWriter(os.Stdout)
    table.Header("Name", "Age", "City")
    table.Bulk(data)
    table.Render()
}

Output:

┌───────┬─────┬──────────┐
│ NAME  │ AGE │   CITY   │
├───────┼─────┼──────────┤
│ Alice │ 25  │ New York │
│ Bob   │ 30  │ Boston   │
└───────┴─────┴──────────┘

Capabilities

Table Creation and Configuration

Create tables with flexible configuration options using functional patterns or builder-style configuration.

func NewTable(w io.Writer, opts ...Option) *Table

func NewWriter(w io.Writer) *Table

func NewCSV(writer io.Writer, fileName string, hasHeader bool, opts ...Option) (*Table, error)

func NewCSVReader(writer io.Writer, csvReader *csv.Reader, hasHeader bool, opts ...Option) (*Table, error)

The Table type provides methods for data management and rendering:

type Table struct {
    // internal fields
}

func (t *Table) Header(elements ...any)
func (t *Table) Append(rows ...interface{}) error
func (t *Table) Bulk(rows interface{}) error
func (t *Table) Footer(elements ...any)
func (t *Table) Render() error
func (t *Table) Configure(fn func(cfg *Config)) *Table
func (t *Table) Options(opts ...Option) *Table
func (t *Table) Start() error
func (t *Table) Close() error

Configuration is managed through the Config struct and functional options:

type Config struct {
    MaxWidth  int
    Header    tw.CellConfig
    Row       tw.CellConfig
    Footer    tw.CellConfig
    Debug     bool
    Stream    tw.StreamConfig
    Behavior  tw.Behavior
    Widths    tw.CellWidth
    Counter   tw.Counter
}

type Option func(target *Table)

Main Package Documentation

Rendering Engines

Multiple output formats with customizable renderers for ASCII, Unicode, Markdown, HTML, SVG, and colorized terminals.

func NewBlueprint(configs ...tw.Rendition) *Blueprint

func NewMarkdown(configs ...tw.Rendition) *Markdown

func NewHTML(configs ...HTMLConfig) *HTML

func NewSVG(configs ...SVGConfig) *SVG

func NewColorized(configs ...ColorizedConfig) *Colorized

func NewOcean(oceanConfig ...OceanConfig) *Ocean

All renderers implement the core Renderer interface:

type Renderer interface {
    Start(w io.Writer) error
    Header(headers [][]string, ctx Formatting)
    Row(row []string, ctx Formatting)
    Footer(footers [][]string, ctx Formatting)
    Line(ctx Formatting)
    Config() Rendition
    Close() error
    Logger(logger *ll.Logger)
}

Renderer Package Documentation

Core Types and Interfaces

Comprehensive type system for table configuration including alignment, borders, symbols, padding, merging, and state management.

type Align string

const (
    AlignNone    Align = "none"
    AlignCenter  Align = "center"
    AlignRight   Align = "right"
    AlignLeft    Align = "left"
    AlignDefault       = AlignLeft
)

type State int

const (
    Unknown State = 0
    On      State = 1
    Off     State = -1
)

type CellConfig struct {
    Formatting   CellFormatting
    Padding      CellPadding
    Callbacks    CellCallbacks
    Filter       CellFilter
    Alignment    CellAlignment
    ColMaxWidths CellWidth
    Merging      CellMerging
}

type Rendition struct {
    Borders  Border
    Symbols  Symbols
    Settings Settings
    Streaming bool
}

type Behavior struct {
    AutoHide  State
    TrimSpace State
    TrimLine  State
    Header    Control
    Footer    Control
    Compact   Compact
    Structs   Struct
}

Border styles with 44+ predefined styles:

type BorderStyle int

const (
    StyleNone BorderStyle = iota
    StyleASCII
    StyleLight
    StyleHeavy
    StyleDouble
    StyleRounded
    StyleMarkdown
    // ... 38+ more styles
)

func NewSymbols(style BorderStyle) Symbols

func NewSymbolCustom(name string) *SymbolCustom

Generic collection types:

type Mapper[K comparable, V any] struct {
    // internal fields
}

func NewMapper[K comparable, V any]() Mapper[K, V]

type Slicer[T any] struct {
    // internal fields
}

func NewSlicer[T any]() Slicer[T]

TW Package Documentation

LRU Caching

Generic LRU cache implementation with eviction callbacks and statistics for performance optimization.

type Cache[K comparable, V any] interface {
    Add(key K, value V) (evicted bool)
    Get(key K) (value V, ok bool)
    Purge()
}

type LRU[K comparable, V any] struct {
    // internal fields
}

func NewLRU[K comparable, V any](size int) *LRU[K, V]

func NewLRUEvict[K comparable, V any](size int, onEvict EvictCallback[K, V]) *LRU[K, V]

func (c *LRU[K, V]) Get(key K) (V, bool)
func (c *LRU[K, V]) GetOrCompute(key K, compute func() V) V
func (c *LRU[K, V]) Add(key K, value V) bool
func (c *LRU[K, V]) HitRate() float64

TWCache Package Documentation

Text Wrapping

Text wrapping algorithms using Knuth-Plass-inspired line breaking for optimal word wrapping with minimal raggedness.

func SplitWords(s string) []string

func WrapString(s string, lim int) ([]string, int)

func WrapStringWithSpaces(s string, lim int) ([]string, int)

func WrapWords(words []string, spc, lim, pen int) [][]string

TWWarp Package Documentation

Display Width Calculation

Display width calculation with ANSI escape sequence handling, East Asian width support, and caching for performance.

type Options struct {
    EastAsianWidth bool
}

func Width(str string) int

func WidthWithOptions(str string, opts Options) int

func WidthNoCache(str string) int

func Truncate(s string, maxWidth int, suffix ...string) string

func SetEastAsian(enable bool)

func IsEastAsian() bool

func SetCacheCapacity(capacity int)

func GetCacheStats() (size, capacity int, hitRate float64)

func Filter() *regexp.Regexp

TWWidth Package Documentation

Key Features

  • Multi-format rendering: ASCII, Unicode, Markdown, HTML, SVG, ANSI-colored terminals
  • Advanced styling: Cell merging (vertical, horizontal, hierarchical), alignment, padding, custom borders
  • Flexible input: CSV files, Go structs, slices, streaming data
  • High performance: Buffer reuse, LRU caching, minimal allocations
  • Modern Go features: Generics support (Go 1.18+), fluent interfaces, functional options
  • Customizable: Custom renderers, formatters, border styles, callbacks, filters
  • Database friendly: Native support for sql.Null* types
  • Safe: Auto-escaping for HTML/Markdown output