tablewriter

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StyleRounded is a table style that displays a list of items with rounded corners.
	// ╭──────────────────────────────────────────────────────────────────────────────────────╮
	// │ EC2 Instances                                                                        │
	// ├─────────────────────┬─────────────────────┬─────────┬───────────────┬────────────────┤
	// │ Name                │ Instance ID         │ State   │ Instance Type │ Public IP      │
	// ├─────────────────────┼─────────────────────┼─────────┼───────────────┼────────────────┤
	// │ web-server-prod     │ i-0abc123def456789a │ running │ m7i.xlarge    │ 18.134.56.201  │
	// │ app-server-staging  │ i-0def456789abc123b │ running │ r7i.xlarge    │ 35.178.45.123  │
	// │ database-primary    │ i-0789abc123def456c │ running │ c7i.large     │ 52.49.178.90   │
	// ╰─────────────────────┴─────────────────────┴─────────┴───────────────┴────────────────╯
	StyleRounded = table.Style{
		Name:    "rounded",
		Box:     table.StyleBoxRounded,
		Color:   ColorOptionsDefault,
		Format:  table.FormatOptions{Header: text.FormatTitle},
		Options: table.OptionsDefault,
		Size:    table.SizeOptionsDefault,
		Title:   table.TitleOptionsDefault,
	}

	// StylePlain is a table style that displays a list of items with no borders or separators.
	// NAME                INSTANCE ID          STATE    INSTANCE TYPE  PUBLIC IP
	// web-server-prod     i-0abc123def456789a  running  m7i.xlarge     18.134.56.201
	// app-server-staging  i-0def456789abc123b  running  r7i.xlarge     35.178.45.123
	// database-primary    i-0789abc123def456c  running  c7i.large      52.49.178.90
	StylePlain = table.Style{
		Name:    "list",
		Box:     table.StyleBoxRounded,
		Color:   ColorOptionsDefault,
		Format:  table.FormatOptionsDefault,
		Options: table.OptionsNoBordersAndSeparators,
	}

	// Style RoundedSeparated is a table style that displays a list of items with rounded corners and separated rows.
	// ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
	// │ RDS Clusters and Instances                                                                                   │
	// ├────────────────────┬────────────────────────────────────┬───────────┬──────────────┬────────────────┬────────┤
	// │ Cluster Identifier │ Instance Identifier                │ Status    │ Engine       │ Size           │ Role   │
	// ├────────────────────┼────────────────────────────────────┼───────────┼──────────────┼────────────────┼────────┤
	// │ prod-cluster-01    │ prod-cluster-01-instance-1         │ available │ aurora-mysql │ db.r6g.2xlarge │ Writer │
	// │                    ├────────────────────────────────────┼───────────┼──────────────┼────────────────┼────────┤
	// │                    │ prod-cluster-01-instance-2         │ available │ aurora-mysql │ db.r6g.2xlarge │ Reader │
	// ├────────────────────┼────────────────────────────────────┼───────────┼──────────────┼────────────────┼────────┤
	// │ dev-cluster-01     │ dev-cluster-01-instance-1          │ available │ aurora-mysql │ db.t3.medium   │ Reader │
	// │                    ├────────────────────────────────────┼───────────┼──────────────┼────────────────┼────────┤
	// │                    │ dev-cluster-01-instance-2          │ available │ aurora-mysql │ db.t4g.medium  │ Writer │
	// ╰────────────────────┴────────────────────────────────────┴───────────┴──────────────┴────────────────┴────────╯
	StyleRoundedSeparated = table.Style{
		Name:    "rounded-separated",
		Box:     table.StyleBoxRounded,
		Color:   ColorOptionsDefault,
		Format:  table.FormatOptions{Header: text.FormatTitle},
		Options: DrawAllBordersAndSeparators,
		Size:    table.SizeOptionsDefault,
		Title:   table.TitleOptionsDefault,
	}
)
View Source
var (
	ColorOptionsDefault = table.ColorOptions{
		Border:       text.Colors{},
		Footer:       text.Colors{},
		Header:       text.Colors{text.Bold},
		IndexColumn:  text.Colors{},
		Row:          text.Colors{},
		RowAlternate: text.Colors{},
		Separator:    text.Colors{},
	}
)

ColorOptions

View Source
var (
	DrawAllBordersAndSeparators = table.Options{
		DrawBorder:      true,
		SeparateColumns: true,
		SeparateFooter:  true,
		SeparateHeader:  true,
		SeparateRows:    true,
	}
)

Options

View Source
var TableStyles = map[string]table.Style{
	"rounded":           StyleRounded,
	"plain":             StylePlain,
	"rounded-separated": StyleRoundedSeparated,
}

