pp

package module
v3.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2019 License: MIT Imports: 14 Imported by: 0

README

pp wercker status

Colored pretty printer for Go language

Usage

Just call pp.Print().

import "github.com/k0kubun/pp"

m := map[string]string{"foo": "bar", "hello": "world"}
pp.Print(m)

API

fmt package-like functions are provided.

pp.Print()
pp.Println()
pp.Sprint()
pp.Fprintf()
// ...

API doc is available at: http://godoc.org/github.com/k0kubun/pp

Custom colors

If you require, you may change the colors (all or some) for syntax highlighting:

// Create a struct describing your scheme
scheme := pp.ColorScheme{
	Integer:       pp.Green | pp.Bold,
	Float:         pp.Black | pp.BackgroundWhite | pp.Bold,
	String:        pp.Yellow,
}

// Register it for usage
pp.SetColorScheme(scheme)

Look into ColorScheme struct for the field names.

If you would like to revert to the default highlighting, you may do so by calling pp.ResetColorScheme().

Out of the following color flags, you may combine any color with a background color and optionally with the bold parameter. Please note that bold will likely not work on the windows platform.

// Colors
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White

// Background colors
BackgroundBlack
BackgroundRed
BackgroundGreen
BackgroundYellow
BackgroundBlue
BackgroundMagenta
BackgroundCyan
BackgroundWhite

// Other
Bold

// Special
NoColor

Demo

Timeline

UserStream event

Works on windows

License

MIT License

Documentation

Index

Constants

View Source
const (
	Black uint16
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)
View Source
const (
	BackgroundBlack uint16
	BackgroundRed
	BackgroundGreen
	BackgroundYellow
	BackgroundBlue
	BackgroundMagenta
	BackgroundCyan
	BackgroundWhite
)
View Source
const (
	// Bold flag for ColorScheme.
	Bold uint16 = 1<<bitsBold | NoColor
)
View Source
const (
	// No color
	NoColor uint16 = 1 << 15
)

Variables

View Source
var (
	// If the length of array or slice is larger than this,
	// the buffer will be shorten as {...}.
	BufferFoldThreshold = 1024
	// PrintMapTypes when set to true will have map types will always appended to maps.
	PrintMapTypes = true
)
View Source
var (
	// If you set false to this variable, you can use pretty formatter
	// without coloring.
	ColoringEnabled = true
)
View Source
var (

	// WithLineInfo add file name and line information to output
	// call this function with care, because getting stack has performance penalty
	WithLineInfo = false
)

Functions

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf formats given arguments and returns it as error type.

func Fatal

func Fatal(a ...interface{})

Fatal prints given arguments and finishes execution with exit status 1.

func Fatalf

func Fatalf(format string, a ...interface{})

Fatalf prints a given format and finishes execution with exit status 1.

func Fatalln

func Fatalln(a ...interface{})

Fatalln prints given arguments with newline and finishes execution with exit status 1.

func Fprint

func Fprint(w io.Writer, a ...interface{}) (n int, err error)

Fprint prints given arguments to a given writer.

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf prints format to a given writer.

func Fprintln

func Fprintln(w io.Writer, a ...interface{}) (n int, err error)

Fprintln prints given arguments to a given writer with newline.

func GetDefaultOutput

func GetDefaultOutput() io.Writer

GetDefaultOutput returns pp's default output.

func Print

func Print(a ...interface{}) (n int, err error)

Print prints given arguments.

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

Printf prints a given format.

func Println

func Println(a ...interface{}) (n int, err error)

Println prints given arguments with newline.

func ResetColorScheme

func ResetColorScheme()

ResetColorScheme resets colorscheme to default.

func ResetDefaultOutput

func ResetDefaultOutput()

Change Print* functions' output to default one.

func SetColorScheme

func SetColorScheme(scheme ColorScheme)

SetColorScheme takes a colorscheme used by all future Print calls.

func SetDefaultOutput

func SetDefaultOutput(o io.Writer)

Change Print* functions' output to a given writer. For example, you can limit output by ENV.

func init() {
	if os.Getenv("DEBUG") == "" {
		pp.SetDefaultOutput(ioutil.Discard)
	}
}

func Sprint

func Sprint(a ...interface{}) string

Sprint formats given arguemnts and returns the result as string.

func Sprintf

func Sprintf(format string, a ...interface{}) string

Sprintf formats with pretty print and returns the result as string.

func Sprintln

func Sprintln(a ...interface{}) string

Sprintln formats given arguemnts with newline and returns the result as string.

Types

type ColorScheme

type ColorScheme struct {
	Bool            uint16
	Integer         uint16
	Float           uint16
	String          uint16
	StringQuotation uint16
	EscapedChar     uint16
	FieldName       uint16
	PointerAdress   uint16
	Nil             uint16
	Time            uint16
	StructName      uint16
	ObjectLength    uint16
}

To use with SetColorScheme.

Jump to

Keyboard shortcuts

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