output

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const (
	FlagNameFormat        = "format"
	FlagNameNoColor       = "no-color"
	FlagNameVerbose       = "verbose"
	FlagNameVerbosity     = "verbosity"
	FlagNameQuiet         = "quiet"
	FlagNameOrder         = "order"
	FlagNameLimit         = "limit"
	FlagNameOffset        = "offset"
	FlagNameTableMaxWidth = "table-width"
)
View Source
const TableRowSeparatorToken = "__melody_table_separator__"

Variables

This section is empty.

Functions

func ApplySortOrder added in v1.19.0

func ApplySortOrder[T any](items []T, order SortOrder)

func DebugFlags

func DebugFlags() []clicontract.Flag

DebugFlags returns StandardFlags with quiet defaulting to false: an introspection command exists to be read by a person, so the headers and context quiet suppresses are part of its answer.

func IsJsonFormat added in v1.19.0

func IsJsonFormat(format Format) bool

IsJsonFormat answers for both json spellings, and every place that used to compare against FormatJson asks through it: the two differ in whitespace alone, so a site that recognises one and not the other would color a banner into a document, or print a table where a document was asked for.

func MergeFlags

func MergeFlags(
	standard []clicontract.Flag,
	commandSpecific []clicontract.Flag,
) []clicontract.Flag

func Render

func Render(
	writer io.Writer,
	envelope Envelope,
	option Option,
) error

func ReverseItems added in v1.19.0

func ReverseItems[T any](items []T)

ReverseItems turns an ascending sort into a descending one in place. It runs before WindowItems so a descending --limit returns the tail of the ascending order rather than its head.

func SetApplicationVersion

func SetApplicationVersion(versionString string)

SetApplicationVersion declares the running application's own version to every command envelope: the meta of each rendered document and the application row of debug:version read it. An application calls this once from its composition root before Run, with the version it keeps wherever it keeps it — its own ldflags variable, its environment, a file. Undeclared, the application version renders empty in the meta and as <unknown> in debug:version; nothing substitutes melody's version for it.

func StandardFlags

func StandardFlags() []clicontract.Flag

StandardFlags returns the flag set every melody command accepts. Quiet defaults to true on purpose: a command's essential output — the document, table or summary the command exists to produce — is never gated on quiet, so the flag governs only headers and decoration, and the default keeps a scripted invocation clean without asking for it.

func WindowItems added in v1.19.0

func WindowItems[T any](
	items []T,
	limit int,
	offset int,
) []T

Types

type Envelope

type Envelope struct {
	Meta     Meta       `json:"meta"`
	Data     any        `json:"data"`
	Table    *TableData `json:"-"`
	Warnings []Warning  `json:"warnings"`
	Error    *Error     `json:"error"`
}

Envelope is not safe for concurrent use: a command that gathers warnings from parallel work funnels them through one goroutine rather than calling AddWarning from several.

func NewEnvelope

func NewEnvelope(
	meta Meta,
) Envelope

func (*Envelope) AddWarning

func (instance *Envelope) AddWarning(
	code string,
	message string,
	details map[string]any,
)

func (*Envelope) SetError

func (instance *Envelope) SetError(
	code string,
	message string,
	details map[string]any,
	cause *ErrorCause,
)

type Error

type Error struct {
	Code    string         `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details"`
	Cause   *ErrorCause    `json:"cause"`
}

func NewError

func NewError(code string, message string, details map[string]any, cause *ErrorCause) *Error

type ErrorCause

type ErrorCause struct {
	Message string         `json:"message"`
	Details map[string]any `json:"details"`
}

func NewErrorCause

func NewErrorCause(message string, details map[string]any) *ErrorCause

type Flags

type Flags struct {
	Format  Format    `json:"format"`
	NoColor bool      `json:"noColor"`
	Verbose bool      `json:"verbose"`
	Quiet   bool      `json:"quiet"`
	Order   SortOrder `json:"order"`
	Limit   int       `json:"limit"`
	Offset  int       `json:"offset"`
}

type Format

