tableprinter

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 4 Imported by: 32

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

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.

Types

type TablePrinter

type TablePrinter interface {
	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.

Jump to

Keyboard shortcuts

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