output

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package output provides shared output formatting utilities for CLI commands. It supports four output modes:

  • Default: Human-readable text output
  • JSON: Pretty-printed JSON output
  • Minimal: Token-optimized text output (no empty values, relative paths)
  • Minimal+JSON: Single-line abbreviated JSON (minimal keys, no empty fields)

Index

Constants

This section is empty.

Variables

View Source
var KeyAbbreviations = map[string]string{
	"file":        "f",
	"line":        "l",
	"name":        "n",
	"type":        "t",
	"question":    "q",
	"answer":      "a",
	"occurrences": "o",
	"directories": "dirs",
	"path":        "p",
	"message":     "msg",
	"status":      "s",
	"count":       "c",
	"total":       "tot",
	"value":       "v",
	"key":         "k",
	"result":      "r",
	"error":       "err",
	"success":     "ok",
	"files":       "fs",
	"matches":     "m",
}

KeyAbbreviations maps full key names to abbreviated versions for minimal JSON output.

Functions

func FilterEmpty

func FilterEmpty(m map[string]interface{}) map[string]interface{}

FilterEmpty removes empty/zero values from a map.

func RelativePath

func RelativePath(path, base string) string

RelativePath converts an absolute path to a path relative to the given base. If the path cannot be made relative, it returns the original path.

func RelativePathCwd

func RelativePathCwd(path string) string

RelativePathCwd converts an absolute path to a path relative to the current working directory.

Types

type Formatter

type Formatter struct {
	JSON    bool // Output as JSON
	Minimal bool // Output in minimal/token-optimized mode
	Writer  io.Writer
}

Formatter handles output formatting with support for JSON and minimal modes.

func New

func New(jsonOutput, minimal bool, w io.Writer) *Formatter

New creates a new Formatter with the given options.

func (*Formatter) Print

func (f *Formatter) Print(data interface{}, textFunc func(io.Writer, interface{})) error

Print outputs the data according to the formatter's configuration. For JSON mode, it marshals the data. For text mode, it uses the textFunc if provided. textFunc is called for default text output; if nil, JSON is used as fallback.

func (*Formatter) PrintLine

func (f *Formatter) PrintLine(key string, value interface{})

PrintLine prints a key-value pair to the writer, respecting minimal mode.

func (*Formatter) PrintSection

func (f *Formatter) PrintSection(title string)

PrintSection prints a section header.

Jump to

Keyboard shortcuts

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