Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func VisibleWidth ¶
VisibleWidth computes the visible width of a string, ignoring ANSI escapes.
Types ¶
type Cell ¶
type Cell struct {
Text string // rendered output for the current mode; may contain ANSI/OSC 8
Plain string // semantic visible text; used for filtering and fallback behavior
SortKey any // typed key used for sorting; nil means unsortable
}
Cell represents a single table cell with structured content.
func DisplayOnly ¶
DisplayOnly creates a Cell with no sort key (unsortable).
func SortableCell ¶
SortableCell creates a Cell with explicit text, plain, and sort key values.
func StyledCell ¶
StyledCell creates a Cell with styled display text and plain text for filtering, using the plain text as a string sort key.
type Column ¶
type Column[T any] struct { Name string Header string Render func(item T, ctx *RenderContext) Cell Flex bool // if true, this column shrinks to fit terminal width }
Column defines a table column for items of type T.
type Grid ¶
type Grid struct {
ColumnPadding int
FlexCols []int // indexes of columns that shrink to fit
MaxWidth int // terminal width; flex columns shrink to fit (0 = disabled)
Padding Padding
Rows [][]string
TTY bool // when true, wrap spaces in SGR 8 to prevent tab optimization
WidthMethod xansi.Method // width measurement; zero value is xansi.WcWidth
}
Grid is a table of cell values (rows x columns) with alignment options.
func NewGrid ¶
func NewGrid(rows [][]string, opts ...GridOption) *Grid
NewGrid creates a Grid with the given rows and applies any options. Default column padding is 2 spaces, left-aligned.
func (*Grid) AlignColumns ¶
AlignColumns aligns the grid into padded strings with gaps between columns. It returns the aligned strings and the computed visible width of each column.
type GridOption ¶
type GridOption func(*Grid)
GridOption configures a Grid.
func WithColumnPadding ¶
func WithColumnPadding(n int) GridOption
WithColumnPadding sets the number of spaces between columns.
func WithFlexColumns ¶
func WithFlexColumns(cols ...int) GridOption
WithFlexColumns sets the columns that shrink to fit MaxWidth.
func WithPadding ¶
func WithPadding(p Padding) GridOption
WithPadding sets the text alignment within columns.
func WithWidthMethod ¶
func WithWidthMethod(m xansi.Method) GridOption
WithWidthMethod sets the method used to measure and truncate cell text. The default (xansi.WcWidth) counts per-rune wcwidth; use xansi.GraphemeWidth on terminals that perform grapheme clustering (mode 2027), where ZWJ and VS16 emoji sequences occupy a single glyph.
type HeaderRenderer ¶
type HeaderRenderer func(name, header string, ctx *RenderContext) string
HeaderRenderer customizes how column headers are rendered before alignment. The returned string may include ANSI styling.
type Option ¶
type Option func(*config)
Option configures a Renderer.
func WithGridOptions ¶
func WithGridOptions(opts ...GridOption) Option
WithGridOptions sets grid options applied to every grid the renderer builds, e.g. WithWidthMethod(xansi.GraphemeWidth) for grapheme-clustering terminals. Options that the renderer manages itself (flex columns, max width, TTY) are overridden by the renderer's own configuration.
func WithHeaderRenderer ¶
func WithHeaderRenderer(fn HeaderRenderer) Option
WithHeaderRenderer sets a custom header renderer used before column alignment.
func WithReverse ¶
WithReverse sets whether to reverse row order (newest first at top).
func WithShowIndex ¶
WithShowIndex sets whether to show row indices.
func WithTermWidth ¶
WithTermWidth sets the terminal width for flex columns. When set, columns marked Flex=true are truncated so rows fit within this width.
type RenderContext ¶
RenderContext holds shared state for column renderers.
func NewRenderContext ¶
func NewRenderContext(theme Theme, ansiWriter *ansi.ANSI) *RenderContext
NewRenderContext creates a RenderContext.
func (*RenderContext) AssignEntityColor ¶
func (ctx *RenderContext) AssignEntityColor(key string) color.Color
AssignEntityColor returns a stable color for the given key, cycling through the theme's EntityColors palette.
type RenderedTable ¶
type RenderedTable[T any] struct { Header string Rows []Row[T] ColWidths []int // visible width of each column (including padding/indicators) }
RenderedTable holds the result of rendering items into a table.
func (RenderedTable[T]) String ¶
func (t RenderedTable[T]) String() string
String returns the full table output with header and rows joined by newlines.
type Renderer ¶
type Renderer[T any] struct { // contains filtered or unexported fields }
Renderer renders a slice of items as an aligned table.
func NewRenderer ¶
func NewRenderer[T any]( columns []Column[T], ctx *RenderContext, opts ...Option, ) *Renderer[T]
NewRenderer creates a Renderer for the given columns.
func (*Renderer[T]) Render ¶
func (r *Renderer[T]) Render(items []T) RenderedTable[T]
Render renders items as a table and returns a RenderedTable with header and rows. The input items slice is never mutated.
func (*Renderer[T]) RenderHeaderOnly ¶
RenderHeaderOnly renders just the header line using provided sample widths for column alignment. This is useful when there are no data rows to derive widths from. Each sampleWidth entry sets the minimum width for that column.
type Row ¶
type Row[T any] struct { Item T Cells []Cell // rendered cell values Display string // aligned display line, set after formatting }
Row pairs an item with its rendered cell values.