pretty

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 13 Imported by: 1

README

pretty

like fmt.Println but more pretty and beautiful print Go values.

Dump

goutil/dump is a golang data printing toolkit that prints beautiful and easy to read go slice, map, struct data

Install

go get github.com/zhangyiming748/pretty/dump

Go docs

Usage

run demo: go run ./dump/_examples/demo1.go

package main

import "github.com/zhangyiming748/pretty"

// rum demo: go run ./dump/_examples/demo1.go
func main() {
	otherFunc1()
}

func otherFunc1() {
	pretty.P(
		23,
		[]string{"ab", "cd"},
		[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // len > 10
		map[string]interface{}{
			"key": "val", "sub": map[string]string{"k": "v"},
		},
		struct {
			ab string
			Cd int
		}{
			"ab", 23,
		},
	)
}

You will see:

More preview

  • print struct

  • nested struct

Options for dump
// Options for dumper
type Options struct {
	// Output the output writer
	Output io.Writer
	// NoType don't show data type TODO
	NoType bool
	// NoColor don't with color
	NoColor bool
	// IndentLen width. default is 2
	IndentLen int
	// IndentChar default is one space
	IndentChar byte
	// MaxDepth for nested print
	MaxDepth int
	// ShowFlag for display caller position
	ShowFlag int
	// CallerSkip skip for call runtime.Caller()
	CallerSkip int
	// ColorTheme for print result.
	ColorTheme Theme
	// SkipNilField value dump on map, struct.
	SkipNilField bool
	// SkipPrivate field dump on struct.
	SkipPrivate bool
	// BytesAsString dump handle.
	BytesAsString bool
	// MoreLenNL array/slice elements length > MoreLenNL, will wrap new line
	// MoreLenNL int
}

Functions API

func Clear(vs ...interface{})
func Config(fn func(opts *Options))
func Format(vs ...interface{}) string
func Fprint(w io.Writer, vs ...interface{})
func NoLoc(vs ...interface{})
func P(vs ...interface{})
func Print(vs ...interface{})
func Println(vs ...interface{})
func Reset()
func V(vs ...interface{})
type Dumper struct{ ... }
    func NewDumper(out io.Writer, skip int) *Dumper
    func NewWithOptions(fn func(opts *Options)) *Dumper
    func Std() *Dumper
type Options struct{ ... }
    func NewDefaultOptions(out io.Writer, skip int) *Options

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./timex/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./timex/...

Documentation

Overview

Package dump like fmt.Println but more pretty and beautiful print Go values.

Package goutil 💪 Useful utils for Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing, debug and more.

Index

Examples

Constants

View Source
const (
	Fnopos = 1 << iota // no position
	Ffunc
	Ffile
	Ffname
	Fline
)

These flags define which print caller information

Variables

This section is empty.

Functions

func Clear

func Clear(vs ...any)

Clear dump clear data, without location.

func Config

func Config(fns ...OptionFunc)

Config std dumper

func ErrOnFail added in v0.0.3

func ErrOnFail(cond bool, err error) error

ErrOnFail return input error on cond is false, otherwise return nil

func Format

func Format(vs ...any) string

Format like fmt.Println, but the output is clearer and more beautiful

func Fprint

func Fprint(w io.Writer, vs ...any)

Fprint like fmt.Println, but the output is clearer and more beautiful

func MustOK added in v0.0.3

func MustOK(err error)

MustOK if error is not empty, will panic

func NoLoc

func NoLoc(vs ...any)

NoLoc dump vars data, without location.

func OrError added in v0.0.3

func OrError(cond bool, err error) error

OrError return input error on cond is false, otherwise return nil

func OrReturn added in v0.0.3

func OrReturn[T any](cond bool, okFn, elseFn func() T) T

OrReturn call okFunc() on condition is true, else call elseFn()

func OrValue added in v0.0.3

func OrValue[T any](cond bool, okVal, elVal T) T

OrValue get

func P

func P(vs ...any)

P like fmt.Println, but the output is clearer and more beautiful

func PanicErr added in v0.0.3

func PanicErr(err error)

PanicErr if error is not empty, will panic

func PanicIfErr added in v0.0.3

func PanicIfErr(err error)

PanicIfErr if error is not empty, will panic

func Panicf added in v0.0.3

func Panicf(format string, v ...any)

Panicf format panic message use fmt.Sprintf

func PkgName added in v0.0.3

func PkgName(funcName string) string

PkgName get current package name. alias of stdutil.PkgName()

Usage:

funcName := goutil.FuncName(fn)
pgkName := goutil.PkgName(funcName)

func Print

func Print(vs ...any)

Print like fmt.Println, but the output is clearer and more beautiful

Example
Config(func(d *Options) {
	d.NoColor = true
})
defer Reset()

Print(
	23,
	[]string{"ab", "cd"},
	[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
	map[string]string{"key": "val"},
	map[string]any{
		"sub": map[string]string{"k": "v"},
	},
	struct {
		ab string
		Cd int
	}{
		"ab", 23,
	},
)

// Output like:
// PRINT AT github.com/zhangyiming748/pretty/dump.ExamplePrint(LINE 14):
// int(23)
// []string{"ab", "cd"}
// []int [
//   1,
//   2,
//   3,
//   4,
//   5,
//   6,
//   7,
//   8,
//   9,
//   10,
//   11,
// ]
// map[string]string {
//   key: "val",
// }
// map[string]interface {} {
//   sub: map[string]string{"k":"v"},
// }
// struct { ab string; Cd int } {
//   ab: "ab",
//   Cd: 23,
// }
//
Output:

func Println

func Println(vs ...any)

Println like fmt.Println, but the output is clearer and more beautiful

func Reset

func Reset()

Reset std dumper

func Reset2

func Reset2()

Reset2 reset std2 dumper

func V

func V(vs ...any)

V like fmt.Println, but the output is clearer and more beautiful

Types

type Dumper

type Dumper struct {
	*Options
	// contains filtered or unexported fields
}

Dumper struct definition

func NewDumper

func NewDumper(out io.Writer, skip int) *Dumper

NewDumper create

func NewWithOptions

func NewWithOptions(fns ...OptionFunc) *Dumper

NewWithOptions create

func Std

func Std() *Dumper

Std dumper

func Std2

func Std2() *Dumper

Std2 dumper

func (*Dumper) Dump

func (d *Dumper) Dump(vs ...any)

Dump vars

func (*Dumper) Fprint

func (d *Dumper) Fprint(w io.Writer, vs ...any)

Fprint print vars to io.Writer

func (*Dumper) Print

func (d *Dumper) Print(vs ...any)

Print vars. alias of Dump()

func (*Dumper) Println

func (d *Dumper) Println(vs ...any)

Println vars. alias of Dump()

func (*Dumper) ResetOptions

func (d *Dumper) ResetOptions()

ResetOptions for dumper

func (*Dumper) WithOptions

func (d *Dumper) WithOptions(fns ...OptionFunc) *Dumper

WithOptions for dumper

func (*Dumper) WithSkip

func (d *Dumper) WithSkip(skip int) *Dumper

WithSkip for dumper

func (*Dumper) WithoutColor

func (d *Dumper) WithoutColor() *Dumper

WithoutColor for dumper

type OptionFunc

type OptionFunc func(opts *Options)

OptionFunc type

func BytesAsString

func BytesAsString() OptionFunc

BytesAsString setting.

func SkipNilField

func SkipNilField() OptionFunc

SkipNilField setting.

func SkipPrivate

func SkipPrivate() OptionFunc

SkipPrivate field dump on struct.

func WithCallerSkip

func WithCallerSkip(skip int) OptionFunc

WithCallerSkip on print caller position information.

func WithoutColor

func WithoutColor() OptionFunc

WithoutColor setting.

func WithoutOutput

func WithoutOutput(out io.Writer) OptionFunc

WithoutOutput setting.

func WithoutPosition

func WithoutPosition() OptionFunc

WithoutPosition dont print call dump position information.

func WithoutType

func WithoutType() OptionFunc

WithoutType setting.

type Options

type Options struct {
	// Output the output writer
	Output io.Writer
	// NoType don't show data type TODO
	NoType bool
	// NoColor don't with color
	NoColor bool
	// IndentLen width. default is 2
	IndentLen int
	// IndentChar default is one space
	IndentChar byte
	// MaxDepth for nested print
	MaxDepth int
	// ShowFlag for display caller position
	ShowFlag int
	// CallerSkip skip for call runtime.Caller()
	CallerSkip int
	// ColorTheme for print result.
	ColorTheme Theme
	// SkipNilField value dump on map, struct.
	SkipNilField bool
	// SkipPrivate field dump on struct.
	SkipPrivate bool
	// BytesAsString dump handle.
	BytesAsString bool
}

Options for dumper

func NewDefaultOptions

func NewDefaultOptions(out io.Writer, skip int) *Options

NewDefaultOptions create.

type Theme

type Theme map[string]string

Theme color code/tag map for dump

type Value added in v0.0.3

type Value = stdutil.Value

Value alias of stdutil.Value

Directories

Path Synopsis
Package arrutil provides some util functions for array, slice
Package arrutil provides some util functions for array, slice
Package byteutil Provide some bytes utils functions or structs
Package byteutil Provide some bytes utils functions or structs
Package cflag Wraps and extends go `flag.FlagSet` to build simple command line applications
Package cflag Wraps and extends go `flag.FlagSet` to build simple command line applications
Package cliutil provides some util functions for CLI
Package cliutil provides some util functions for CLI
cmdline
Package cmdline provide quick build and parse cmd line string.
Package cmdline provide quick build and parse cmd line string.
termctrl
Package termctrl provide some simple term control utils
Package termctrl provide some simple term control utils
Package color is command line color library.
Package color is command line color library.
Package comdef provide some common type or constant definitions
Package comdef provide some common type or constant definitions
Package envutil provide some commonly ENV util functions.
Package envutil provide some commonly ENV util functions.
Package errorx provide an enhanced error implements for go, allow with stacktraces and wrap another error.
Package errorx provide an enhanced error implements for go, allow with stacktraces and wrap another error.
Package fmtutil provide some format util functions.
Package fmtutil provide some format util functions.
Package fsutil Filesystem util functions, quick create, read and write file.
Package fsutil Filesystem util functions, quick create, read and write file.
finder
Package finder provide a finder tool for find files
Package finder provide a finder tool for find files
internal
Package jsonutil provide some util functions for quick operate JSON data
Package jsonutil provide some util functions for quick operate JSON data
Package maputil provide map data util functions.
Package maputil provide map data util functions.
Package mathutil provide math(int, number) util functions.
Package mathutil provide math(int, number) util functions.
Package reflects Provide extends reflect util functions.
Package reflects Provide extends reflect util functions.
Package stdio provide some standard IO util functions.
Package stdio provide some standard IO util functions.
Package stdutil provide some standard util functions for go.
Package stdutil provide some standard util functions for go.
Package structs Provide some extends util functions for struct.
Package structs Provide some extends util functions for struct.
Package strutil provide some string,char,byte util functions
Package strutil provide some string,char,byte util functions
secutil
Package secutil provide some security utils
Package secutil provide some security utils
textscan
Package textscan Implemented a parser that quickly scans and analyzes text content.
Package textscan Implemented a parser that quickly scans and analyzes text content.
textutil
Package textutil provide some extra text handle util
Package textutil provide some extra text handle util
Package sysutil provide some system util functions.
Package sysutil provide some system util functions.
clipboard
Package clipboard provide a simple clipboard read and write operations.
Package clipboard provide a simple clipboard read and write operations.
cmdr
Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks
Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks
process
Package process Provide some process handle util functions
Package process Provide some process handle util functions
Package testutil provide some test help util functions.
Package testutil provide some test help util functions.
assert
Package assert provides some tool functions for use with the Go testing.
Package assert provides some tool functions for use with the Go testing.
Package timex provides an enhanced time.Time implementation.
Package timex provides an enhanced time.Time implementation.

Jump to

Keyboard shortcuts

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