GithubHelp home page GithubHelp logo

k0kubun / pp Goto Github PK

View Code? Open in Web Editor NEW
1.8K 19.0 93.0 173 KB

Colored pretty printer for Go language

Home Page: https://pkg.go.dev/github.com/k0kubun/pp/v3

License: MIT License

Go 100.00%
go pretty-printer

pp's Introduction

pp Go Go Reference

Colored pretty printer for Go language

Usage

Just call pp.Print().

import "github.com/k0kubun/pp/v3"

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()

See the documentation for API details.

Configuration

They can be customized globally with pp.Default.

pp.Default.SetColoringEnabled(false)
pp.Println() // no color

You can also create individual instances that do not interfere with the default printer:

mypp := pp.New()
mypp.SetColoringEnabled(false)
mypp.SetExportedOnly(true)
mypp.Println()

See PrettyPrinter documentation for all available configurations.

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.Default.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

pp's People

Contributors

alteholz avatar bl-ue avatar bquenin avatar deliangyang avatar dependabot[bot] avatar eun avatar huydx avatar itchyny avatar juneezee avatar k0kubun avatar kyokomi avatar mariusgrigoriu avatar motemen avatar rasecoiac03 avatar rickbau5 avatar sdidyk avatar shogo82148 avatar walf443 avatar yudai avatar zakuro9715 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pp's Issues

WithLineInfo print the wrong line

Vseion: v1.3.1-0.20200204103551-99835366d1cc
Code:

func main(){
    pp.WithLineInfo = true
    pp.Println("Hello")
}

Output:

github.com/k0kubun/[email protected]/pp.go:176
Hello

Expect:

/Users/linzhanwei/portal-room/main.go:3
Hello

pp/pp.go

Line 154 in 9983536

_, fn, line, _ := runtime.Caller(2) // 2 because current Caller is pp itself

runtime.Caller(2) should be 3

ps. Why the newest version prefix is v1.3.1? Do I import it wrong?

time.Time formatter

The output of time.Time is not so pretty. I think pry's output is good to imitate.

Add option to skip unexported fields

Sometimes, when printing datastructures that you don't have access to, it's not possible to modify the struct tags. In these situations, sometimes it is nice to be able to ignore unexported fields. Perhaps a flag on the printer, or a global variable (for the default printer) could work?

Option to format time.Time

The problem:
I have a struct with a bunch of populated fields.
I want to print the struct in order to use it in my unit test as data input and not to describe it manually.

The library fits it well, but a few formatting options are missing.

The solution
Add an optional formatting for fields like time.Time and may be the others, instead of raw string value I would like to see:

time.Date(1, 2, 3, 5, 6, 7, time.Local)

Any questions/concerns regarding the feature?

How to use the latest version?

I want to print numbers as decimal, but now it's hex format. I see the mater now supports a flag p.decimalUint, but my installed version is: github.com/k0kubun/pp v1.3.1-0.20200505181502-f5c257df338e, which doesn't contain this flag, it just prints with "%#v"

I installed it via github.com/k0kubun/pp master in go.mod and go mod tidy, but why it's still different from the master?

How can I use the latest code ?

Thanks.

Unsure why number coming out in base 16

My structs, when printed look like:

