gop

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 17 Imported by: 0

README

Go Pretty Print Value

Make a random Go value human readable. The output format uses valid golang syntax, so you don't have to learn any new knowledge to understand the output.

Features

  • Uses valid golang syntax to print the data
  • Prints the path of circular reference
  • Make rune, []byte, time, etc. data human readable
  • No invisible char in string
  • Color output with customizable theme
  • Stable map output with sorted by keys
  • Auto split multiline large string block
  • Low-level API to extend the lib

Usage

Usually, you only need to use gop.P function:

package main

import (
    "time"

    "github.com/ysmood/got/lib/gop"
)

func main() {
    val := map[string]interface{}{
        "bool":   true,
        "number": 1 + 1i,
        "bytes":  []byte{97, 98, 99},
        "lines":  "multiline string\nline two",
        "slice":  []interface{}{1, 2},
        "time":   time.Now(),
        "struct": struct{ test int32 }{
            test: 13,
        },
    }
    val["slice"].([]interface{})[1] = val["slice"]

    gop.P(val)
}

The output will be something like:

// 2022-03-29T09:58:44.690232+08:00 (main.main) lib/gop/example/main.go:23
map[string]interface {}/* len=7 */{
    "bool": true,
    "bytes": []byte("abc")/* len=3 */,
    "lines": "" +
        "multiline string\n" +
        "line two"/* len=25 */,
    "number": 1+1i,
    "slice": []interface {}/* len=2 cap=2 */{
        1,
        gop.Circular("slice").([]interface {}),
    },
    "struct": struct { test int32 }{
        test: int32(13),
    },
    "time": gop.Time("2022-03-29T09:58:44.689892+08:00"),
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTheme = func(t Type) Color {
	switch t {
	case TypeName:
		return Cyan
	case Bool:
		return Blue
	case Rune, Byte, String:
		return Yellow
	case Number:
		return Green
	case Chan, Func, UnsafePointer:
		return Magenta
	case Comment:
		return White
	case PointerCircular:
		return Red
	default:
		return None
	}
}

DefaultTheme colors for Sprint

View Source
var NoTheme = func(t Type) Color {
	return None
}

NoTheme colors for Sprint

View Source
var Stdout io.Writer = os.Stdout

Stdout is the default stdout for gop.P .

View Source
var SupportsColor = func() bool {
	b, _ := exec.Command("tput", "colors").CombinedOutput()
	n, _ := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 32)
	return n > 0
}()

SupportsColor returns true if current shell supports ANSI color

Functions

func Base64

func Base64(s string) []byte

Base64 returns the []byte that s represents

func Circular added in v0.17.0

func Circular(path ...interface{}) interface{}

Circular reference of the path from the root

func ColorStr

func ColorStr(c Color, s string) string

ColorStr string

func Duration added in v0.15.4

func Duration(s string) time.Duration

Duration from parsing s

func F

func F(v interface{}) string

F is a shortcut for Format with color

func Format

func Format(ts []*Token, theme func(Type) Color) string

Format a list of tokens

func GetPrivateField

func GetPrivateField(v reflect.Value, i int) reflect.Value

GetPrivateField field value via field index TODO: we can use a LRU cache for the copy of the values, but it might be trivial for just testing.

func P

func P(values ...interface{}) error

P pretty print the values

func Plain

func Plain(v interface{}) string

Plain is a shortcut for Format with plain color

func Ptr added in v0.16.0

func Ptr(v interface{}) interface{}

Ptr returns a pointer to v

func StripColor added in v0.15.2

func StripColor(str string) string

StripColor is copied from https://github.com/acarl005/stripansi

func Time added in v0.15.4

func Time(s string) time.Time

Time from parsing s

Types

type Color

type Color int

Color type

const (
	// Black type
	Black Color = iota + 30
	// Red type
	Red
	// Green type
	Green
	// Yellow type
	Yellow
	// Blue type
	Blue
	// Magenta type
	Magenta
	// Cyan type
	Cyan
	// White type
	White

	// None type
	None Color = -1
)

type Token

type Token struct {
	Type    Type
	Literal string
}

Token represents a symbol in value layout

func Tokenize

func Tokenize(v interface{}) []*Token

Tokenize a random Go value

type Type

type Type int

Type of token

const (
	// Nil type
	Nil Type = iota
	// Bool type
	Bool
	// Number type
	Number
	// Float type
	Float
	// Complex type
	Complex
	// String type
	String
	// Byte type
	Byte
	// Rune type
	Rune
	// Chan type
	Chan
	// Func type
	Func
	// UnsafePointer type
	UnsafePointer

	// Comment type
	Comment

	// TypeName type
	TypeName

	// ParenOpen type
	ParenOpen
	// ParenClose type
	ParenClose

	// PointerOpen type
	PointerOpen
	// PointerClose type
	PointerClose
	// PointerCircular type
	PointerCircular

	// SliceOpen type
	SliceOpen
	// SliceItem type
	SliceItem
	// InlineComma type
	InlineComma
	// Comma type
	Comma
	// SliceClose type
	SliceClose

	// MapOpen type
	MapOpen
	// MapKey type
	MapKey
	// Colon type
	Colon
	// MapClose type
	MapClose

	// StructOpen type
	StructOpen
	// StructKey type
	StructKey
	// StructField type
	StructField
	// StructClose type
	StructClose
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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