type Format string
const (
	FormatTable Format = "table"
	FormatJson  Format = "json"
	/* FormatJsonPretty is the same document indented for a person reading it by hand. It exists because json is the machine format and a machine reads a stream: one document per line, so a consumer can follow a long-running command live with a line reader and hand each line to a parser whole. Indenting by default made every document many lines, which every line-framed consumer — a log collector's json stage, a `while read line`, a `grep '"failed": true'` — receives as fragments that each fail to parse. */
	FormatJsonPretty Format = "json-pretty"
)

type JsonPrinter

type JsonPrinter struct {
}

func (*JsonPrinter) Print

func (instance *JsonPrinter) Print(
	writer io.Writer,
	envelope Envelope,
	option Option,
) error

the document is written on ONE line, terminated by the encoder's own newline, so a stream of them is a stream of records a line reader can hand to a parser whole — which is what a long-running command under --format=json is for, and what its own documentation promised while the indentation made every document a block of twenty. FormatJsonPretty is the same document with the indentation back, for the person reading it by hand; `| jq` does the same for a pipeline that already has it.

type ListPayload

type ListPayload[T any] struct {
	Items  []T `json:"items"`
	Total  int `json:"total"`
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

func NewListPayload

func NewListPayload[T any](
	items []T,
	total int,
	limit int,
	offset int,
) ListPayload[T]

type Meta

type Meta struct {
	Command              string    `json:"command"`
	Arguments            []string  `json:"arguments"`
	Flags                Flags     `json:"flags"`
	StartedAt            time.Time `json:"startedAt"`
	DurationMilliseconds int64     `json:"durationMilliseconds"`
	Version              Version   `json:"version"`
}

func NewMeta

func NewMeta(
	command string,
	arguments []string,
	option Option,
	startedAt time.Time,
	duration time.Duration,
	version Version,
) Meta

type Option

type Option struct {
	Format         Format
	NoColor        bool
	VerbosityLevel int
	Verbose        bool
	Quiet          bool
	Order          SortOrder
	Limit          int
	Offset         int
	TableMaxWidth  int
}

func DefaultOption

func DefaultOption() Option

func NormalizeOption

func NormalizeOption(option Option) Option

func ParseOptionFromCommand

func ParseOptionFromCommand(commandContext *clicontract.CommandContext) Option

type Printer

type Printer interface {
	Print(
		writer io.Writer,
		envelope Envelope,
		option Option,
	) error
}

func SelectPrinter

func SelectPrinter(option Option) Printer

type SortOrder

type SortOrder string
const (
	SortOrderAscending  SortOrder = "asc"
	SortOrderDescending SortOrder = "desc"
)

type TableBlock

type TableBlock struct {
	Title   string
	Columns []string
	Rows    [][]string
}

type TableBlockBuilder

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

func (*TableBlockBuilder) AddRow

func (instance *TableBlockBuilder) AddRow(cells ...string) *TableBlockBuilder

type TableBuilder

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

TableBuilder is not safe for concurrent use: a command that assembles its table from parallel work funnels the rows through one goroutine — a channel the workers send to — rather than sharing the builder between them.

func NewTableBuilder

func NewTableBuilder() *TableBuilder

func (*TableBuilder) AddBlock

func (instance *TableBuilder) AddBlock(
	title string,
	columns []string,
) *TableBlockBuilder

func (*TableBuilder) AddSummaryLine

func (instance *TableBuilder) AddSummaryLine(line string) *TableBuilder

func (*TableBuilder) Build

func (instance *TableBuilder) Build() *TableData

type TableData

type TableData struct {
	SummaryLines []string
	Blocks       []TableBlock
}

type TablePrinter

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

func NewDefaultTablePrinter

func NewDefaultTablePrinter() *TablePrinter

func NewTablePrinter

func NewTablePrinter(tableMaxWidth int) *TablePrinter

func (*TablePrinter) Print

func (instance *TablePrinter) Print(
	writer io.Writer,
	envelope Envelope,
	option Option,
) error

type Version

type Version struct {
	Application string `json:"application"`
	Melody      string `json:"melody"`
	Go          string `json:"go"`
}

type Warning

type Warning struct {
	Code    string         `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details"`
}

func NewWarning

func NewWarning(code string, message string, details map[string]any) Warning

Jump to

Keyboard shortcuts

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