Documentation
¶
Overview ¶
prettyx builds on the efficient formatting algorithm from Josh Baker's github.com/tidwall/pretty package. We inline the core routines so we can extend them with recursive JSON unwrapping and Lip Gloss styling.
Index ¶
- Variables
- func NewRenderer(w io.Writer, forceColor bool) *lipgloss.Renderer
- func Pretty(in []byte, opts *Options) ([]byte, error)
- func PrettyTo(w io.Writer, in []byte, opts *Options, palette *ColorPalette) error
- func PrettyWithRenderer(in []byte, opts *Options, renderer *lipgloss.Renderer, palette *ColorPalette) ([]byte, error)
- type ColorPalette
- type Options
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = &Options{Width: 80, Prefix: "", Indent: " ", SortKeys: false, NoUnwrap: false, ForceColor: false}
DefaultOptions holds the fallback pretty-print configuration.
var MaxNestedJSONDepth = 10
MaxNestedJSONDepth controls how deep we recursively parse JSON that appears inside string values. Set to 10 by default. Special case:
- If MaxNestedJSONDepth == 0, we still unwrap one level (i.e., parse the string as JSON once, but do not recurse further).
Example meanings:
0 -> unwrap once (non-recursive) 1 -> unwrap once (same as 0) 2+ -> unwrap up to that many recursive levels
Functions ¶
func NewRenderer ¶ added in v0.3.0
NewRenderer returns a lipgloss renderer configured for the given writer. When forceColor is true, ANSI colors will be emitted even if the writer does not look like a TTY (via termenv's unsafe mode).
func Pretty ¶
Pretty parses the input JSON, unwraps nested JSON strings (recursing up to MaxNestedJSONDepth), formats it, and colorizes it with lipgloss before returning the resulting bytes. The renderer automatically adapts to the detected color capabilities of os.Stdout.
func PrettyTo ¶
PrettyTo writes a pretty-printed, colorized JSON document to the provided writer using a renderer bound to that writer. Colors degrade automatically when the writer is not a TTY.
func PrettyWithRenderer ¶
func PrettyWithRenderer(in []byte, opts *Options, renderer *lipgloss.Renderer, palette *ColorPalette) ([]byte, error)
PrettyWithRenderer mirrors Pretty but allows callers to provide a custom lipgloss renderer and palette. Passing palette == nil uses the DefaultColorPalette derived from the renderer.
Types ¶
type ColorPalette ¶
type ColorPalette struct {
Key lipgloss.Style
String lipgloss.Style
Number lipgloss.Style
True lipgloss.Style
False lipgloss.Style
Null lipgloss.Style
Brackets lipgloss.Style
Punctuation lipgloss.Style
}
ColorPalette configures the Lip Gloss styles for each JSON token class.
func DefaultColorPalette ¶
func DefaultColorPalette(renderer *lipgloss.Renderer) ColorPalette
DefaultColorPalette returns a VS Code-inspired theme tuned for Lip Gloss. The renderer governs how colors degrade on limited terminals.
func NoColorPalette ¶
func NoColorPalette(renderer *lipgloss.Renderer) ColorPalette
NoColorPalette disables all styling while still routing through lipgloss so we benefit from its rendering decisions (width handling, etc.).
type Options ¶
type Options struct {
// Width is the max column width for single-line arrays. Default 80.
Width int
// Prefix is applied to every output line. Default "".
Prefix string
// Indent defines the nested indentation. Default two spaces.
Indent string
// SortKeys sorts object keys alphabetically when true. Default false.
SortKeys bool
// NoUnwrap disables recursive decoding of JSON strings. This is equivalent to
// jq's default behaviour (unless you call fromjson) and mirrors the CLI's
// -no-unwrap flag. When true, prettyx leaves any JSON-looking strings as-is.
NoUnwrap bool
// ForceColor forces lipgloss to emit ANSI color even when the destination
// writer is not detected as a TTY.
ForceColor bool
}
Options controls the pretty-printing behavior. It mirrors the struct from github.com/tidwall/pretty.