table

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 2 Imported by: 0

README

table

Static table rendering for CLI output.

Overview

The table package provides simple, non-interactive table rendering for command-line applications. It's designed for tools that need to display tabular data and exit (like kubectl get, ls -l, docker ps, etc.).

This is separate from the interactive Bubble Tea components in the parent tui package.

Installation

go get github.com/SCKelemen/tui/table

Quick Start

package main

import (
    "github.com/SCKelemen/tui/table"
)

func main() {
    t := table.New("Name", "Status", "Age")
    t.AddRow("service-a", "Running", "2d")
    t.AddRow("service-b", "Stopped", "5h")
    t.AddRow("service-c", "Starting", "30s")
    t.Print()
}

Output:

┌───────────┬──────────┬──────┐
│ Name      │ Status   │ Age  │
├───────────┼──────────┼──────┤
│ service-a │ Running  │ 2d   │
├───────────┼──────────┼──────┤
│ service-b │ Stopped  │ 5h   │
├───────────┼──────────┼──────┤
│ service-c │ Starting │ 30s  │
└───────────┴──────────┴──────┘

Features

  • Unicode box drawing - Beautiful rounded borders by default
  • Multiple border styles - Rounded, double-line, or ASCII for compatibility
  • Automatic column sizing - Columns auto-expand to fit content
  • Bold headers - Optional bold formatting for headers
  • Simple or detailed - Choose between full borders or compact mode

Border Styles

Rounded (default)
t := table.New("A", "B")
t.AddRow("1", "2")
t.Print()
┌───┬───┐
│ A │ B │
├───┼───┤
│ 1 │ 2 │
└───┴───┘
Double-line
t := table.New("A", "B")
t.SetBorderStyle(table.BorderStyleDouble)
t.AddRow("1", "2")
t.Print()
╔═══╦═══╗
║ A ║ B ║
╠═══╬═══╣
║ 1 ║ 2 ║
╚═══╩═══╝
ASCII (maximum compatibility)
t := table.New("A", "B")
t.SetBorderStyle(table.BorderStyleASCII)
t.AddRow("1", "2")
t.Print()
+---+---+
| A | B |
+---+---+
| 1 | 2 |
+---+---+

Simple Mode

For more compact output without row separators:

t := table.New("Name", "Status")
t.AddRow("service-a", "Running")
t.AddRow("service-b", "Stopped")
t.PrintSimple()
┌───────────┬─────────┐
│ Name      │ Status  │
├───────────┼─────────┤
│ service-a │ Running │
│ service-b │ Stopped │
└───────────┴─────────┘

API Reference

Creating Tables
// Create a table with headers
t := table.New("Column1", "Column2", "Column3")

// Set border style
t.SetBorderStyle(table.BorderStyleRounded)  // default
t.SetBorderStyle(table.BorderStyleDouble)
t.SetBorderStyle(table.BorderStyleASCII)

// Control header formatting
t.SetHeaderBold(true)  // default
t.SetHeaderBold(false)
Adding Data
// Add a single row
t.AddRow("value1", "value2", "value3")

// Add multiple rows at once
rows := [][]string{
    {"a", "b", "c"},
    {"d", "e", "f"},
}
t.AddRows(rows)

// Clear all rows (keeps headers)
t.Clear()
Rendering
// Get rendered string
output := t.Render()        // with row separators
output := t.RenderSimple()  // without row separators

// Print to stdout
t.Print()        // with row separators
t.PrintSimple()  // without row separators

// Or use as a Stringer
fmt.Println(t)  // calls Render()

Use Cases

Perfect for:

  • kubectl-style output - myapp get resources
  • ls-style listings - myapp list --format table
  • Status displays - myapp status --all
  • Comparison tables - myapp compare service-a service-b
  • Any CLI tool that needs formatted table output

Comparison with Interactive Components

Feature table package tui interactive components
Use case Static CLI output Interactive TUI apps
Dependencies None (stdlib only) Bubble Tea
Keyboard nav No Yes
Mouse support No Yes
Updates No Yes (real-time)
Complexity Very simple Full framework

Design Philosophy

The table package follows these principles:

  1. Zero dependencies - Only uses Go stdlib
  2. Print and exit - For non-interactive CLI tools
  3. Beautiful by default - Unicode borders, proper spacing
  4. Fallback support - ASCII mode for restricted terminals
  5. Simple API - Minimal methods, intuitive usage

