Documentation ¶
Overview ¶
Package tableprinter facilitates rendering column-formatted data to a terminal and TSV-formatted data to a script or a file. It is suitable for presenting tabular data in a human-readable format that is guaranteed to fit within the given viewport, while at the same time offering the same data in a machine-readable format for scripts.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithColor ¶
WithColor sets the color function for the field. The function should transform a string value by wrapping it in ANSI escape codes. The color function will not be used if the table was initialized in non-terminal mode. The color function will be called before truncation and padding.
func WithPadding ¶ added in v2.5.0
WithPadding overrides the padding function for the field. The function should transform a string argument into a string that is padded to fit within the given display width. The default behavior is to pad fields with spaces except for the last field. The padding function will be called after truncation and before coloring. Pass nil to disable padding for this value.
func WithTruncate ¶
WithTruncate overrides the truncation function for the field. The function should transform a string argument into a string that fits within the given display width. The default behavior is to truncate the value by adding "..." in the end. The truncation function will be called before padding and coloring. Pass nil to disable truncation for this value.
Types ¶
type TablePrinter ¶
type TablePrinter interface { AddHeader([]string, ...fieldOption) AddField(string, ...fieldOption) EndRow() Render() error }
Example ¶
// information about the terminal can be obtained using the [pkg/term] package isTTY := true termWidth := 14 red := func(s string) string { return "\x1b[31m" + s + "\x1b[m" } t := New(os.Stdout, isTTY, termWidth) t.AddField("9", WithTruncate(nil)) t.AddField("hello") t.EndRow() t.AddField("10", WithTruncate(nil)) t.AddField("long description", WithColor(red)) t.EndRow() if err := t.Render(); err != nil { log.Fatal(err) } // stdout now contains: // 9 hello // 10 long de...
Output:
func New ¶
func New(w io.Writer, isTTY bool, maxWidth int) TablePrinter
New initializes a table printer with terminal mode and terminal width. When terminal mode is enabled, the output will be human-readable, column-formatted to fit available width, and rendered with color support. In non-terminal mode, the output is tab-separated and all truncation of values is disabled.