output

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 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",
	"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. NOTE: "count" intentionally not abbreviated - other tools output "count" directly

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 ErrorResult added in v1.3.0

type ErrorResult struct {
	Error   bool   `json:"error"`
	Message string `json:"message"`
}

ErrorResult is a helper for creating error responses in handlers.

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) PrintError added in v1.3.0

func (f *Formatter) PrintError(err error) int

PrintError outputs an error respecting JSON and Minimal modes. In JSON mode, outputs to stdout for parsing. In text mode, outputs to stderr. Returns the exit code (always 1 for errors).

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