Performance

The table renderer is optimized for CLI output:

  • Memory efficient - Single string builder allocation
  • Fast rendering - Benchmarked at ~50µs for 100 rows
  • Minimal allocations - Reuses buffers where possible

Future Enhancements

Potential additions:

  • Column alignment (left, right, center)
  • Color support for cells
  • Column width limits with truncation
  • Footer rows
  • Multi-line cell support
  • Custom cell padding
  • Row highlighting

Contributing

Contributions welcome! Please ensure:

  • All tests pass: go test ./table
  • Code is formatted: go fmt ./table
  • Examples work: go run ./table/example

License

MIT

Documentation

Overview

Package table provides static table rendering for CLI output.

This package is designed for non-interactive command output (like kubectl get, ls -l, etc.) and is separate from the interactive Bubble Tea components in the parent tui package.

Example usage:

table := table.New("Name", "Status", "Age")
table.AddRow("service-a", "Running", "2d")
table.AddRow("service-b", "Stopped", "5h")
fmt.Println(table.Render())

Output:

┌───────────┬─────────┬─────┐
│ Name      │ Status  │ Age │
├───────────┼─────────┼─────┤
│ service-a │ Running │ 2d  │
├───────────┼─────────┼─────┤
│ service-b │ Stopped │ 5h  │
└───────────┴─────────┴─────┘

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// BorderStyleRounded uses rounded Unicode box-drawing characters
	BorderStyleRounded = BorderStyle{
		TopLeft:     "┌",
		TopRight:    "┐",
		BottomLeft:  "└",
		BottomRight: "┘",
		Horizontal:  "─",
		Vertical:    "│",
		Cross:       "┼",
		LeftT:       "├",
		RightT:      "┤",
		TopT:        "┬",
		BottomT:     "┴",
	}

	// BorderStyleDouble uses double-line box-drawing characters
	BorderStyleDouble = BorderStyle{
		TopLeft:     "╔",
		TopRight:    "╗",
		BottomLeft:  "╚",
		BottomRight: "╝",
		Horizontal:  "═",
		Vertical:    "║",
		Cross:       "╬",
		LeftT:       "╠",
		RightT:      "╣",
		TopT:        "╦",
		BottomT:     "╩",
	}

	// BorderStyleASCII uses plain ASCII characters for maximum compatibility
	BorderStyleASCII = BorderStyle{
		TopLeft:     "+",
		TopRight:    "+",
		BottomLeft:  "+",
		BottomRight: "+",
		Horizontal:  "-",
		Vertical:    "|",
		Cross:       "+",
		LeftT:       "+",
		RightT:      "+",
		TopT:        "+",
		BottomT:     "+",
	}
)

Common border styles

Functions

This section is empty.

Types

type BorderStyle

type BorderStyle struct {
	TopLeft     string
	TopRight    string
	BottomLeft  string
	BottomRight string
	Horizontal  string
	Vertical    string
	Cross       string
	LeftT       string
	RightT      string
	TopT        string
	BottomT     string
}

BorderStyle defines the characters used for table borders

type Table

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

Table represents a static table for CLI output

Example

Example test that demonstrates usage

table := New("Name", "Status", "Age")
table.AddRow("service-a", "Running", "2d")
table.AddRow("service-b", "Stopped", "5h")
table.Print()
// Output will show a formatted table

func New

func New(headers ...string) *Table

New creates a new table with the given headers

func (*Table) AddRow

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

AddRow adds a row to the table

func (*Table) AddRows

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

AddRows adds multiple rows to the table

func (*Table) Clear

func (t *Table) Clear()

Clear removes all rows but keeps headers

func (*Table) Print

func (t *Table) Print()

Print renders and prints the table to stdout

func (*Table) PrintSimple

func (t *Table) PrintSimple()

PrintSimple renders and prints the table in simple mode (no row separators)

func (*Table) Render

func (t *Table) Render() string

Render renders the table to a string

func (*Table) RenderSimple

func (t *Table) RenderSimple() string

RenderSimple renders the table without row separators (more compact)

func (*Table) SetBorderStyle

func (t *Table) SetBorderStyle(style BorderStyle)

SetBorderStyle sets the border style for the table

func (*Table) SetHeaderBold

func (t *Table) SetHeaderBold(bold bool)

SetHeaderBold controls whether headers are rendered in bold

func (*Table) String

func (t *Table) String() string

String implements the Stringer interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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