gop

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 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
  • Make rune, []byte, time, etc. data human readable
  • Color output with customizable theme
  • Stable map output with sorted by keys
  • Auto split multiline large string block
  • Prints the path of circular reference
  • Auto format inline json string
  • 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(),
        "chan":   make(chan int, 1),
        "struct": struct{ test int32 }{
            test: 13,
        },
        "json": `{"a"   : 1}`,
        "func": func(int) int { return 0 },
    }
    val["slice"].([]interface{})[1] = val["slice"]

    _ = gop.P(val)
}

The output will be something like:

// 2022-04-07T00:16:25.460227+08:00 example/main.go:26 (main.main)
gop.Obj/* len=10 */{
    "bool": true,
    "bytes": []byte("abc"),
    "chan": make(chan int, 1)/* 0xc00016e000 */,
    "func": (func(int) int)(nil)/* 0x10e36c0 */,
    "json": gop.JSONStr(gop.Obj{
        "a": float64(1),
    }, "{\"a\"   : 1}"),
    "lines": `multiline string
line two`/* len=25 */,
    "number": 1+1i,
    "slice": gop.Arr/* len=2 cap=2 */{
        1,
        gop.Circular("slice").(gop.Arr),
    },
    "struct": struct { test int32 }{
        test: int32(13),
    },
    "time": gop.Time(`2022-04-07T00:16:25.459776+08:00`, 2956128),
}

Documentation

Index

Constants

This section is empty.

Variables

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

DefaultTheme colors for Sprint

View Source
var LongBytesLen = 16

LongBytesLen is the length of that will be treated as long bytes

View Source
var LongStringLen = 16

LongStringLen is the length of that will be treated as long string

View Source
var NoColor = func() bool {
	_, has := os.LookupEnv("NO_COLOR")
	return has
}()

NoColor respects https://no-color.org/

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 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 GetPrivateFieldByName added in v0.22.2

func GetPrivateFieldByName(v reflect.Value, name string) reflect.Value

GetPrivateFieldByName is similar with GetPrivateField

func JSONBytes added in v0.20.0

func JSONBytes(v interface{}, raw string) []byte

JSONBytes returns the raw as []byte

func JSONStr added in v0.20.0

func JSONStr(v interface{}, raw string) string

JSONStr returns the raw

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, monotonic int) time.Time

Time from parsing s

Types

type Any added in v0.20.0

type Any interface{}

Any type

type Arr added in v0.20.0

type Arr []Any

Arr type

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
	// Forground type
	Forground
	// Default type
	Default

	// BgBlack type
	BgBlack
	// BgRed type
	BgRed
	// BgGreen type
	BgGreen
	// BgYellow type
	BgYellow
	// BgBlue type
	BgBlue
	// BgMagenta type
	BgMagenta
	// BgCyan type
	BgCyan
	// BgWhite type
	BgWhite
	// Background type
	Background
	// BgDefault type
	BgDefault

	// None type
	None Color = -1
)

type Obj added in v0.20.0

type Obj map[string]Any

Obj type

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

	// Comment type
	Comment

	// TypeName type
	TypeName

	// ParenOpen type
	ParenOpen
	// ParenClose type
	ParenClose

	// Dot type
	Dot
	// And type
	And

	// 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