table

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2025 License: MIT Imports: 14 Imported by: 0

README

table

Go Reference

Package table offers an easy way to generate tables similar to Taskwarrior, featuring 256-color support, automatic text wrapping and Markdown rendering.

Features

  • Supports 256-color output for improved readability in terminal applications.
  • Automatically wraps text to fit the specified column width.
  • Renders Markdown formatting in tables, including bold, italic, and inline code.
  • Automatically hides empty columns.

Screenshots

Preview

Credits

Documentation

Overview

Package table offers an easy way to generate tables similar to Taskwarrior, featuring 256-color support, automatic text wrapping and Markdown rendering.

tbl := table.NewTableWithStyle(&table.TableStyle{
	DefaultWidth:  80,
	FitToTerminal: false,
	WrapText:      true,
	Markdown:      true,
	HideEmpty:     true,
	OuterPadding:  0,
	InnerPadding:  1,
})

bgGreen := text.BgGreen.Sprint("BgGreen")
bgBlue := text.BgBlue.Sprint("BgBlue")
fgRed := text.FgRed.Sprint("FgRed")

tbl.AddHeader("Feature", "Example", "Example")
tbl.AddRow(table.Row{"**Color-256**", bgGreen + " " + bgBlue, fgRed})
tbl.AddRow(table.Row{"**Wrap Text**", strings.Repeat("Hello World! ", 20), "Hello World!"})
tbl.AddRow(table.Row{"Markdown", "[link](http://example.com)", "**Bold** _Italic_ `inline Code`"})

tbl.SetHeaderStyle(&table.CellStyle{
	CellAttrs: text.Colors{text.FgGreen, text.Underline},
})

fmt.Print(tbl.Render())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell added in v0.1.5

type Cell struct {
	Content string

	// Prefix defines the prefix of the cell.
	Prefix string
	// PrefixFunc defines the prefix function of the cell.
	PrefixFunc func(isFirst, isLast bool) string

	// Suffix defines the suffix of the cell.
	Suffix string
	// SuffixFunc defines the suffix function of the cell.
	SuffixFunc func(isFirst, isLast bool) string
	// contains filtered or unexported fields
}

type CellStyle added in v0.1.2

type CellStyle struct {
	// Align defines the alignment of the text.
	Align text.Align

	// WrapText defines if the text should be wrapped.
	WrapText *bool

	// Markdown defines if the text should be rendered as markdown.
	Markdown *bool

	// TextAttrs defines the text attributes.
	TextAttrs text.Colors

	// CellAttrs defines the cell attributes.
	CellAttrs text.Colors
}

CellStyle is the style of a cell in the table

type Row added in v0.1.5

type Row []any

type Table

type Table interface {
	AddHeader(header ...string)
	AddRow(row Row)
	AddRows(rows []Row)

	Length() int

	SetStyle(style *TableStyle)
	SetHeaderStyle(style *CellStyle)
	SetRowStyle(row int, style *CellStyle)
	SetColStyle(col int, style *CellStyle)

	Render() string
}

func NewTable

func NewTable() Table

func NewTableWithStyle added in v0.1.2

func NewTableWithStyle(style *TableStyle) Table

type TableStyle added in v0.1.2

type TableStyle struct {
	// DefaultWidth defines the default width of the table.
	DefaultWidth int
	// FitToTerminal defines if the table should fit to the terminal width.
	FitToTerminal bool

	// WrapText defines if the text should be wrapped.
	WrapText bool
	// Markdown defines if the text should be rendered as markdown.
	Markdown bool
	// HideEmpty defines if empty rows should be hidden.
	HideEmpty bool

	// OuterPadding defines the padding around the table.
	OuterPadding int
	// InnerPadding defines the padding between the cells.
	InnerPadding int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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