output

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package output provides output formatting for CLI commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsColorEnabled

func IsColorEnabled(mode string) bool

IsColorEnabled determines if color output should be enabled.

func SimpleTable

func SimpleTable(w io.Writer, headers []string, rows [][]string) error

SimpleTable is a convenience function for quick table output.

func WithMode

func WithMode(ctx context.Context, mode Mode) context.Context

WithMode returns a context with the given output mode.

func WriteJSON

func WriteJSON(w io.Writer, v any) error

WriteJSON writes v as indented JSON to w.

func WriteTSV

func WriteTSV(w io.Writer, headers []string, rows [][]string) error

WriteTSV writes rows as tab-separated values. If headers is non-empty, it's written as the first row.

Types

type Colors

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

Colors provides terminal color support.

func NewColors

func NewColors(mode string) *Colors

NewColors creates a Colors instance based on the mode setting. mode: "auto" (detect), "always" (force), "never" (disable)

func (*Colors) Bold

func (c *Colors) Bold(s string) string

Bold returns the string in bold.

func (*Colors) Cyan

func (c *Colors) Cyan(s string) string

Cyan returns the string in cyan.

func (*Colors) Dim

func (c *Colors) Dim(s string) string

Dim returns the string in a dimmed style.

func (*Colors) Enabled

func (c *Colors) Enabled() bool

Enabled returns whether colors are enabled.

func (*Colors) Error

func (c *Colors) Error(s string) string

Error returns the string styled as an error message (red).

func (*Colors) Magenta

func (c *Colors) Magenta(s string) string

Magenta returns the string in magenta.

func (*Colors) Success

func (c *Colors) Success(s string) string

Success returns the string styled as a success message (green).

func (*Colors) Warning

func (c *Colors) Warning(s string) string

Warning returns the string styled as a warning message (yellow).

type Formatter

type Formatter struct {
	Mode   Mode
	Writer io.Writer
}

Formatter provides a unified interface for outputting data.

func NewFormatter

func NewFormatter(w io.Writer, mode Mode) *Formatter

NewFormatter creates a formatter with the given mode.

func (*Formatter) Output

func (f *Formatter) Output(v any, headers []string, rows [][]string) error

Output writes data in the appropriate format. For JSON mode, v is encoded directly. For table/plain modes, headers and rows are used.

type Mode

type Mode int

Mode represents the output format mode.

const (
	// ModeTable outputs formatted tables (default).
	ModeTable Mode = iota
	// ModeJSON outputs JSON.
	ModeJSON
	// ModePlain outputs tab-separated values.
	ModePlain
)

func GetMode

func GetMode(ctx context.Context) Mode

GetMode returns the output mode from context, defaulting to ModeTable.

func ModeFromFlags

func ModeFromFlags(jsonFlag, plainFlag bool) Mode

ModeFromFlags returns the output mode based on command flags. JSON takes precedence over plain.

func (Mode) String

func (m Mode) String() string

String returns the string representation of the mode.

type Table

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

Table is a simple table renderer using tabwriter.

func NewTable

func NewTable(w io.Writer, headers ...string) *Table

NewTable creates a new table with the given headers.

func (*Table) AddRow

func (t *Table) AddRow(cells ...string)

AddRow adds a row to the table.

func (*Table) Render

func (t *Table) Render() error

Render writes the table to the underlying writer.

func (*Table) RowCount

func (t *Table) RowCount() int

RowCount returns the number of rows added.

type TableBuilder

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

TableBuilder provides a fluent interface for building tables.

func NewTableBuilder

func NewTableBuilder(w io.Writer) *TableBuilder

NewTableBuilder creates a new table builder.

func (*TableBuilder) Build

func (b *TableBuilder) Build() *Table

Build returns the configured table.

func (*TableBuilder) Headers

func (b *TableBuilder) Headers(headers ...string) *TableBuilder

Headers sets the table headers.

func (*TableBuilder) Render

func (b *TableBuilder) Render() error

Render writes the table immediately.

func (*TableBuilder) Row

func (b *TableBuilder) Row(cells ...string) *TableBuilder

Row adds a row to the table.

Jump to

Keyboard shortcuts

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