db.Company{
  ID:                          0x4c58da13a2c8001,
  Credits:                     0x01f2,
  RetryAttempts:               0x0000,
  LeadCallerID:                0x0000,
  CallRecording:               false,

How do I convert?

Sort map by key before printing

I wish there is an option / set of methods that will print maps with keys sorted in a best-effort manner. This can be extremely helpful in diffing outputs across runs.

(As you likely know, spew has a SortKeys global config that achieves exactly this.)

Thank you for your consideration.

Display of nil slice as same as an empty slice

fmt.Printf with %#v displays []int{} for empty int slice, and []int(nil) for nil int slice.
But pp.Printf with %s displays []int{} for both of empty int slice and nil int slice.
Is it expected behavior?

Reproduction

Go Playground: https://play.golang.org/p/M-2JZK--7uW

Code snippet

package main

import (
	"fmt"

	"github.com/k0kubun/pp"
)

func main() {
	pp.ColoringEnabled = false

	var nilInts []int
	pp.Printf("pp.Printf: %s\n", nilInts)
	fmt.Printf("fmt.Printf: %#v\n", nilInts)

	emptyInts := []int{}
	pp.Printf("pp.Printf: %s\n", emptyInts)
	fmt.Printf("fmt.Printf: %#v\n", emptyInts)
}

Its output

pp.Printf: []int{}
fmt.Printf: []int(nil)
pp.Printf: []int{}
fmt.Printf: []int{}

Fold large buffers

I want to print a struct contains a large buffer.

type Foo struct {
    B [65535]byte
}

func main() {
    m := Foo{}
    pp.Print(m)
}

But it's not pretty and takes a long time:

    ...
    ...
    ...
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    0x0,
    ...
    ...
    ...

I prefer large buffers printed like this:

main.Foo{
  B: [65535]uint8{...},
}

Feature request: Pretty print big.Int and big.Float?

cause stuff like

&big.Int{
              neg: false,
              abs: big.nat{
                0x908f8474ea971baf,
              },
}

does look too messy to be honest. Also, some default cool things like http response, rsa keys, etc. could be looking more cool (only most popular, much of default types should be printed explicitly)

Cannot format output address

p := map[string]any{"name": 10, "age": 33}

pp.Printf("address: %p\n", &p)
/* print:
%!p(string=&map[string]interface {}{
  "age":  33,
  "name": 10,
})
*/

fmt.Printf("address: %p\n", &p)
// print: 0xc00008a9a8

Printf not work correctly

func main() {
	pp.Printf("%d", 123)
	fmt.Println()
}

The code outputs

%!d(string=123)

I'm using the latest version: v3.1.1-0.20220216035903-af1e26e8087e

Verb '%T' is working incorrectly in formatted print

Problem:

Passing any variable to pp.Printf under formatting %T always results in string type, even when the actual type is different (for example, int).

Code to reproduce:

package main

import (
	"fmt"

	"github.com/k0kubun/pp"
)

func main() {
	a := 1
	fmt.Printf("%T\n", a)
	pp.Printf("%T\n", a)
}

Actual output:

int
string

Expected output:

int
int

Versions:

Go version - 1.17.1
Package (pp) version - 3.0.7 (github.com/k0kubun/pp/v3)

Option for thousands separator

It seems like it would be a good idea to have an option for a thousands separator for floats and ints, which can make structs with many large numbers easier to read. For example:

myStruct{
  myfloat: 1,000,000.00,
}

What do you think?

Option to print byte as decimal

When print the byte slice, pp print as hex, quite hard to see.

E.g:

pp.Println("65:", []byte{65})

will print:

print:
"65:" []uint8{
  0x41,
}

Expected something like:

"65:" []uint8{
  65,
}

Is this possible and useful?

features suggestions

hi,
I started to use this nice lib.
It would be nice to have a function :
func (pp *PrettyPrinter) SetMaxDepth(depth int) {
pp.maxDepth = depth
}
so that we can build different printers with different depths.
It would be nice too to have an option to force the representation of some kinds of variables, like force []byte to string.
or even time.Time to string instead of going deeper ?
cheers
jf

Feature request: Add `pp:"noprint"` tag

For hiding specific fields in struct:

type SomeObject struct {
	someField   int         // this field will be printed
	HiddenField interface{} `pp:"noprint"` // and this one will not.
}

go mod doesn't install 3.0.2

I have

import "github.com/k0kubun/pp"

in the main.go. But go get (with go mod) able to install 3.0.1 only:

go get
go: finding module for package github.com/k0kubun/pp
go: found github.com/k0kubun/pp in github.com/k0kubun/pp v3.0.1+incompatible

Could you please provide support for go mod ? I want to use 3.0.2 seamless

UPD
When using sha of the latest release:

go get github.com/k0kubun/pp@f5c257df338e921f81bfe022aba3d9ba2e00b7b4
go: github.com/k0kubun/pp f5c257df338e921f81bfe022aba3d9ba2e00b7b4 => v1.3.1-0.20200505181502-f5c257df338e
go: downloading github.com/k0kubun/pp v1.3.1-0.20200505181502-f5c257df338e

v1.3.1, what?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.