printer

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewUnsupportedPrinterError

func NewUnsupportedPrinterError(ptype string) error

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.

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. Pass nil to disable truncation for this value.

type JSONPrinter

type JSONPrinter interface {
	Printer
}

func NewJSONPrinter

func NewJSONPrinter(w io.Writer) (jp JSONPrinter, err error)

NewJSONPrinter 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 ListPrinter added in v0.2.0

type ListPrinter interface {
	Printer
}

ListPrinter prints data as a list of key/value lines per object, separated by a blank line.

func NewListPrinter added in v0.2.0

func NewListPrinter(w io.Writer) (lp ListPrinter, err error)

type Printer

type Printer interface {
	AddColumns(columns ...string)
	AddField(string, ...FieldOption)
	AddTimeField(now, t time.Time, c func(string) string)
	EndRow()
	Render() error
}

type TablePrinter

type TablePrinter interface {
	Printer
}

func NewTablePrinter

func NewTablePrinter(w io.Writer, isTTY bool, maxWidth int) (tp TablePrinter, err error)

NewTablePrinter 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 UnsupportedPrinterError

type UnsupportedPrinterError struct {
	// contains filtered or unexported fields
}

func (*UnsupportedPrinterError) Error

func (e *UnsupportedPrinterError) Error() string

Jump to

Keyboard shortcuts

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