table

package module
v0.30.4 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package table provides terminal table output formatting using lipgloss.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render added in v0.30.0

func Render(data *output.Table, opts ...Option) (string, error)

Render renders a Table as a styled terminal table string.

func Write added in v0.30.0

func Write(w io.Writer, data *output.Table, opts ...Option) error

Write writes a Table as a styled terminal table to the provided writer. Uses lipgloss for rendering with optional color mode control.

Types

type FooterProvider added in v0.6.1

type FooterProvider interface {
	GetFooter() []string
}

FooterProvider is an optional interface that TableProvider can implement to provide a footer row (e.g., totals). Checked via type assertion in FromTable.

type Option

type Option func(*Table)

Option configures a Table during construction.

func WithColorMode

func WithColorMode(mode output.ColorMode) Option

WithColorMode sets the color mode for the table renderer. ColorModeAuto (default) enables colors when stdout is a terminal. ColorModeNever disables all ANSI styling. ColorModeAlways forces ANSI styling regardless of terminal state.

func WithFooterStyle added in v0.6.1

func WithFooterStyle(fn func(lipgloss.Style) lipgloss.Style) Option

WithFooterStyle sets a custom style function for the footer row. The provided function receives a base lipgloss.Style (with padding) and returns the styled result. When set, this overrides the default bold footer styling.

type Table

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

Table renders formatted tables using lipgloss.

func FromTable added in v0.30.0

func FromTable(data TableProvider, opts ...Option) *Table

FromTable creates a new Table populated from a TableProvider. If data is nil, returns an empty table. If data also implements FooterProvider, the footer row is added and bold-styled.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/table"
	"github.com/larsartmann/go-output/testhelpers"
)

func main() {
	data := output.NewTable([]string{"Name", "Score"})
	data.AddRow([]string{"Alice", "95"})
	data.AddRow([]string{"Bob", "87"})

	tbl := table.FromTable(data, table.WithColorMode(output.ColorModeNever))

	fmt.Println(testhelpers.MustRender(tbl))
}

func New

func New(opts ...Option) *Table

New creates a new Table with default styling. Pass WithColorMode to control ANSI color output (default: ColorModeAuto).

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output/table"
	"github.com/larsartmann/go-output/testhelpers"
)

func main() {
	tbl := table.New()
	tbl.SetHeaders("Name", "Age", "City")
	tbl.AddRow("Alice", "30", "NYC")
	tbl.AddRow("Bob", "25", "LA")

	fmt.Println(testhelpers.MustRender(tbl))
}

func (*Table) AddRow

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

AddRow adds a row to the table.

func (*Table) AsTableRenderer added in v0.6.1

func (t *Table) AsTableRenderer() output.TableRenderer

AsTableRenderer returns a TableRenderer that delegates to this Table. This adapts the variadic API (...string) to the slice-based TableRenderer interface.

func (*Table) Render

func (t *Table) Render() (string, error)

Render returns the rendered table string.

func (*Table) SetFooter added in v0.6.1

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

SetFooter adds a bold-styled footer row to the table. Only the last footer row receives bold styling; previous footer rows become unstyled data rows. Use SetFooter once for a single summary row.

Example
package main

import (
	"fmt"

	"github.com/larsartmann/go-output"
	"github.com/larsartmann/go-output/table"
	"github.com/larsartmann/go-output/testhelpers"
)

func main() {
	tbl := table.New(table.WithColorMode(output.ColorModeNever))
	tbl.SetHeaders("Name", "Score")
	tbl.AddRow("Alice", "95")
	tbl.AddRow("Bob", "87")
	tbl.SetFooter("Total", "182")

	fmt.Println(testhelpers.MustRender(tbl))
}

func (*Table) SetHeaders

func (t *Table) SetHeaders(headers ...string) *Table

SetHeaders sets the table headers.

func (*Table) StyleFunc

func (t *Table) StyleFunc(fn func(row, col int) lipgloss.Style) *Table

StyleFunc sets a custom style function.

type TableProvider added in v0.30.0

type TableProvider interface {
	GetHeaders() []string
	GetRows() [][]string
}

TableProvider defines the interface for types that provide tabular data. The root package's Table satisfies this interface implicitly.

Jump to

Keyboard shortcuts

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