Documentation
¶
Overview ¶
Package table implements a row-based data set that can be printed with a header and space aligned columns. The text output looks like a spreadsheet.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotStruct = errors.New("not a struct")
ErrNotStruct is returned from a Table's Write method if the argument is not a struct.
Functions ¶
func AsJSON ¶ added in v0.6.0
func AsJSON() func(*Table)
AsJSON is an option setting function for New. It sets JSON as the default output format for Flush.
func AsYAML ¶ added in v0.6.0
func AsYAML() func(*Table)
AsYAML is an option setting function for New. It sets YAML as the default output format for Flush.
func WithColor ¶
WithColor is an option setting function for New. It replaces the default set of Colors with c.
func WithLabelFunction ¶
WithLabelFunction is an option setting function for New. This function convert struct field names into text header labels. The default behavior is to convert the CamelCase field names into UPPER_CASE labels. The "table" struct tags can be used to override this.
func WithWriter ¶
WithWriter is an option setting function for New. It replaces the default io.Writer with w. The io.Writer is used for all Table output.
Types ¶
type Colors ¶
type Colors struct {
Header []sgr.Param
EvenRow []sgr.Param
OddRow []sgr.Param
Empty []sgr.Param
Repeat []sgr.Param
Annotation []sgr.Param
}
Colors is the set styles/colors to be applied to Table elements.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table holds a slice of structs that can be Flush()ed as a Text table, or encoded as JSON or YAML.
func New ¶
New returns a new Table. The default settings can be overridden using the With* options setting functions. For example: WithColors() can be used to replace the default coloring scheme.
Example (AsJSON) ¶
var buf bytes.Buffer
t := New(AsJSON(), WithWriter(&buf))
t.Write(server{Name: "web-1", Status: "running", Port: 8080})
t.Write(server{Name: "web-2", Status: "stopped", Port: 8081})
_ = t.Flush()
fmt.Print(buf.String())
Output: [ { "Name": "web-1", "Status": "running", "Port": 8080 }, { "Name": "web-2", "Status": "stopped", "Port": 8081 } ]
Example (WithCustomColors) ¶
var buf bytes.Buffer
t := New(WithWriter(&buf), WithColor(&Colors{
Header: append([]sgr.Param{sgr.Bold}, color.Cyan...),
}))
t.Write(task{Name: "Fix bug", Priority: 8})
t.Write(task{Name: "Write docs", Priority: 3})
_ = t.Flush()
fmt.Print(buf.String())
Output: NAME PRIORITY Fix bug 8 Write docs 3
func NewJSON
deprecated
NewJSON returns a Table with JSON as the default for Flush.
Deprecated: Use New(AsJSON()) instead.
Example ¶
var buf bytes.Buffer
t := NewJSON(WithWriter(&buf))
t.Write(server{Name: "web-1", Status: "running", Port: 8080})
t.Write(server{Name: "web-2", Status: "stopped", Port: 8081})
_ = t.Flush()
fmt.Print(buf.String())
Output: [ { "Name": "web-1", "Status": "running", "Port": 8080 }, { "Name": "web-2", "Status": "stopped", "Port": 8081 } ]
func (*Table) Annotate ¶ added in v0.4.0
Annotate inserts a string into the table as a row. This is useful for inserting comments or other information that is not a struct. The string will be printed as-is, without any formatting or coloring.
Annotations are only used in text output, and are ignored in JSON or YAML formats.
Example ¶
var buf bytes.Buffer
t := New(WithWriter(&buf))
t.Write(server{Name: "web-1", Status: "running", Port: 8080})
t.Annotate("--- maintenance window ---")
t.Write(server{Name: "web-2", Status: "stopped", Port: 8081})
_ = t.Flush()
fmt.Print(buf.String())
Output: NAME STATUS PORT web-1 running 8080 --- maintenance window --- web-2 stopped 8081
func (*Table) Clear ¶ added in v0.5.0
func (t *Table) Clear()
Clear removes all rows and annotations from the table, allowing it to be reused. Configuration settings (colors, writer, style, label function) are preserved.
Example ¶
var buf bytes.Buffer
t := New(WithWriter(&buf))
// First batch of data
t.Write(server{Name: "web-1", Status: "running", Port: 8080})
_ = t.Flush()
fmt.Print(buf.String())
// Clear and reuse the same table
buf.Reset()
t.Clear()
// Second batch of data
t.Write(server{Name: "api-1", Status: "running", Port: 9000})
_ = t.Flush()
fmt.Print(buf.String())
Output: NAME STATUS PORT web-1 running 8080 NAME STATUS PORT api-1 running 9000
func (*Table) FlushText ¶
func (t *Table) FlushText()
FlushText flushes the Table data to its io.Writer as column aligned ANSI styled text. If the io.Writer is not a terminal no ANSI styles will be applied.
func (*Table) Write ¶
Write appends the struct a to the table as a row. The current table will be flushed if a new struct type is written. If a is not a struct, an error table will be added to the output.
Example ¶
var buf bytes.Buffer
t := New(WithWriter(&buf))
t.Write(server{Name: "web-1", Status: "running", Port: 8080})
t.Write(server{Name: "web-2", Status: "stopped", Port: 8081})
_ = t.Flush()
fmt.Print(buf.String())
Output: NAME STATUS PORT web-1 running 8080 web-2 stopped 8081
Directories
¶
| Path | Synopsis |
|---|---|
|
Package main shows some features of the table package.
|
Package main shows some features of the table package. |
|
Package sgr is a minimalist package for setting ANSI color terminal escape sequences.
|
Package sgr is a minimalist package for setting ANSI color terminal escape sequences. |
|
color
Package color provides default foreground and background color convenience variables.
|
Package color provides default foreground and background color convenience variables. |