tableprinter

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 7 Imported by: 0

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 DisplayWidth

func DisplayWidth(s string) int

DisplayWidth calculates what the rendered width of string s will be.

func PadRight

func PadRight(maxWidth int, s string) string

PadRight returns a copy of the string s that has been padded on the right with whitespace to fit the maximum display width.

func Truncate

func Truncate(maxWidth int, s string) string

Truncate returns a copy of the string s that has been shortened to fit the maximum display width.

Types

type FieldOption

type FieldOption func(*tableField)

func WithColor

func WithColor(fn func(string) string) FieldOption

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

func WithPadding(fn func(int, string) string) FieldOption

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

func WithTruncate(fn func(int, string) string) FieldOption

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.

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, 0)
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...

func New

func New(w io.Writer, isTTY bool, maxWidth int, minColWidth 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.

type TablePrinterMock

type TablePrinterMock struct {
	// AddFieldFunc mocks the AddField method.
	AddFieldFunc func(s string, fieldOptions ...FieldOption)

	// AddHeaderFunc mocks the AddHeader method.
	AddHeaderFunc func(strings []string, fieldOptions ...FieldOption)

	// EndRowFunc mocks the EndRow method.
	EndRowFunc func()

	// RenderFunc mocks the Render method.
	RenderFunc func() error
	// contains filtered or unexported fields
}

TablePrinterMock is a mock implementation of TablePrinter.

func TestSomethingThatUsesTablePrinter(t *testing.T) {

	// make and configure a mocked TablePrinter
	mockedTablePrinter := &TablePrinterMock{
		AddFieldFunc: func(s string, fieldOptions ...FieldOption)  {
			panic("mock out the AddField method")
		},
		AddHeaderFunc: func(strings []string, fieldOptions ...FieldOption)  {
			panic("mock out the AddHeader method")
		},
		EndRowFunc: func()  {
			panic("mock out the EndRow method")
		},
		RenderFunc: func() error {
			panic("mock out the Render method")
		},
	}

	// use mockedTablePrinter in code that requires TablePrinter
	// and then make assertions.

}

func (*TablePrinterMock) AddField

func (mock *TablePrinterMock) AddField(s string, fieldOptions ...FieldOption)

AddField calls AddFieldFunc.

func (*TablePrinterMock) AddFieldCalls

func (mock *TablePrinterMock) AddFieldCalls() []struct {
	S            string
	FieldOptions []FieldOption
}

AddFieldCalls gets all the calls that were made to AddField. Check the length with:

len(mockedTablePrinter.AddFieldCalls())

func (*TablePrinterMock) AddHeader

func (mock *TablePrinterMock) AddHeader(strings []string, fieldOptions ...FieldOption)

AddHeader calls AddHeaderFunc.

func (*TablePrinterMock) AddHeaderCalls

func (mock *TablePrinterMock) AddHeaderCalls() []struct {
	Strings      []string
	FieldOptions []FieldOption
}

AddHeaderCalls gets all the calls that were made to AddHeader. Check the length with:

len(mockedTablePrinter.AddHeaderCalls())

func (*TablePrinterMock) EndRow

func (mock *TablePrinterMock) EndRow()

EndRow calls EndRowFunc.

func (*TablePrinterMock) EndRowCalls

func (mock *TablePrinterMock) EndRowCalls() []struct {
}

EndRowCalls gets all the calls that were made to EndRow. Check the length with:

len(mockedTablePrinter.EndRowCalls())

func (*TablePrinterMock) Render

func (mock *TablePrinterMock) Render() error

Render calls RenderFunc.

func (*TablePrinterMock) RenderCalls

func (mock *TablePrinterMock) RenderCalls() []struct {
}

RenderCalls gets all the calls that were made to Render. Check the length with:

len(mockedTablePrinter.RenderCalls())

Jump to

Keyboard shortcuts

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