Functions

func AddSection

func AddSection(t AscWriter, s Section)

AddSection adds a single section to a table writer

func AddSections

func AddSections(t AscWriter, sections []Section)

AddSections adds multiple sections to a table writer

func BuildHeaderRow

func BuildHeaderRow(fields []Field) []string

func RenderList added in v0.2.1

func RenderList(opts RenderListOptions)

RenderList creates and renders a list-style table with the provided options. This helper consolidates the common pattern used across all list commands:

  • Creates a table with the specified title and style
  • Applies plain style if PlainStyle is true
  • Appends tag fields to the field list
  • Builds and appends header row
  • Builds and appends data rows using the provided getters
  • Configures field sorting
  • Renders the table

func SetFieldVisibility added in v0.2.1

func SetFieldVisibility(fields []Field, name string, visible bool)

SetFieldVisibility sets the visibility of a field by name. It iterates through the fields slice and sets Visible to the specified value for the first field matching the given name.

Types

type AscTable

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

AscTable is the implementation of the AscWriter interface.

func (*AscTable) AppendGridRow

func (at *AscTable) AppendGridRow(ar GridRow)

AppendGridRow creates a new grid row with the provided fields and values.

┌───────────────────────┬─────────────────────────┬───────────────────────┐
│ FirstName             │ LastName                │ Age                   │
├───────────────────────┼─────────────────────────┼───────────────────────┤
│ John                  │ Doe                     │ 30                    │
├───────────────────────┼─────────────────────────┼───────────────────────┤

func (*AscTable) AppendGridRows

func (at *AscTable) AppendGridRows(ar []GridRow)

AppendGridRows accepts a list of GridRows and creates new grid rows for each.

func (*AscTable) AppendHeader

func (at *AscTable) AppendHeader(headers []string)

AppendHeader creates a header row that will be formatted according to the table style.

┌───────────────────────┬─────────────────────────┬───────────────────────┐
│ FirstName             │ LastName                │ Age                   │
├───────────────────────┼─────────────────────────┼───────────────────────┤

func (*AscTable) AppendHorizontalRow

func (at *AscTable) AppendHorizontalRow(hr HorizontalRow)

AppendHorizontalRow creates a new row that is made up of the provided name (column 1) and the value (all remaining columns, merged).

┌───────────────────────┬─────────────────────────────────────────────────┐
│ Name                  │ John Doe                                        │
╰───────────────────────┴─────────────────────────────────────────────────╯

func (*AscTable) AppendHorizontalRows

func (at *AscTable) AppendHorizontalRows(hr []HorizontalRow)

AppendHorizontalRow accepts a list of Rows and creates new horizontal rows for each.

func (*AscTable) AppendRow

func (at *AscTable) AppendRow(row Row)

AppendRow creates a standard row with the provided values.

func (*AscTable) AppendRows

func (at *AscTable) AppendRows(rows []Row)

AppendRows creates a new row for each of the provided rows.

func (*AscTable) AppendTitleRow

func (at *AscTable) AppendTitleRow(title string)

AppendHeaderRow creates a new row that is made up of the provided title * at.columns times.

┌─────────────────────────────────────────────────────────────────────────┐
│ Title                                                                   │
╰─────────────────────────────────────────────────────────────────────────╯

func (*AscTable) GetColumns

func (at *AscTable) GetColumns() int

GetColumns returns the number of columns in the table

func (*AscTable) Render

func (at *AscTable) Render()

Render writes the table to the console.

func (*AscTable) SetColumnWidth

func (at *AscTable) SetColumnWidth(minWidth int, maxWidth int)

SetColumnWidth sets the minimum and maximum width for all columns.

func (*AscTable) SetFieldConfigs

func (at *AscTable) SetFieldConfigs(fields []Field, reverse bool)

SetFieldConfigs sets the field configurations for the table.

func (*AscTable) SetRenderStyle

func (at *AscTable) SetRenderStyle(style string)

SetRenderStyle sets the style for rendering

func (*AscTable) SetStyle

func (at *AscTable) SetStyle(style string)

SetStyle sets the style of the table. SetStyle sets the style of the table

type AscTableRenderOptions

type AscTableRenderOptions struct {
	Title          string
	Style          string
	Columns        int
	ColumnConfigs  []table.ColumnConfig
	MinColumnWidth int
	MaxColumnWidth int
	MergedColumns  []string
}

AscTableRenderOptions is the options for the AscTable.

type AscWriter

