pretty

package module
v0.0.0-...-1738579 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 12 Imported by: 5

README

go-pretty

Pretty printing for complex Go types

Documentation

Overview

Package pretty offers print functions that format values of any Go type in a compact single line string suitable for logging and debugging. Strings are escaped to be single line with fmt.Sprintf("%#q", s). %#q is used instead of %q to minimize the number of double quotes that would have to be escaped in JSON logs.

MaxStringLength, MaxErrorLength, MaxSliceLength can be set to values greater zero to prevent excessive log sizes. An ellipsis rune is used as last element to represent the truncated elements.

Index

Examples

Constants

View Source
const CircularRef = "CIRCULAR_REF"

CircularRef is a replacement token CIRCULAR_REF that will be printed instad of a circular data reference.

Variables

View Source
var DefaultPrinter = Printer{
	MaxStringLength: 200,
	MaxErrorLength:  2000,
	MaxSliceLength:  20,
}

DefaultPrinter is used by the package level print functions

Functions

func Fprint

func Fprint(w io.Writer, value any, indent ...string)

Fprint pretty prints a value to a io.Writer

func Fprintln

func Fprintln(w io.Writer, value any, indent ...string)

Fprint pretty prints a value to a io.Writer followed by a newline

func Indent

func Indent(source []byte, indent string, linePrefix ...string) []byte

Indent pretty printed source using the passed indent string and an optional linePrefix used for every line in case of a multiple line result.

func Print

func Print(value any, indent ...string)

Print pretty prints a value to os.Stdout

func PrintAsJSON

func PrintAsJSON(input any, indent ...string)

PrintAsJSON marshalles input as indented JSON and calles fmt.Println with the result. If indent arguments are given, they are joined into a string and used as JSON line indent. If no indet argument is given, two spaces will be used to indent JSON lines. A byte slice as input will be marshalled as json.RawMessage.

func Println

func Println(value any, indent ...string)

Println pretty prints a value to os.Stdout followed by a newline

Example
type Parent struct {
	Map map[int]string
}

type Struct struct {
	Parent
	Int        int
	unexported bool
	Str        string
	Sub        struct {
		Map map[string]string
	}
}

value := &Struct{
	Sub: struct{ Map map[string]string }{
		Map: map[string]string{
			"key": "value",
			// Note that the resulting `Multi\nLine` is not a valid Go string.
			// Double quotes are avoided for better readability of
			// pretty printed strings in JSON.
			"Multi\nLine": "true",
		},
	},
}

Println(value)
Println(value, "  ")
Println(value, "  ", "    ")
Output:

Struct{Parent{Map:nil};Int:0;Str:``;Sub:{Map:{`Multi\nLine`:`true`;`key`:`value`}}}
Struct{
  Parent{
    Map: nil
  }
  Int: 0
  Str: ``
  Sub: {
    Map: {
      `Multi\nLine`: `true`
      `key`: `value`
    }
  }
}
    Struct{
      Parent{
        Map: nil
      }
      Int: 0
      Str: ``
      Sub: {
        Map: {
          `Multi\nLine`: `true`
          `key`: `value`
        }
      }
    }

func Sprint

func Sprint(value any, indent ...string) string

Sprint pretty prints a value to a string

Types

type Nullable

type Nullable interface {
	// IsNull returns true if the implementing value is considered null.
	IsNull() bool
}

Nullable can be implemented to print "null" instead of the representation of the underlying type's value.

type Printable

type Printable interface {
	// PrettyPrint the implementation's data
	PrettyPrint(io.Writer)
}

Printable can be implemented to customize the pretty printing of a type.

type Printer

type Printer struct {
	// MaxStringLength is the maximum length for escaped strings.
	// Longer strings will be truncated with an ellipsis rune at the end.
	// A value <= 0 will disable truncating.
	MaxStringLength int

	// MaxErrorLength is the maximum length for escaped errors.
	// Longer errors will be truncated with an ellipsis rune at the end.
	// A value <= 0 will disable truncating.
	MaxErrorLength int

	// MaxSliceLength is the maximum length for slices.
	// Longer slices will be truncated with an ellipsis rune as last element.
	// A value <= 0 will disable truncating.
	MaxSliceLength int
}

Printer holds a pretty-print configuration

func (*Printer) Fprint

func (p *Printer) Fprint(w io.Writer, value any, indent ...string)

Fprint pretty prints a value to a io.Writer

func (*Printer) Fprintln

func (p *Printer) Fprintln(w io.Writer, value any, indent ...string)

Fprint pretty prints a value to a io.Writer followed by a newline

func (*Printer) Print

func (p *Printer) Print(value any, indent ...string)

Print pretty prints a value to os.Stdout

func (*Printer) Println

func (p *Printer) Println(value any, indent ...string)

Println pretty prints a value to os.Stdout followed by a newline

func (*Printer) Sprint

func (p *Printer) Sprint(value any, indent ...string) string

Sprint pretty prints a value to a string

Jump to

Keyboard shortcuts

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