format

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStreamingLimit = errors.New("streaming record limit exceeded")

Functions

func FormatCSS added in v0.16.0

func FormatCSS(buf []byte, p *core.Printer) error

FormatCSS formats the provided CSS to the Printer.

func FormatCSSIndented added in v0.16.0

func FormatCSSIndented(buf []byte, p *core.Printer, baseIndent int) error

FormatCSSIndented formats CSS with a base indentation level. Used for formatting CSS embedded in HTML <style> tags.

func FormatCSV added in v0.16.0

func FormatCSV(buf []byte, p *core.Printer) error

FormatCSV formats the provided CSV data to the Printer.

func FormatEventStream added in v0.6.0

func FormatEventStream(r io.Reader, p *core.Printer) error

FormatEventStream formats the provided stream of server sent events to the Printer, flushing after each event.

func FormatGRPCStream added in v0.17.0

func FormatGRPCStream(r io.Reader, md protoreflect.MessageDescriptor, p *core.Printer) error

FormatGRPCStream formats an uncompressed gRPC response stream by reading and formatting each length-prefixed frame as it arrives. It is retained for callers that do not have response metadata; compressed frames are rejected with an actionable missing-encoding error.

func FormatGRPCStreamWithEncoding added in v0.28.0

func FormatGRPCStreamWithEncoding(r io.Reader, md protoreflect.MessageDescriptor, p *core.Printer, encoding string) error

FormatGRPCStreamWithEncoding formats a gRPC response stream incrementally. The grpc-encoding value applies to frames whose compressed flag is set.

func FormatHTML added in v0.16.0

func FormatHTML(buf []byte, w *core.Printer) error

FormatHTML formats the provided HTML to the Printer.

func FormatJSON

func FormatJSON(buf []byte, p *core.Printer) error

FormatJSON formats the provided raw JSON data to the Printer.

func FormatJSONLine added in v0.19.0

func FormatJSONLine(buf []byte, p *core.Printer) error

FormatJSONLine formats the provided raw JSON data as a single compact line to the Printer.

func FormatMarkdown added in v0.20.0

func FormatMarkdown(buf []byte, p *core.Printer) error

FormatMarkdown formats the provided Markdown to the Printer.

func FormatMarkdownWithOptions added in v0.28.0

func FormatMarkdownWithOptions(buf []byte, p *core.Printer, options MarkdownOptions) error

FormatMarkdownWithOptions formats the provided Markdown to the Printer using the supplied terminal presentation options.

func FormatMsgPack added in v0.12.0

func FormatMsgPack(buf []byte, p *core.Printer) error

FormatMsgPack formats the provided raw MessagePack data to the Printer as JSON.

MessagePack map keys are not restricted to strings, unlike JSON object keys. This parser converts the supported scalar key types to their JSON string representation before formatting. Binary keys use base64 so arbitrary bytes remain representable. It also validates strings itself so malformed strings produce a parser error before JSON formatting.

func FormatNDJSON added in v0.5.0

func FormatNDJSON(r io.Reader, p *core.Printer) error

FormatNDJSON streams newline-delimited JSON to the Printer, flushing after each record. It uses explicit line boundaries so a partial record never causes the decoder to retain an unbounded stream prefix.

func FormatProtobuf added in v0.16.0

func FormatProtobuf(buf []byte, p *core.Printer) error

FormatProtobuf formats the provided raw protobuf data to the Printer.

func FormatProtobufWithDescriptor added in v0.16.0

func FormatProtobufWithDescriptor(buf []byte, md protoreflect.MessageDescriptor, p *core.Printer) error

FormatProtobufWithDescriptor formats protobuf data as JSON using a message descriptor.

func FormatProtobufWithSchema added in v0.16.0

func FormatProtobufWithSchema(buf []byte, schema *fetchproto.Schema, typeName string, p *core.Printer) error

FormatProtobufWithSchema formats protobuf data as JSON using the provided schema.

func FormatXML

func FormatXML(buf []byte, w *core.Printer) error

FormatXML formats the provided XML to the Printer.

func FormatYAML added in v0.18.0

func FormatYAML(buf []byte, p *core.Printer) error

FormatYAML formats the provided raw YAML data to the Printer.

Types

type BufferedFormatter added in v0.20.0

type BufferedFormatter func(buf []byte, p *core.Printer) error

BufferedFormatter formats buffered bytes to the Printer.

func GetBuffered added in v0.20.0

func GetBuffered(ct ContentType) BufferedFormatter

GetBuffered returns the buffered formatter for the given content type, or nil.

type ContentType added in v0.20.0

type ContentType int

ContentType represents a recognized response content type.

const (
	TypeUnknown ContentType = iota
	TypeCSS
	TypeCSV
	TypeGRPC
	TypeHTML
	TypeImage
	TypeJSON
	TypeMarkdown
	TypeMsgPack
	TypeNDJSON
	TypeProtobuf
	TypeSSE
	TypeXML
	TypeYAML
)

func GetContentType added in v0.20.0

func GetContentType(headers http.Header) (ContentType, string)

GetContentType parses the Content-Type header and returns the detected ContentType and charset. Returns TypeUnknown if the type is not recognized.

func SniffContentType added in v0.20.0

func SniffContentType(buf []byte) ContentType

SniffContentType attempts to detect the content type of the provided bytes by examining the leading bytes of the body. It is used as a fallback when no Content-Type header is present in the HTTP response.

type MarkdownOptions added in v0.28.0

type MarkdownOptions struct {
	MaxWidth int

	// RenderImage renders a Markdown image for terminal presentation and
	// returns true when it wrote the image. It is ignored for non-terminal
	// output. The callback owns image fetching and safety policy.
	RenderImage func(destination string) bool
}

MarkdownOptions controls terminal-only Markdown presentation.

MaxWidth is a maximum display width in terminal columns. A zero value uses the smaller of the current terminal width and 100 columns when the destination is a TTY. Non-terminal output remains raw Markdown regardless of this value.

type StreamingFormatter added in v0.20.0

type StreamingFormatter func(r io.Reader, p *core.Printer) error

StreamingFormatter formats a stream to the Printer.

func GetStreaming added in v0.20.0

func GetStreaming(ct ContentType) StreamingFormatter

GetStreaming returns the streaming formatter for the given content type, or nil.

Jump to

Keyboard shortcuts

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