type AscWriter interface {
	AppendRow(row Row)
	AppendRows(rows []Row)
	AppendHeader(headers []string)
	AppendTitleRow(title string)
	AppendHorizontalRow(hr HorizontalRow)
	AppendHorizontalRows([]HorizontalRow)
	AppendGridRow(ar GridRow)
	AppendGridRows([]GridRow)
	Render()
	SetColumnWidth(minWidth int, maxWidth int)
	GetColumns() int
	SetStyle(style string)
	SetRenderStyle(style string)
	SetFieldConfigs(fields []Field, reverse bool)
}

AscWriter is the interface for the AscTable.

func NewAscWriter

func NewAscWriter(renderOptions AscTableRenderOptions) AscWriter

NewAscWriter creates a new AscWriter with the provided number of columns.

type AttributeGetter

type AttributeGetter func(fieldName string, instance any) (string, error)

AttributeGetter is a function type that retrieves a field value from an instance

type DetailTable

type DetailTable struct {
	Headers  []string
	Rows     []Row
	Options  AscTableRenderOptions
	Sections []Section
}

DetailTable handles detailed tables.

func NewDetailTable

func NewDetailTable(options AscTableRenderOptions) *DetailTable

NewDetailTable creates a new DetailTable.

func (*DetailTable) AddHeader

func (dt *DetailTable) AddHeader(headers []string)

func (*DetailTable) AddRow

func (dt *DetailTable) AddRow(row Row)

func (*DetailTable) AddSection

func (dt *DetailTable) AddSection(s Section)

AddSection adds a section to the detail table

func (*DetailTable) AddSections

func (dt *DetailTable) AddSections(sections []Section)

AddSections adds multiple sections to the detail table

func (*DetailTable) Render

func (dt *DetailTable) Render()

type Field

type Field struct {
	Category      string
	Name          string
	Value         string
	Visible       bool
	Merge         bool
	DefaultSort   bool
	SortBy        bool
	SortDirection SortDirection
}

Field is a single field in a row. It contains a name and a value.

func AppendTagFields

func AppendTagFields(fields []Field, tags []string, instances []any) []Field

func PopulateFieldValues

func PopulateFieldValues(instance any, fields []Field, getFieldValue AttributeGetter) ([]Field, error)

type GridRow

type GridRow struct {
	Fields []Field
}

GridRow is made up of two go-pretty table.Row objects. The first row is one or more field names, and the second row is the value(s) for each field.

type HorizontalRow

type HorizontalRow struct {
	Field Field
}

HorizontalRow is made up of a single field. The field name is the first column, and the value is the rest of the columns.

type Layout

type Layout string

Layout is the layout of a section.

const (
	Grid       Layout = "Grid"
	Horizontal Layout = "Horizontal"
)

Layout constants.

type ListTable

type ListTable struct {
	Headers []string
	Rows    []Row
	Options AscTableRenderOptions
}

ListTable handles simple list-style tables

func NewListTable

func NewListTable(options AscTableRenderOptions) *ListTable

NewListTable creates a new ListTable.

func (*ListTable) AddHeader

func (lt *ListTable) AddHeader(headers []string)

func (*ListTable) AddRow

func (lt *ListTable) AddRow(row Row)

func (*ListTable) Render

func (lt *ListTable) Render()

type RenderListOptions added in v0.2.1

type RenderListOptions struct {
	Title         string
	Style         string
	PlainStyle    bool
	Fields        []Field
	Tags          []string
	Data          []any
	GetFieldValue AttributeGetter
	GetTagValue   TagGetter
	ReverseSort   bool
	HideEmpty     bool
}

RenderListOptions contains the configuration for rendering a list table.

type Row

type Row struct {
	Values []string
}

Row is a single row of a table. It contains a list of values.

func BuildRows

func BuildRows(instances []any, fields []Field, getFieldValue AttributeGetter, getTagValue TagGetter) []Row

BuildRows builds a slice of tablewriter.Row objects from a slice of instances and fields This is used to build the rows for a "List" table

type Section

type Section struct {
	SectionConfig SectionConfig
	Fields        []Field
	Title         string
}

Section is a section of a table. It contains a title, fields, and a section config.

func BuildSection

func BuildSection(title string, fields []Field, layout Layout) Section

BuildSection builds a section with a row layout.

func BuildSections

func BuildSections(fields []Field, layout Layout) []Section

BuildSections builds a list of sections from a list of fields with a grid layout. The title of each section is the category of the fields.

type SectionConfig

type SectionConfig struct {
	Layout Layout
}

SectionConfig is the configuration for a section. For now the SectionConfig only contains a "Layout" The Layout can be "Grid" or "Horizontal"

type SortDirection

type SortDirection table.SortMode

SortDirection is the direction of the sort.

type TagGetter

type TagGetter func(tagKey string, instance any) (string, error)

TagGetter is a function type that retrieves a tag value from an instance

Jump to

Keyboard shortcuts

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