Documentation
¶
Overview ¶
Package table provides terminal table output formatting using lipgloss.
Index ¶
- func Render(data *output.Table, opts ...Option) (string, error)
- func Write(w io.Writer, data *output.Table, opts ...Option) error
- type FooterProvider
- type Option
- type Table
- func (t *Table) AddRow(row ...string) *Table
- func (t *Table) AsTableRenderer() output.TableRenderer
- func (t *Table) Render() (string, error)
- func (t *Table) SetFooter(row ...string) *Table
- func (t *Table) SetHeaders(headers ...string) *Table
- func (t *Table) StyleFunc(fn func(row, col int) lipgloss.Style) *Table
- type TableProvider
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type FooterProvider ¶ added in v0.6.1
type FooterProvider interface {
}
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 ¶
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
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))
}
Output:
func New ¶
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))
}
Output:
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) SetFooter ¶ added in v0.6.1
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.
func (*Table) SetHeaders ¶
SetHeaders sets the table headers.
type TableProvider ¶ added in v0.30.0
TableProvider defines the interface for types that provide tabular data. The root package's Table satisfies this interface implicitly.