davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
6.05k stars 364 forks source link

Feature Request: add colorized output/option on console #20

Open kokizzu opened 9 years ago

kokizzu commented 9 years ago

It would be nice if this library as similar as possible to Ruby's awesome_print that gives colorized output image

davecgh commented 9 years ago

I'll take a look atwhat this will entail, but I suspect it will require a beefy terminal library and I'd prefer to not rely on anything not in the stdlib.

johngb commented 9 years ago

@davecgh I was looking into this earlier, and although it may not work on all terminals, using the ASCII escape codes works just fine for in iTerm, which I assume means that it should work in Unix based terminals. I haven't tested it in any other terminals though, so I can't be sure how widely compatible it is.

To use it, you basically just insert the relevant escape sequence into the string where you want it to change colour, and from that point on it's that colour until you change it to something else. Here's a quick example to illustrate what I mean.

package main

import (
    "fmt"
    "strings"

    "github.com/davecgh/go-spew/spew"
)

type Person struct {
    Name       string
    Age        int
    Children   *[]Person
    SpouseName string
}

func main() {
    cs := spew.NewDefaultConfig()
    cs.Indent = "    "

    t := Person{
        Name: "John",
        Age:  37,
        Children: &[]Person{
            Person{
                Name: "Amy",
                Age:  1,
            },
        },
        SpouseName: "Tetyana",
    }
    s := cs.Sdump(t)

    i := strings.Index(s, "Amy")
    lenAmy := len("Amy")
    red := "\x1b[31m"
    def := "\x1b[39;49m"
    ns := s[:i] + red + s[i:i+lenAmy] + def + s[i+lenAmy:]

    fmt.Println(ns)
}

And the output then looks like: screenshot 2014-12-29 13 18 46

pjebs commented 9 years ago

it's actually quite easy to do.

See generally:

https://github.com/op/go-logging

adamlc commented 6 years ago

Sorry to resurrect this old issue, it would be great to see a little more colour in the terminal 👍

awkj commented 5 years ago

Sorry to resurrect this old issue, it would be great to see a little more colour in the terminal 👍

Yes, I Agree